Use DigitalOceans Ubuntu Node image.
sudo apt-get update
sudo apt-get install nginx
sudo service nginx start
update-rc.d nginx defaults
If you receive the following message, it means nginx is already setup to start after server reboot:
System start/stop links for /etc/init.d/nginx already exist.
Replace example.com
where appropriate.
nano /etc/nginx/conf.d/example.com.conf
Inside example.com.conf
add the following:
upstream app_example {
server 127.0.0.1:{PORT};
keepalive 8;
}
# Remove WWW
server {
listen 0.0.0.0:80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 0.0.0.0:80;
server_name example.com example;
access_log /var/log/nginx/example.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://app_example/;
proxy_redirect off;
}
}
Replace {PORT}
with the port number the app is running on.
Reboot nginx
sudo /etc/init.d/nginx restart
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
sudo npm install pm2 -g
Depending on how the app is setup (look in package.json to find out) it is worth running npm install
or npm start
and performing an initial test run before setting up the following.
cd /path/to/node/app/
pm2 start app.js