In this blog post, we would follow the steps to deploy a NodeJS app using the latest version of Node with nginx on Ubuntu.
Installing Node.js 5
The official repository on Ubuntu doesn’t ship the latest version of NodeJS yet. So we will use a third party source to install it from:
1 2 |
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash - sudo apt-get install -y nodejs |
Using PM2
I love PM2
for keeping my node apps alive. If you didn’t know, PM2 is an awesome tool that launches Node processes and monitors them. If it crashes, it can restart them. PM2 is very easy to setup and use. It’s also quite feature packed.
We would setup PM2, launch our app with it and then generate a launch script so PM2 itself is started on system reboot.
1 2 3 |
npm install -g pm2 # Install PM2 pm2 start index.js # Start an app pm2 startup ubuntu # Create a launch script for PM2 |
Nginx Configuration
Now that the app is running, it’s time to setup nginx as our reverse proxy. Here’s the default configurations I use:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
server { listen 80; location / { proxy_pass http://localhost: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; } } |