|
| 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 | + ######################################### |
| 86 | + # By default SpacetimeDB is completely open so that anyone can publish to it. If you want to block |
| 87 | + # users from creating new databases you should keep this section commented out. Otherwise, if you |
| 88 | + # want to open it up (probably for dev environments) then you can uncomment this section and then |
| 89 | + # also comment out the location / section below. |
| 90 | + ######################################### |
| 91 | + # location / { |
| 92 | + # proxy_pass http://localhost:3000; |
| 93 | + # proxy_http_version 1.1; |
| 94 | + # proxy_set_header Upgrade $http_upgrade; |
| 95 | + # proxy_set_header Connection "Upgrade"; |
| 96 | + # proxy_set_header Host $host; |
| 97 | + # } |
| 98 | +
|
| 99 | + # Anyone can subscribe to any database. |
| 100 | + # Note: This is the only section *required* for the websocket to function properly. Clients will |
| 101 | + # be able to create identities, call reducers, and subscribe to tables through this websocket. |
| 102 | + location ~ ^/v1/database/[^/]+/subscribe$ { |
| 103 | + proxy_pass http://localhost:3000; |
| 104 | + proxy_http_version 1.1; |
| 105 | + proxy_set_header Upgrade $http_upgrade; |
| 106 | + proxy_set_header Connection "Upgrade"; |
| 107 | + proxy_set_header Host $host; |
| 108 | + } |
| 109 | +
|
| 110 | + # Uncomment this section to allow all HTTP reducer calls |
| 111 | + # location ~ ^/v1/[^/]+/call/[^/]+$ { |
| 112 | + # proxy_pass http://localhost:3000; |
| 113 | + # proxy_http_version 1.1; |
| 114 | + # proxy_set_header Upgrade $http_upgrade; |
| 115 | + # proxy_set_header Connection "Upgrade"; |
| 116 | + # proxy_set_header Host $host; |
| 117 | + # } |
| 118 | +
|
| 119 | + # Uncomment this section to allow all HTTP sql requests |
| 120 | + # location ~ ^/v1/[^/]+/sql$ { |
| 121 | + # proxy_pass http://localhost:3000; |
| 122 | + # proxy_http_version 1.1; |
| 123 | + # proxy_set_header Upgrade $http_upgrade; |
| 124 | + # proxy_set_header Connection "Upgrade"; |
| 125 | + # proxy_set_header Host $host; |
| 126 | + # } |
| 127 | +
|
| 128 | + # NOTE: This is required for the typescript sdk to function, it is optional |
| 129 | + # for the rust and the C# SDKs. |
| 130 | + location /v1/identity { |
| 131 | + proxy_pass http://localhost:3000; |
| 132 | + proxy_http_version 1.1; |
| 133 | + proxy_set_header Upgrade $http_upgrade; |
| 134 | + proxy_set_header Connection "Upgrade"; |
| 135 | + proxy_set_header Host $host; |
| 136 | + } |
| 137 | +
|
| 138 | + # Block all other routes explicitly. Only localhost can use these routes. If you want to open your |
| 139 | + # server up so that anyone can publish to it you should comment this section out. |
| 140 | + location / { |
| 141 | + allow 127.0.0.1; |
| 142 | + deny all; |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +This configuration by default blocks all connections other than `/v1/identity` and `/v1/database/<database-name>/subscribe` which only allows the most basic functionality. This will prevent all remote users from publishing to your SpacetimeDB instance. |
| 148 | + |
| 149 | +Enable the configuration: |
| 150 | + |
| 151 | +```sh |
| 152 | +sudo ln -s /etc/nginx/sites-available/spacetimedb /etc/nginx/sites-enabled/ |
| 153 | +``` |
| 154 | + |
| 155 | +Restart Nginx: |
| 156 | + |
| 157 | +```sh |
| 158 | +sudo systemctl restart nginx |
| 159 | +``` |
| 160 | + |
| 161 | +### Configure Firewall |
| 162 | +Ensure your firewall allows HTTPS traffic: |
| 163 | + |
| 164 | +```sh |
| 165 | +sudo ufw allow 'Nginx Full' |
| 166 | +sudo ufw reload |
| 167 | +``` |
| 168 | + |
| 169 | +## Step 4: Secure with Let's Encrypt |
| 170 | + |
| 171 | +### Install Certbot |
| 172 | + |
| 173 | +```sh |
| 174 | +sudo apt install certbot python3-certbot-nginx -y |
| 175 | +``` |
| 176 | + |
| 177 | +### Obtain an SSL Certificate |
| 178 | + |
| 179 | +Run this command to request a new SSL cert from Let's Encrypt. Remember to replace `example.com` with your own domain: |
| 180 | + |
| 181 | +```sh |
| 182 | +sudo certbot --nginx -d example.com |
| 183 | +``` |
| 184 | + |
| 185 | +Certbot will automatically configure SSL for Nginx. Restart Nginx to apply changes: |
| 186 | + |
| 187 | +```sh |
| 188 | +sudo systemctl restart nginx |
| 189 | +``` |
| 190 | + |
| 191 | +### Auto-Renew SSL Certificates |
| 192 | +Certbot automatically installs a renewal timer. Verify that it is active: |
| 193 | + |
| 194 | +```sh |
| 195 | +sudo systemctl status certbot.timer |
| 196 | +``` |
| 197 | + |
| 198 | +## Step 5: Verify Installation |
| 199 | + |
| 200 | +On your local machine, add this new server to your CLI config. Make sure to replace `example.com` with your own domain: |
| 201 | + |
| 202 | +```bash |
| 203 | +spacetime server add self-hosted --url https://example.com |
| 204 | +``` |
| 205 | + |
| 206 | +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: |
| 207 | + |
| 208 | +```bash |
| 209 | +spacetime build |
| 210 | +scp target/wasm32-unknown-unknown/release/spacetime_module.wasm ubuntu@<host>:/home/ubuntu/ |
| 211 | +ssh ubuntu@<host> spacetime publish -s local --bin-path spacetime_module.wasm <database-name> |
| 212 | +``` |
| 213 | + |
| 214 | +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). |
| 215 | + |
| 216 | +## Step 6: Updating SpacetimeDB Version |
| 217 | +To update SpacetimeDB to the latest version, first stop the service: |
| 218 | + |
| 219 | +```sh |
| 220 | +sudo systemctl stop spacetimedb |
| 221 | +``` |
| 222 | + |
| 223 | +Then upgrade SpacetimeDB: |
| 224 | + |
| 225 | +```sh |
| 226 | +sudo -u spacetimedb -i -- spacetime --root-dir=/stdb version upgrade |
| 227 | +``` |
| 228 | + |
| 229 | +To install a specific version, use: |
| 230 | + |
| 231 | +```sh |
| 232 | +sudo -u spacetimedb -i -- spacetime --root-dir=/stdb install <version-number> |
| 233 | +``` |
| 234 | + |
| 235 | +Finally, restart the service: |
| 236 | + |
| 237 | +```sh |
| 238 | +sudo systemctl start spacetimedb |
| 239 | +``` |
| 240 | + |
| 241 | +## Step 7: Troubleshooting |
| 242 | + |
| 243 | +### SpacetimeDB Service Fails to Start |
| 244 | +Check the logs for errors: |
| 245 | + |
| 246 | +```sh |
| 247 | +sudo journalctl -u spacetimedb --no-pager | tail -20 |
| 248 | +``` |
| 249 | + |
| 250 | +Verify that the `spacetimedb` user has the correct permissions: |
| 251 | + |
| 252 | +```sh |
| 253 | +sudo ls -lah /stdb/spacetime |
| 254 | +``` |
| 255 | + |
| 256 | +If needed, add the executable permission: |
| 257 | + |
| 258 | +```sh |
| 259 | +sudo chmod +x /stdb/spacetime |
| 260 | +``` |
| 261 | + |
| 262 | +### Let's Encrypt Certificate Renewal Issues |
| 263 | +Manually renew the certificate and check for errors: |
| 264 | + |
| 265 | +```sh |
| 266 | +sudo certbot renew --dry-run |
| 267 | +``` |
| 268 | + |
| 269 | +### Nginx Fails to Start |
| 270 | +Test the configuration: |
| 271 | + |
| 272 | +```sh |
| 273 | +sudo nginx -t |
| 274 | +``` |
| 275 | + |
| 276 | +If errors are found, check the logs: |
| 277 | + |
| 278 | +```sh |
| 279 | +sudo journalctl -u nginx --no-pager | tail -20 |
| 280 | +``` |
0 commit comments