0

I am trying to continuously run a nodeJs server on my VPS, I made a systemdservice and started it.

I worked by my server is still not accessible, I can check the status and it works Okey :

Service Status :

Node.service - Runs the Node Server for Node API
   Loaded: loaded (/etc/systemd/system/Node.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-05-12 16:16:38 CEST; 2min 37s ago
 Main PID: 26697 (node)
    Tasks: 10
   Memory: 87.5M
      CPU: 2.734s
   CGroup: /system.slice/Node.service
           └─26697 /root/.nvm/versions/node/v9.11.1/bin/node www

May 12 16:16:38 vps543107 systemd[1]: Started Runs the Node Server for Node API.
May 12 16:16:41 vps543107 node[26697]: Listening on port 3000

Service :

[Unit]
Description=Runs the Node Server for Node API
Documentation="No Docs"
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/Node/bin
ExecStart=/root/.nvm/versions/node/v9.11.1/bin/node www
Restart=on-failure

[Install]
WantedBy=multi-user.target

I can't get to the server via any HTTP request, all requests timeout.

Is there a problem with my Service.

2 Answers2

1

Try following

[Unit]
Description="Runs the Node Server for Node API"
#Requires=After=mysql.service       # If you have any dependency then add it

[Service]
ExecStart=/root/.nvm/versions/node/v9.11.1/bin/node /root/Node/bin/www
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10

# Output to syslog
StandardOutput=syslog
StandardError=syslog
#Change this to find app logs in /var/log/syslog
SyslogIdentifier=nodejs-api
# Followig will require if you are using the PORT or Node from Envirnoment
Environment=NODE_ENV=production PORT=3000

[Install]
WantedBy=multi-user.target

Once your server machine is up & you are not able to access server, you troubleshoot by checking logs from /var/log/syslog by the following command

sudo cat /var/log/syslog | grep -r "nodejs-api"

Start on boot: sudo systemctl enable rocketchat

0

Start your app with After=network-online.target to make sure the network is fully up before you start it.

You mention that the app is not not available over "HTTP". Did you intent to run the app on port 3000 on your server port 80, the default HTTP port? Are you testing it on the same port you are running it on?

Running your app with User=root is a security risk. A security flaw in your web app could directly lead to the full compromise of your VPS. Consider running your app as an unprivileged user to mitigate that risk.

If you the app to start at boot, make sure you have used systemctl enable your-app to enable it to start at boot.

Also, I see you are using NVM. It appears to be working based on your service status, but is not my recommendation for production services. It depends on environment variables, which makes it more fragile.

I recommend using the n package manager instead for production instead, which uses only symlinks to switch versions, and no environment variables.

Also see the FAQ answer for why things don't work under system when they start manually.