Deploy SvelteKit/Pocketbase app to Linode server

982 Views Asked by At

I have created sveltekit/pocketbase project that I would like to deploy to Linode to run under subdomains:

backend.mydomain.com for pocketbase appname.mydomain.com for sveltekit

First I deployed pocketbase and I'm running it with systemd service which is working fine. But there's problem when I want to deploy and run sveltekit app. I was following this article on how to run svelte app on linode using pm2 and caddy. But there's problem because 443 address is already in use. When I turn off the pocketbase.service I can run my svelte app.

Now I know I should probably use reverse proxy for pocketbase, but I'm new in this and a bit lost in all the information. Any tips on how could I achieve this?

1

There are 1 best solutions below

0
On

You are right, you need a reverse proxy. I'm familiar with nginx and so I will use that to answer your question. You are also right that you use systemd to run your app in the background.

I am assuming you are running your pocketbase at http://localhost:8001 and your sveltekit at http://localhost:8002. Now, all you have to do is reverse proxy http://backend.mydomain.com to http://localhost:8001 and http://appname.mydomain.com to http://localhost:8002. Optionally, force https only. Both can be accomplished by nginx and certbot.

Install nginx reading the documentation for your distro. Some distros enable the nginx systemd service automatically, if yours does not, enable nginx with

sudo systemctl enable --now nginx.service

The config file will be found at /etc/nginx/nginx.conf.

Then add server under http in the nginx config for both backend and the frontend. The config will look somewhat like this.

http {
  server {
    server_name backend.mydomain.com;
    location / {
      proxy_pass http://localhost:8001;
    }
  }
  server {
    server_name appname.mydomain.com;
    location / {
      proxy_pass http://localhost:8002;
    }
  }
}

Reload nginx.

sudo systemctl reload nginx

nginx looks at the http headers and matches it with the server_name and reverse proxies to the appropriate backends.

Now, there is only one problem. Your site serves everything under http, which is not secure. Any modern site should upgrade http request at port 80 to https at port 443. We are lucky, nginx can do this and you don't even have to manually get certificates.

LetsEncrypt is a non-profit organization, which gives ssl certificates for free and they have a easy to use bot which can automatically edit the nginx config files for you.

Install certbot and certbot-nginx for your distro. The package names could vary.

Point the domains of backend.mydomain.com and appname.mydomain.com to the IP address of your linode machine. Use a service like https://www.whatsmydns.net/ to check if DNS has propagated. Now, run certbot.

sudo certbot

and follow the instructions on screen. certbot will get the required certificates from letsencrypt, modify the nginx configuration to upgrade all requests to https.

Reload nginx again.

sudo systemctl reload nginx

And congrats, you have deployed your sveltekit / pocketbase apps in a production ready way.