Skip to content

Latest commit

 

History

History
297 lines (201 loc) · 5.19 KB

File metadata and controls

297 lines (201 loc) · 5.19 KB

Deploying a Node.js Application with Nginx and PM2

This guide outlines the steps required to deploy your Node.js application on an Ubuntu/Debian server using Nginx as a reverse proxy, nvm for Node.js version management, and PM2 for process management. It also covers setting up SSL with Let’s Encrypt and configuring the UFW firewall.


1. Install Nginx

Update your package list and install Nginx:

sudo apt update && sudo apt install nginx -y

2. Install Node.js with nvm

Using nvm allows you to easily manage multiple versions of Node.js.

  1. Install nvm:

    curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
  2. Reload your shell environment:

    source ~/.bashrc
  3. Install the latest Node.js version:

    nvm install node
  4. Use the latest Node.js version:

    nvm use node
  5. Verify the installation:

    node -v

3. Prepare Your Application

a. Create Application Directory

Create a directory for your website:

sudo mkdir -p /var/www/web/

b. Deploy Application Files

  • If uploading locally: Use SFTP or scp:

    scp web.zip root@<YOUR_VPS_IP>:/var/www/web

    Then:

    cd /var/www/web
    apt install unzip
    unzip web.zip
  • If cloning from a repository:

    git clone <YOUR_REPO_URL> /var/www/web

4. Install Application Dependencies

Navigate to your application directory and install dependencies:

cd /var/www/web
npm install

If your application requires environment variables, create a .env file:

nano .env

Then add your variables (example):

PORT=3000
MONGO_URI=mongodb://localhost:27017/mydb

5. Start the Node.js Application with PM2

a. Install PM2 Globally

sudo npm install -g pm2

b. (If Applicable) Build Your Application

For frameworks like React, Vue.js, Angular, Svelte, Next.js, or if using TypeScript:

npm run build

c. Start Your Application

  • For npm-based start scripts:

    pm2 start npm --name "myapp" -- start
  • For a specific entry file (e.g., src/index.js):

    pm2 start src/index.js --name "web"

d. Enable PM2 to Auto-Start on Reboot

pm2 startup
pm2 save

e. Monitor PM2 Logs

Check the logs to verify your application is running correctly:

pm2 logs

For more detailed logs (last 1000 lines):

pm2 logs --lines 1000

6. Configure Nginx Server Blocks

a. Create a Server Block for Your Domain

Create a configuration file for your domain (e.g., web1.com):

sudo nano /etc/nginx/sites-available/web1.com

b. Configuration Examples

For Applications Using WebSockets

server {
    listen 80;
    server_name web1.com;

    location /.well-known/acme-challenge/ {
        root /var/www/html;
        allow all;
    }

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /api/ws {
        proxy_pass http://127.0.0.1:3000/api/ws;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
    }
}

For Standard HTTP Reverse Proxy

server {
    listen 80;
    server_name web1.com www.web1.com;

    location /.well-known/acme-challenge/ {
        root /var/www/html;
        allow all;
    }

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

c. Enable the Server Block

Create a symbolic link to enable your site:

sudo ln -s /etc/nginx/sites-available/web1.com /etc/nginx/sites-enabled/

7. Test and Restart Nginx

  1. Test the configuration for syntax errors:

    sudo nginx -t
  2. Restart Nginx:

    sudo systemctl restart nginx

8. Secure Your Site with SSL (Let’s Encrypt)

a. Install Certbot and the Nginx Plugin

sudo apt install certbot python3-certbot-nginx -y

b. Obtain and Install an SSL Certificate

Run the Certbot command to automatically configure SSL for your domain:

sudo certbot --nginx

c. Set Up Automatic Renewal

Test the renewal process with:

sudo certbot renew --dry-run

9. Configure the Firewall with UFW

Ensure only essential services (SSH, HTTP, HTTPS) are allowed:

sudo apt install ufw -y
sudo ufw allow OpenSSH
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

Your Node.js application should now be running securely behind Nginx on your specified domain. Should you encounter any issues, verify the logs for both PM2 and Nginx and adjust configurations as necessary.

Happy deploying!


Made with ❤️ by Sahil Hossain