Deploy node.js using pm2, nginx & certbot

Node.js is a powerful server side JavaScript framework that has been used by a large number projects as a back-end technology. As the uses of this framework soars, developers face difficulties to deploy node applications. PM2 is a pretty neat choice for running a development server. Because, as you make changes to your codebase it will dynamically show you the results in your browser.

Installing PM2 globally

We need to install pm2 globally. Before you run the command, make sure your system has node.js installed. You can check the version of node installed using node --version

sudo npm install -g pm2

Installing nginx and enabling firewall

nginx is well known to handle a large number of traffic and we’ll use certbot to deploy ssl to our deployment. To install nginx —

sudo apt install nginx

To protect your server from unwanted attacks it is recommended to use a firewall and open only specific ports. We’ll use ufw here, as it comes preinstalled in most of the systems. If you opt-in for a minimal Ubuntu installation, you may need to install it

sudo apt install ufw

We need to configure ufw. To check current status of ufw run the command:

sudo ufw status

It should return — Status active or Status inactive. If it is inactive, enable the firewall using:

sudo ufw enable

Now we need to allow some essential ports like ssh 22, http 80, https 443 to serve the node application through nginx.

sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https

Running node application using PM2

Navigate to the node.js project directory and run the following commands;

sudo npm install

Running node app using pm2

sudo pm2 start npm --name "app name" -- start

The ”app name” represents the process name in pm2. So you can assign it whatever you want. To check the running process in pm2 you can run

sudo pm2 ls

You might want to autostart pm2 with the process just in case a system failure:

sudo pm2 startup
sudo pm2 save

Configure nginx to serve pm2 process as reverse proxy

We’ll edit the default nginx config which is located at /etc/nginx/sites-availble/default using nano editor.

sudo nano /etc/nginx/sites-availble/default

Paste the following to the location part of the server block. Assuming that the node app in pm2 is running on port 5000, if it runs on a different port, you should change the proxy pass port.

server_name example.com www.example.com;

location / {
  proxy_pass http://localhost:5000;
  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;
}

Save the config using Ctrl + X, followed by Y then Enter.

Restart the nginx web server;

sudo service nginx restart

Issue a free ssl certificate using certbot

According to the official documentation, you should install and use snap version of certbot.

Update snapd repository:

sudo snap install core; sudo snap refresh core && sudo snap install --classic certbot

make certbot explicitly executable

sudo ln -s /snap/bin/certbot /usr/bin/certbot

We’re almost done, now for issuing a ssl you can run the command

sudo certbot certonly --nginx

It’ll ask you to choose domain, enter email address, accept TOS etc. Certbot will automatically deploy the ssl certificate to your nginx configuration file.

Conclusion

Your node.js app is now deployed with a free ssl certificate.

Leave a Reply

Your email address will not be published. Required fields are marked *