|
| 1 | +# Self Hosting SpacetimeDB |
| 2 | + |
| 3 | +This tutorial will guide you through setting up SpacetimeDB on an Ubuntu 24.04 server, securing it with HTTPS using Nginx and Let's Encrypt, and configuring a systemd service to keep it running. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | +- A fresh Ubuntu 24.04 server (VM or cloud instance of your choice) |
| 7 | +- A domain name (e.g., `example.com`) |
| 8 | +- `sudo` privileges on the server |
| 9 | + |
| 10 | +## Step 1: Create a Dedicated User for SpacetimeDB |
| 11 | +For security purposes, create a dedicated `spacetimedb` user to run SpacetimeDB: |
| 12 | + |
| 13 | +```sh |
| 14 | +sudo mkdir /stdb |
| 15 | +sudo useradd --system spacetimedb |
| 16 | +sudo chown -R spacetimedb:spacetimedb /stdb |
| 17 | +``` |
| 18 | + |
| 19 | +Install SpacetimeDB as the new user: |
| 20 | + |
| 21 | +```sh |
| 22 | +sudo -u spacetimedb bash -c 'curl -sSf https://install.spacetimedb.com | sh -s -- --root-dir /stdb --yes' |
| 23 | +``` |
| 24 | + |
| 25 | +## Step 2: Create a Systemd Service for SpacetimeDB |
| 26 | +To ensure SpacetimeDB runs on startup, create a systemd service file: |
| 27 | + |
| 28 | +```sh |
| 29 | +sudo nano /etc/systemd/system/spacetimedb.service |
| 30 | +``` |
| 31 | + |
| 32 | +Add the following content: |
| 33 | + |
| 34 | +```ini |
| 35 | +[Unit] |
| 36 | +Description=SpacetimeDB Server |
| 37 | +After=network.target |
| 38 | + |
| 39 | +[Service] |
| 40 | +ExecStart=/stdb/spacetime --root-dir=/stdb start --listen-addr='127.0.0.1:3000' |
| 41 | +Restart=always |
| 42 | +User=spacetimedb |
| 43 | +WorkingDirectory=/stdb |
| 44 | + |
| 45 | +[Install] |
| 46 | +WantedBy=multi-user.target |
| 47 | +``` |
| 48 | + |
| 49 | +Enable and start the service: |
| 50 | + |
| 51 | +```sh |
| 52 | +sudo systemctl enable spacetimedb |
| 53 | +sudo systemctl start spacetimedb |
| 54 | +``` |
| 55 | + |
| 56 | +Check the status: |
| 57 | + |
| 58 | +```sh |
| 59 | +sudo systemctl status spacetimedb |
| 60 | +``` |
| 61 | + |
| 62 | +## Step 3: Install and Configure Nginx |
| 63 | + |
| 64 | +### Install Nginx |
| 65 | + |
| 66 | +```sh |
| 67 | +sudo apt update |
| 68 | +sudo apt install nginx -y |
| 69 | +``` |
| 70 | + |
| 71 | +### Configure Nginx Reverse Proxy |
| 72 | +Create a new Nginx configuration file: |
| 73 | + |
| 74 | +```sh |
| 75 | +sudo nano /etc/nginx/sites-available/spacetimedb |
| 76 | +``` |
| 77 | + |
| 78 | +Add the following configuration, remember to change `example.com` to your own domain: |
| 79 | + |
| 80 | +```nginx |
| 81 | +server { |
| 82 | + listen 80; |
| 83 | + server_name example.com; |
| 84 | +
|
| 85 | + location / { |
| 86 | + proxy_pass http://localhost:3000; |
| 87 | + proxy_http_version 1.1; |
| 88 | + proxy_set_header Upgrade $http_upgrade; |
| 89 | + proxy_set_header Connection "Upgrade"; |
| 90 | + proxy_set_header Host $host; |
| 91 | + } |
| 92 | +
|
| 93 | + # This restricts who can publish new databases to your SpacetimeDB instance. We recommend |
| 94 | + # restricting this ability to local connections. |
| 95 | + location /v1/publish { |
| 96 | + allow 127.0.0.1; |
| 97 | + deny all; |
| 98 | + proxy_pass http://localhost:3000; |
| 99 | + proxy_http_version 1.1; |
| 100 | + proxy_set_header Upgrade $http_upgrade; |
| 101 | + proxy_set_header Connection "Upgrade"; |
| 102 | + proxy_set_header Host $host; |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +This configuration contains a restriction to the `/v1/publish` route. This restriction makes it so that you can only publish to the database if you're publishing from a local connection on the host. |
| 108 | + |
| 109 | +Enable the configuration: |
| 110 | + |
| 111 | +```sh |
| 112 | +sudo ln -s /etc/nginx/sites-available/spacetimedb /etc/nginx/sites-enabled/ |
| 113 | +``` |
| 114 | + |
| 115 | +Restart Nginx: |
| 116 | + |
| 117 | +```sh |
| 118 | +sudo systemctl restart nginx |
| 119 | +``` |
| 120 | + |
| 121 | +### Configure Firewall |
| 122 | +Ensure your firewall allows HTTPS traffic: |
| 123 | + |
| 124 | +```sh |
| 125 | +sudo ufw allow 'Nginx Full' |
| 126 | +sudo ufw reload |
| 127 | +``` |
| 128 | + |
| 129 | +## Step 4: Secure with Let's Encrypt |
| 130 | + |
| 131 | +### Install Certbot |
| 132 | + |
| 133 | +```sh |
| 134 | +sudo apt install certbot python3-certbot-nginx -y |
| 135 | +``` |
| 136 | + |
| 137 | +### Obtain an SSL Certificate |
| 138 | + |
| 139 | +Run this command to request a new SSL cert from Let's Encrypt. Remember to replace `example.com` with your own domain: |
| 140 | + |
| 141 | +```sh |
| 142 | +sudo certbot --nginx -d example.com |
| 143 | +``` |
| 144 | + |
| 145 | +Certbot will automatically configure SSL for Nginx. Restart Nginx to apply changes: |
| 146 | + |
| 147 | +```sh |
| 148 | +sudo systemctl restart nginx |
| 149 | +``` |
| 150 | + |
| 151 | +### Auto-Renew SSL Certificates |
| 152 | +Certbot automatically installs a renewal timer. Verify that it is active: |
| 153 | + |
| 154 | +```sh |
| 155 | +sudo systemctl status certbot.timer |
| 156 | +``` |
| 157 | + |
| 158 | +## Step 5: Verify Installation |
| 159 | + |
| 160 | +On your local machine, add this new server to your CLI config. Make sure to replace `example.com` with your own domain: |
| 161 | + |
| 162 | +```bash |
| 163 | +spacetime server add self-hosted --url https://example.com |
| 164 | +``` |
| 165 | + |
| 166 | +If you have uncommented the `/v1/publish` restriction in Step 3 then you won't be able to publish to this instance unless you copy your module to the host first and then publish. We recommend something like this: |
| 167 | + |
| 168 | +```bash |
| 169 | +spacetime build |
| 170 | +scp target/wasm32-unknown-unknown/release/spacetime_module.wasm ubuntu@<host>:/home/ubuntu/ |
| 171 | +ssh ubuntu@<host> spacetime publish -s local --bin-path spacetime_module.wasm <module-name> |
| 172 | +``` |
| 173 | + |
| 174 | +You could put the above commands into a shell script to make publishing to your server easier and faster. It's also possible to integrate a script like this into Github Actions to publish on some event (like a PR merging into master). |
| 175 | + |
| 176 | +## Step 6: Updating SpacetimeDB Version |
| 177 | +To update SpacetimeDB to the latest version, first stop the service: |
| 178 | + |
| 179 | +```sh |
| 180 | +sudo systemctl stop spacetimedb |
| 181 | +``` |
| 182 | + |
| 183 | +Then upgrade SpacetimeDB: |
| 184 | + |
| 185 | +```sh |
| 186 | +sudo -u spacetimedb -i -- spacetime --root-dir=/stdb version upgrade |
| 187 | +``` |
| 188 | + |
| 189 | +To install a specific version, use: |
| 190 | + |
| 191 | +```sh |
| 192 | +sudo -u spacetimedb -i -- spacetime --root-dir=/stdb install <version-number> |
| 193 | +``` |
| 194 | + |
| 195 | +Finally, restart the service: |
| 196 | + |
| 197 | +```sh |
| 198 | +sudo systemctl start spacetimedb |
| 199 | +``` |
| 200 | + |
| 201 | +## Step 7: Troubleshooting |
| 202 | + |
| 203 | +### SpacetimeDB Service Fails to Start |
| 204 | +Check the logs for errors: |
| 205 | + |
| 206 | +```sh |
| 207 | +sudo journalctl -u spacetimedb --no-pager | tail -20 |
| 208 | +``` |
| 209 | + |
| 210 | +Verify that the `spacetimedb` user has the correct permissions: |
| 211 | + |
| 212 | +```sh |
| 213 | +sudo ls -lah /stdb/spacetime |
| 214 | +``` |
| 215 | + |
| 216 | +If needed, add the executable permission: |
| 217 | + |
| 218 | +```sh |
| 219 | +sudo chmod +x /stdb/spacetime |
| 220 | +``` |
| 221 | + |
| 222 | +### Let's Encrypt Certificate Renewal Issues |
| 223 | +Manually renew the certificate and check for errors: |
| 224 | + |
| 225 | +```sh |
| 226 | +sudo certbot renew --dry-run |
| 227 | +``` |
| 228 | + |
| 229 | +### Nginx Fails to Start |
| 230 | +Test the configuration: |
| 231 | + |
| 232 | +```sh |
| 233 | +sudo nginx -t |
| 234 | +``` |
| 235 | + |
| 236 | +If errors are found, check the logs: |
| 237 | + |
| 238 | +```sh |
| 239 | +sudo journalctl -u nginx --no-pager | tail -20 |
| 240 | +``` |
0 commit comments