I have a svelte kit project. I want to deploy the app in an Nginx web server after an npm run build
. At the moment I have a node container and I use to start using npm run preview
. It’s working fine, but I want to deploy in a production environment using build
.
How could I do that?
ref: https://kit.svelte.dev/docs#command-line-interface-svelte-kit-build
3
Answers
If you have a static website (ie no endpoints) you should use
@sveltejs/adapter-static@next
. It will put the files you should serve in/build
directory. You can then serve the generated pages using NGINX. A sample NGINX config would be:If your site is not static you should use
@sveltejs/adapter-node
and run that in your container. You could put NGINX in front of it to use its features (SSL, load balancing, firewall, etc). After building your site (usingnpm run build
) you can runnode ./build/index.js
.Alternatively, you could use Netlify, Vercel, or Cloudflare Pages to host you site.
To see how to change your adapter see the docs.
Good luck!
As @Caleb Irwin said, you can run
node ./build/index.js
The NGINX configuration will look like this:
(I’m not a NGINX pro and welcomes feedback to improve on it)
You may also want to make the SvelteKit app listen only to localhost by adding the environment
HOST=127.0.0.1
before runningnode build/index.js
. This will prevent the port 3000 from being reached from the outside.You can also look into using pm2 to manage the sveltekit process, including running it on each of your cores in cluster mode, automatic restart in case of server crash / reboot.
I’ve managed to deploy a Svelte Kit app to my Google Cloud Engine virtual machine and serve it using Nginx. I’ve still got some outstanding questions myself, but so far these are my steps:
$ npm run build
gcloud compute scp --recurse build/ user@gcpinstance:~/Desktop
gcloud compute scp package*.* user@gcpinstance:~/Desktop
npm install
. That re-created the node-modules folder (otherwise it takes forever to upload the node-modules folder from the local machine).mkdir SvelteKitProd/
mv package*.* build/ node-modules/ SvelteKitProd/
sudo chown -R root:root SvelteKitProd/
mv SvelteKitProd/ /var/www/domainname/
9 ~/Desktop$
cd /var/www/domainname/
sudo vi /etc/nginx/sites-available/domainname
(this is my nginx configuration for this domain and this app).pm2 start SvelteKitProd/build/index.js
I’m still trying to figure out what all I need to do in order to serve multiple apps each from its own top-level domain. I was hoping that I could change the PORT once built and uploaded (see the build/index.js file), but so far that isn’t working for me. So I’ll try specifying a unique port number while building it locally or messing with it once uploaded to the remote server; or perhaps use PM2 and Nginx to make multiple apps work on the same port, e.g. 3000.