Question

Help start a NodeJs project after server restart?

I am new to Nginx and Ubuntu 16.04. I had an Nginx website up at armadasol.com 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/nginx.pid;

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_pass http://127.0.0.1:3000;
       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.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello,

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:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04

Best, Bobby

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