Node.js Deployment Guide to Vultr with PM2, NGINX, and LetsEncrypt SSL

node-deployment

1. Sign up for Vultr

Are you looking for a reliable VPS service? Try Vultr! With top-notch performance and a wide range of server locations, Vultr makes it easy to deploy your applications.

Get started now!

2. Create an Ubuntu Server on Vultr and Log In via SSH

Example: Ubuntu 22.04. I will be using the root user, but would suggest creating a new user.

3. Install Node.js and NPM

curl -sL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install nodejs
node --version
      

4. Clone Your Project from GitHub

There are a few ways to get your files onto the server. I suggest using Git:

git clone yourproject.git

5. Install Dependencies and Test the App

cd yourproject
npm install
npm start  
# Stop the app
ctrl+C
      

6. Setup PM2 Process Manager

sudo npm i pm2 -g
pm2 start app 

# Other pm2 commands
pm2 show app
pm2 status
pm2 restart app
pm2 stop app
pm2 logs  
pm2 flush  

# To make sure app starts when reboot
pm2 startup ubuntu
      

You should now be able to access your app using your IP and port. We’ll now set up a firewall and configure NGINX as a reverse proxy so we can access it directly on port 80 (HTTP).

7. Setup UFW Firewall

sudo ufw enable
sudo ufw status
sudo ufw allow ssh 
sudo ufw allow http 
sudo ufw allow https 
      

8. Install NGINX and Configure

sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
      

In the location part of the server block, add the following:

server {
  listen 80; # Listen on port 80
  listen [::]:80; # Listen on port 80 for IPv6

  server_name yourdomain.com www.yourdomain.com;

  location / {
      proxy_pass http://localhost:3000; # replace with your app's port
      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;
  }
}
      
# Check NGINX config
sudo nginx -t

# Restart NGINX
sudo service nginx restart
      

You should now be able to visit your IP address with no port (port 80) and see your app. Now let's add a domain.

9. Point Your Namecheap Domain to Your Server's IP

Follow these steps to point your Namecheap domain to your Vultr server using A records:

Step 1: Add A Records

  1. Log in to your Namecheap account.
  2. Go to the Domain List and select your domain.
  3. Click on the Advanced DNS tab.
  4. Add the following A records:
    • Type: A Record, Host: @, Value: [Your Server's IP]
    • Type: A Record, Host: www, Value: [Your Server's IP]
  5. Save changes and wait for DNS propagation.

Step 2: Test with Ping

To check if your domain is set up correctly, run the following command in your terminal or command prompt:

ping yourdomain.com

Replace yourdomain.com with your actual domain. If you see your server's IP address in the response, your setup is successful!

10. Add SSL with LetsEncrypt

sudo apt-get update
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

# Test renewal process with
certbot renew --dry-run
      

Now visit https://yourdomain.com and you should see your Node.js app with SSL enabled!

Made with SubEditPro