I currently have a server, which contains a NextJS project, running on port 4000.
The scripts
part of the the package.json file is as follows:
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start -p 4000"
},
When I run npm run start
in the SSH session, the project works fine but naturally I'll have to run Node as a service so that the project keeps on working after I close the SSH session.
I followed the official documentation of the hosting provider which gives the following example for a unit file
[Unit]
Description={Service name} # Enter the service name here. This is mandatory but has no impact on function
[Service]
Restart=always
Environment=NODE_VERSION={the desired version} # Specify the Node version to be used here. Make sure it's installed beforehand with "nvm install {the desired version}"
WorkingDirectory=%h/
ExecStart=/bin/bash -c "exec $HOME/.nvm/nvm-exec {command to launch the node script}" # This command depends on the project. For example, "npm run start", "npm run serve" or even "node server.js" are running
[Install]
WantedBy=default.target
I ended with a node.service
file with the following content:
[Unit]
Description=node
[Service]
Restart=always
Environment=NODE_VERSION=16.15.1 NODE_ENV=production PORT=4000
WorkingDirectory=$HOME/sites/croockys.ch
ExecStart=/bin/bash -c "exec $HOME/.nvm/nvm-exec npm run start"
[Install]
WantedBy=default.target
My project is found in $HOME/sites/croockys.ch
I ran the following commands in order:
systemctl --user daemon-reload
systemctl --user enable node
systemctl --user start node
and did not encounter any errors but the website does not work and I get a 503 Service Unavailable
.
I guess there is something wrong in my unit file.