Set Up a Keystone 6 Server Behind an Nginx Reverse Proxy on Ubuntu 24.04
Create a Keystone 6 project on Ubuntu 24.04, run it in production mode under systemd, and put Nginx with a TLS certificate in front of it.
By the end of this guide you will have a Keystone 6 application running in production mode as a systemd service on Ubuntu 24.04 LTS, reachable on your own domain through Nginx with a TLS certificate. The same steps work on Ubuntu 22.04 LTS.
Keystone has changed a great deal since guides were written for Ubuntu 16.04. Those used the yo keystone generator and MongoDB, which belonged to Keystone 4. The current product, Keystone 6, is a TypeScript headless CMS published as @keystone-6/* packages. It generates a GraphQL API and an Admin UI from your schema, and stores data through Prisma in PostgreSQL, MySQL or SQLite. MongoDB is not supported. Ubuntu 16.04 itself is long past its standard support period.
Requirements
- An Ubuntu 24.04 or 22.04 server with a non-root user that has
sudorights. This guide usesdeploy. - A domain name with an A record (and AAAA record, if you use IPv6) pointing at the server. The examples use
example.com. - Ports 80 and 443 reachable from the internet.
Install Node.js
Keystone is written for the Active and Maintenance LTS releases of Node.js. The NodeSource repository provides current LTS packages for Ubuntu:
sudo apt update
sudo apt install -y curl ca-certificates
curl -fsSL https://deb.nodesource.com/setup_lts.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs
Check that both node and npm are available:
node --version
npm --version
Create the Keystone project
Run the project generator as deploy in your home directory. It asks for a project name; this guide uses website.
cd ~
npm create keystone-app@latest
cd website
The generator installs the dependencies and writes a small starter project: keystone.ts is the main configuration, schema.ts defines your lists (content types) and auth.ts configures login and sessions. The starter uses a SQLite database file, keystone.db, in the project directory.
Start it once in development mode
Sessions are encrypted with a secret of at least 32 characters, read from the SESSION_SECRET environment variable. Set one for this shell and start the development server:
export SESSION_SECRET="$(openssl rand -hex 32)"
npm run dev
Development mode creates the database tables from your schema. On the first start the starter project also creates an initial user and prints its e-mail address and a random password in the terminal. Copy them.
The server listens on port 3000. Do not open that port to the internet; forward it through SSH from your own computer instead:
ssh -L 3000:localhost:3000 deploy@203.0.113.10
Open http://localhost:3000 in your browser, sign in with the printed credentials, then create your own user with a strong password and delete the generated one. Stop the server with Ctrl+C.
The code that creates the initial user sits in the onConnect function in keystone.ts and is marked as not intended for production. Once your own user exists, remove that function from the file.
Build and run in production mode
Production mode serves a prebuilt Admin UI, so build the project first. Repeat this after every code or schema change:
cd ~/website
npm run build
Store the environment for the service in a file only root can read. With NODE_ENV=production, Keystone marks its session cookie as secure, which means logging in only works over HTTPS; that is set up below.
sudo install -m 600 /dev/null /etc/keystone-website.env
echo "NODE_ENV=production" | sudo tee -a /etc/keystone-website.env > /dev/null
echo "SESSION_SECRET=$(openssl rand -hex 32)" | sudo tee -a /etc/keystone-website.env > /dev/null
Create the systemd unit:
sudo nano /etc/systemd/system/keystone-website.service
[Unit]
Description=Keystone website
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/website
EnvironmentFile=/etc/keystone-website.env
ExecStart=/usr/bin/npm run start
Restart=on-failure
[Install]
WantedBy=multi-user.target
Enable and start it, then confirm that it answers locally:
sudo systemctl daemon-reload
sudo systemctl enable --now keystone-website
sudo systemctl status keystone-website
curl -I http://127.0.0.1:3000
Configure Nginx as the reverse proxy
Install Nginx and create a site that passes every request to Keystone:
sudo apt install -y nginx
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
client_max_body_size 50m;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
client_max_body_size raises Nginx's small default upload limit so that file and image fields work; adjust it to the largest upload you expect. Enable the site, test the configuration and reload:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Firewall and HTTPS
Allow SSH and web traffic only. Port 3000 stays closed to the outside:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
Request a certificate with Certbot's Nginx plugin, which also rewrites the site to redirect HTTP to HTTPS:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
Verify
Open https://example.com. You should see the Keystone sign-in page and be able to log in with the user you created. If the page does not load, check the two services:
sudo journalctl -u keystone-website -n 50 --no-pager
sudo tail -n 50 /var/log/nginx/error.log
A 502 Bad Gateway from Nginx means Keystone is not running or not listening on port 3000; the journal shows why.
Maintenance: schema changes and the database
To deploy a change, update the code, rebuild and restart:
cd ~/website
npm install
npm run build
sudo systemctl restart keystone-website
Be aware of how the database schema is handled. keystone dev pushes schema changes to the database automatically, but keystone start never does. For production, Keystone's documentation has you create migration files with Prisma during development and apply them with npx prisma migrate deploy before starting the new build. See the database migration guide.
The SQLite file from the starter is convenient for trying Keystone, but the project advises against SQLite for production sites. For anything beyond a small or test site, install PostgreSQL or MySQL and switch the db section of keystone.ts to the postgresql or mysql provider as described in the configuration reference. Whichever you use, back up the database and any uploaded files regularly.
Frequently asked questions
Can I still use MongoDB with Keystone?
Not with Keystone 6. Its database layer supports PostgreSQL, MySQL and SQLite. Projects built on the older MongoDB-based releases have to be migrated, not upgraded in place, because the schema format and APIs are different.
Why does login fail when I open the site over plain HTTP?
In production mode the session cookie is flagged as secure, so browsers only send it over HTTPS. Use the HTTPS address. Browsers make an exception for localhost, which is why the SSH tunnel works without a certificate.
How do I run Keystone on a different port?
Set server: { port: 4000 } in the configuration object in keystone.ts, rebuild, and change the port in the Nginx proxy_pass line to match. The default is 3000.
Do I need a process manager such as PM2?
No. The systemd unit above already starts Keystone at boot, restarts it if it crashes and collects its output in the journal.