Create a production server for a NodeJs wep app
by sij /
I have created a NodeJs wep app, which I wanted to host on a unmanaged server. This guide shows how I have done it on an ubuntu 18.10 lts server.
Environment variables for NodeJs
If the web app is on a production server, it should know that. Otherwise some installed npm packeges log extensively.
vi ~/.bashrc
...
# NODEJS ENVIRONMENT VARIABLES AT THE END OF THE FILE
export NODE_ENV=production
export PORT=8080
Handle the NodeJs app with pm2
Install pm2 globally:
sudo npm install pm2 -g
Start the NodeJs:
pm2 start app.js --name "My Web App"
Show an overview:
pm2 list
Make the app available by with nginx
Install nginx
sudo apt-get install nginx
Configure
sudo vi /etc/nginx/sites-available/default
default file content
server {
listen 80;
server_name my-node-app.ch;
location / {
proxy_pass http://127.0.0.1:8080;
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;
}
}
Activate the app
sudo servive nginx restart
And now the wep app should be reachable.