Help start a NodeJs project after server restart?

Posted February 27, 2020 1.7k views

I am new to Nginx and Ubuntu 16.04.
I had an Nginx website up at with a NodeJS script. It uses a local MongoDB and PM2. I made some hard code changes on my home page and after I refreshed the website in the browser, the changes did not update. I restarted my server and now I get a 502 Bad Gateway error.

I have got the PM2 restarted.
MongoDB is restarted

I use Nginx, NVM, NodeJs, MongoDB, PM2.

My nginx.conf server settings:

user www-data;
worker_processes auto;
pid /run/;

events {
    worker_connections 768;
    # multi_accept on;

http {

      server {
   listen         80 default_server;
   listen         [::]:80 default_server;
   server_name    localhost;
   root           /usr/share/nginx/html;
   location / {
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host $host;
       proxy_cache_bypass $http_upgrade;

    # Basic Settings

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # SSL Settings

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    # Logging Settings

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # Gzip Settings

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # Virtual Host Configs

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

Question 1) Why am I receiving a 502 error?
Question 2) How do I prevent this next time I make changes in my files?
Question 3) Is there a script anyone recommends that can restart my nodejs app when I make changes.

edited by MattIPv4
1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Sorry, it never forwarded past the “posting screen” on my account. I gave up and figured it would never post.


I just came across this question.

As a reference for anyone who comes across this in the future, in order to start your Node.js process with PM2 after a server restart, you need to do the following:

  • This subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots:
pm2 startup systemd
  • Start the service with systemctl:
sudo systemctl start pm2-sammy

If at this point you encounter an error, you may need to reboot, which you can achieve with sudo reboot.

Check the status of the systemd unit:

systemctl status pm2-sammy

For more information, you can check out this tutorial here:


by Lisa Tagliaferri
by Kathleen Juell
Node.js is an open-source JavaScript runtime environment for building server-side and networking applications. The platform runs on Linux, macOS, FreeBSD, and Windows. Though you can run Node.js applications at the command line, this tutorial will…