Application server with Node.js, PM2 and Nginx on Linux Ubuntu 16.04 Lts

by Mecbar 13 luglio 2017


For install NodeJs from Terminal insert :
sudo apt-get install
sudo apt-get install build-essential
If our application is ready install the process manager pm2 with the command:
sudo npm install -g pm2
and then start the aplication from Terminal
pm2 start name_app.js #from the directory of the app
verify if the app is in execution in the browser insert the link localhost:5500 (port assigned into the app)


If the app is running set the automatic start of Pm2 at the ipl of the machine:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u root --hp /home/root
Now setup the Http Server Nginx hw reverse proxy opening the following file with an editor, here use Nano: seguente con l'editor nano:
sudo nano /etc/nginx/sites-available/default

insert the following text:
location / {
proxy_pass http://localhost:5500;
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;
}
}
if you want insert an other application add the following instruction: location / {}
when all test is written save the file with command ctrl+o and exit with ctrl+x
Now for verifycation the correct operation shutdown the machine and restart it.



pm2 status

For verification correct automatic start of the process manager pm2 that runs the app insert from Terminal the command pm2 show name_app or pm2 show number_app
We see that the app run and it's active. insert from Terminal :


sudo pm2 show 0

pm2 process attivi

We see that the app run and it's active. From the browser insert the link http://localhost:5500 we can open our app and it's work.



app pronta ad uso