Skip to main content

Use Node.js and Express.js application with Nginx on a VM using GitHub project To deployment

1. Prepare VM

  • Update and install necessary packages:
    • sudo apt update && sudo apt upgrade -y
      sudo apt install git nodejs npm nginx -y

  • Verify Node.js and npm installation:
    • node -v
      npm -v
  • Install a process manager like PM2:
    • sudo npm install -g pm2

2. Clone and Set Up Project

  • Navigate to the desired directory:

    • cd /var/www
  • Clone your project repository:

    • git clone https://github.com/RosCheath/rupp-e-card.git
      cd rupp-e-card
  • Install dependencies:

    • npm install
  • Run the app with PM2:

    • cd src
      pm2 start app.js --name rupp-e-card
      pm2 save
      pm2 startup

3. Configure Nginx as a Reverse Proxy

  • Create an Nginx configuration file for domain:

    • sudo nano /etc/nginx/sites-available/verify.rupp.edu.kh
  • Add the following content:

    • server {
          server_name verify.rupp.edu.kh;
      
          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;
          }
      
          listen 80;
      }
  • Enable the configuration:

    • sudo ln -s /etc/nginx/sites-available/verify.rupp.edu.kh /etc/nginx/sites-enabled/
  • Test Nginx configuration:

    • sudo nginx -t
  • Reload Nginx:

    • sudo systemctl reload nginx