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?
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 usesystemd
to run your app in the background.I am assuming you are running your
pocketbase
at http://localhost:8001 and yoursveltekit
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 bynginx
andcertbot
.Install
nginx
reading the documentation for your distro. Some distros enable thenginx
systemd service automatically, if yours does not, enablenginx
withThe config file will be found at
/etc/nginx/nginx.conf
.Then add
server
underhttp
in thenginx
config for both backend and the frontend. The config will look somewhat like this.Reload
nginx
.nginx
looks at the http headers and matches it with theserver_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
andcertbot-nginx
for your distro. The package names could vary.Point the domains of
backend.mydomain.com
andappname.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, runcertbot
.and follow the instructions on screen.
certbot
will get the required certificates from letsencrypt, modify thenginx
configuration to upgrade all requests to https.Reload
nginx
again.And congrats, you have deployed your sveltekit / pocketbase apps in a production ready way.