SYSTEM INFORMATION | |
---|---|
OS type and version | AlmaLinux 8.8 |
Webmin version | 2.101 |
Usermin version | 2.001 |
Virtualmin version | 7.7 Pro |
Theme version | 21.04 |
Package updates | All installed packages are up to date |
For the benefit of the community and my future self I am going to document one approach I have succesfully used for deploying a sveltekit (nodejs) application on virtualmin pro using the nodejs script installer as the starting point.
Sveltekit environment / dependencies
- adapter-node (Node servers • Docs • SvelteKit)
- Prisma ORM client for db access
I will assume you know how to install all this stuff locally in your dev environment and will also assume you can successfully run a local build process before deploying to your production server.
Production server deployment steps
Step 1 - Install node.js from the available install scripts in your virtual server
- At the time of writing that is node.js v. 20.6.0
- I chose the default install config which installs the binaries to public_html/nodejs
Step 2 - Copy source files to server (for production build)
You could optionally simply copy all your build files from your development machine to server. Due to some weird differences between environments I prefer to copy the source files to server and run build there and later remove the source files.
Directories to copy to public_html
- prisma
- src
- static
Omit the prisma schema directory if not using Prisma
Files to copy to public_html
- .env
- package.json
- package-lock.json
- svelte.config.js
- vite.config.js
Step 3 - Install dependencies
npm install
npx prisma generate
Step 4 - Build your application
npm run build
Step 5 - Update the host and port of the build/index.js startup script
Update the build/index.js file host and port to match node.js service created by virtualmin script install. Could not currently find an environmental variable to auto handle this at build time. Svelte defaults this to
const host = env('HOST', '0.0.0.0');
const port = env('PORT', !path && '3000');
Change it to the port of the service created by the virtualmin Node.js script install
e.g.
const host = env('HOST', '127.0.0.1');
const port = env('PORT', !path && '3001');
Step 6 - Create an application startup script for the node.js server
This could possibly be handled directly in the systemd service created by the install script but I chose to create a separate startup shell script.
Create a startup.sh file at the path /home/your-virtual-server-path/public_html/nodejs/startup.sh with the following contents.
#! /bin/bash
source ${HOME}/.bashrc
cd /home/your-virtual-server-path/public_html/nodejs
export NODE_ENV=production
ORIGIN=https://your-virtual-server.com node /home/your-virtual-server-path/public_html/build/index.js
Some additional notes on this startup script. The ORIGIN section takes care of the Cross-site POST form submissions are forbidden issue noted here Node servers • Docs • SvelteKit
Setting your startup script to include an origin will solve this issue for your public domain and a form posts error as noted above.
Step 7 - Modify node.js server systemd service to use the startup script created above
- Click the Service name from the Manage script screen in Virtualmin Pro
- Update the ExecStart line
ExecStart=/bin/bash /home/your-virtual-server-path/public_html/nodejs/startup.sh
Step 8 - Modify the Apache directives - startup script does not like the script installed defaults
Replace
<Proxy balancer://mongrel3001>
BalancerMember http://localhost:3001
</Proxy>
ProxyPass /nodejs balancer://mongrel3001
with
ProxyPass / http://127.0.0.1:3001/
ProxyPassReverse / http://127.0.0.1:3001/
click apply changes on both Website and Website SSL apache config after making the changes.