Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 16, 2022 08:27 pm GMT

Host SvelteKit apps with SSR-support via ploi.io (on Hetzner Cloud)

SvelteKit 1.0 was officially released a few hours ago , time to dive into NodeJS and SvelteKit 1.0 hosting.

One key advantage of SvelteKit is that you can generate static sites as well as take advantage of Server Side Rendering (SSR).

In order to use SSR mode, one option is to use the awesome server management service ploi.io.

Ploi enables you to deploy NodeJS apps to european cloud server providers like Hetzner (which is good for GDPR-compliance). Ploi is like a nice graphical user interface for cloud hosting providers.

So let's go!

Steps I won't cover in this tutorial:

First we need to create a new site on your server:

Screenshot ploi site dashboard, button create site is highlighted

Expand the advanced settings and use the following:

Screenshot ploi site dashboard, form for site generation with web directory to slash, project directory to slash, project type nodejs

(Set the project directory to `/, otherwise you won't be able to request an SSL cert in the next steps)

Afterwards you can install your project from a git repository:

Screenshot ploi site dashboard with install git button

I added my GitHub account and used my demo repo sveltekit-company-demo:

Image description

After adding a repository you can edit the deploy script of your new site:

Screenshot deploy script

Replace it with the following, we will need npm run build in there:

`
cd /home/ploi/sveltekit-company-demo.mandrasch.dev
git pull origin main

npm install
npm run build

echo " Application deployed!"
`

We will also need a SSL certificate. Thanks to Lets's Encrypt this is easily done nowadays:

Screenshot ploi request ssl certificate successful

Now we need to make adjustments to the NodeJS settings, ploi will use pm2 to host our SvelteKit app.

Change the NodeJS settings to the following:

Screenshot ploi node js settings with PORT=3001 node build/index.js and port 3001 selected

Important: If you create your first site, it will run on port 3000. SvelteKits node-adapter assumes port 3000 by default. But if you add another site on a server, ploi will use another port like 3001, 3002, etc. for each new project.

You'll need to set the env variable for the specific port correctly:

PORT=3001 node build/index.js

Thanks very much to aarondiel!

In order to run your SvelteKit Web App with NodeJS, you'll also need to apply the changes to your projects source code.

Run

npm i -D @sveltejs/adapter-node

and replace in svelte.config.js

import adapter from '@sveltejs/adapter-auto';

with

import adapter from '@sveltejs/adapter-node';

See SvelteKits official docs for all information:
https://kit.svelte.dev/docs/adapters#supported-environments-node-js

Commit and push these changes.

It is time to deploy our app:

Screenshot deploy now button

You can check the status in the deployments section below:

Screenshot deployments section

Our app is now successfully prepared in the build/ directory on the server, but we still start our app via the pm2 service.

Hit "Spawn" in the NodeJS settings dashboard:

Screenshot before spawn

You should see a brief success message and the buttons should change as following:

Screenshot successful span

Congrats, your SvelteKit app is now live!

Screenshot live site

Have I missed something or can this be optimized? How do you host SvelteKit apps? Let me know in the comments, much appreciated!

Further resources

Reboot configuration

If this is your first time using pm2 on your Hetzner server, you need to add pm2 to the $PATH for server reboots. Follow the ploi.io-guide How to make pm2 start on boot.

In my case I logged in to the Hetzner server ssh [email protected] via terminal / SSH. I previously added my laptops SSH key to ploi, see https://ploi.io/documentation/ssh for more information.

Screenshot of ssh connection

I ran the command pm2 startup and pasted the suggested command (sudo env PATH=$PATH ...):

Screenshot terminal command from guide one

Screenshot terminal command from guide one

The root server password was retrieved via mail after the initial server completion (https://ploi.io/documentation/ssh/how-do-i-login-as-root-user):
Screenshot mail ploi

Troubleshooting

You can check the NodeJS error log for information if something failed. In my case I wasn't aware I need to set the PORT=3001 if I use another port than 300.

Screenshot error logs

Investigating errors is also possible via SSH and pm2 list or pm2 logs command:

Screenshot pm2 list
See pm2 quickstart docs for more information.

What is pm2?

"PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks." (pm2)

Other similiar tools are nodemon and supervisor.

What is SSR?

SvelteKit hosting and GDPR


Original Link: https://dev.to/mandrasch/host-sveltekit-apps-with-ssr-support-via-ploiio-on-hetzner-cloud-1cpa

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To