Aller au contenu

Self-hosted libSQL server

Ce contenu n’est pas encore disponible dans votre langue.

The sqld (“SQL daemon”) project is a server mode for libSQL created by Turso. This guide will go over setting up and configuring sqld to work from a docker container for your StudioCMS project.

Learn more about sqld using their Documentation^

The final file structure will look something like this if testing locally:

  • docker-compose.yml
  • Répertoiredata/
  • Répertoirekeys/
  • Répertoiremy-studiocms-project/
    • astro.config.mjs
    • studiocms.config.mjs
    • package.json
    • Répertoiresrc/

From inside the keys directory run the following command to create a new PKCS#8-encoded Ed25519 key-pair:

Terminal window
openssl genpkey -algorithm Ed25519 -out ./libsql.pem

Then extract the public key with:

Terminal window
openssl pkey -in ./libsql.pem -pubout -out ./libsql.pub

This will generate a new PKCS#8 key-pair that will be used for the sqld server.

Navigate to your studiocms project directory and run the following command:

Fenêtre de terminal
npm run studiocms crypto gen-jwt "../keys/libsql.pem" -c "iss=admin" -e 31556926

The output is the JWT auth token encrypted with your private key, which will be used for libSQL authentication. Keep in mind that the token will be valid for 1 year!

Update your StudioCMS .env with the following:

Section titled “Update your StudioCMS .env with the following:”
Terminal window
ASTRO_DB_REMOTE_URL=http://localhost:8080 # This should point to your docker server/local system
ASTRO_DB_APP_TOKEN= # paste your base64URL encoded JWT auth token here

Create a new docker-compose.yml file outside of the StudioCMS project directory, for the libSQL instance. At this point you have two options to configure libSQL authentication:

  • Using the public key file itself
  • Using the JWT token as an environment variable (the same ASTRO_DB_APP_TOKEN value you used in your .env file)
services:
libsql:
image: ghcr.io/tursodatabase/libsql-server:latest
platform: linux/amd64
ports:
- "8080:8080"
- "5001:5001"
environment:
- SQLD_NODE=primary
- SQLD_AUTH_JWT_KEY_FILE=/home/.ssh/libsql.pub
volumes:
- ./data/libsql:/var/lib/sqld
- ./keys/libsql.pub:/home/.ssh/libsql.pub
  1. Start the Docker container:

    From the base directory (or the server if you went that route) run the following command to start the libSQL Server container

    Terminal window
    docker compose up -d
  2. Sync your StudioCMS database schema:

    Move into your StudioCMS project directory and run the following command:

    Fenêtre de terminal
    npx astro db push --remote
  3. Follow the first time setup for your new database.

If you are looking for an option for Self-hosting your own libSQL database with StudioCMS sqld is a viable option and is not very complicated to get up and running if you do it the right way, following this guide provides you with a secure libSQL server to use alongside your StudioCMS project.