0

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.

1 Answer 1

2

I'll be willing to accept this is a dumb question, but is $HOME/sites/croockys.ch actually a directory? .ch looks like it should be a file extension, not the actual directory which to me looks like it should be in $HOME/sites. also keep in mind that the user running this is likely not going to be the same dev user that you are creating this - probably better to expand $HOME to what it really should be as a full path: /opt/home/place/where/code/site or whatever the path needs to be in order to start the process correctly.

1
  • 1
    the thing is the hosting provider creates the directory croockys.ch by itself but replacing $HOME with the full path worked for me!
    – Jolan
    Commented Jul 6, 2022 at 13:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .