Ops23

Deploying applications efficiently and consistently is essential in modern software development. This guide outlines the process for automating the deployment of a Node.js application using Nginx as a reverse proxy on a Virtual Private Server (VPS), leveraging GitHub Actions for automation through SSH. This method emphasizes security, reliability, and streamlined processes for deployment.

Prerequisites

Before starting, ensure you have:

  • A GitHub repository with your Node.js application.
  • A VPS with SSH access.
  • Nginx is installed on your VPS, and configured as a reverse proxy.

Step 1: Configure Your VPS

Set up your VPS by installing Node.js and Nginx. Configure Nginx to redirect traffic to your Node.js app by editing the Nginx configuration. This involves adjusting the /etc/nginx/sites-available/default file or creating a new one in the same directory to include a server block directing to your app’s port.

Nginx configuration example:

server { 
    listen 80; 
    server_name example.com; 
    location / { 
        proxy_pass http://localhost:3000; # Node.js app 
        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; 
    } 
}

Test and reload Nginx after making these changes.

Step 2: Prepare Your GitHub Repository

Create a .github/workflows directory in your GitHub repository. Within this directory, create a YAML file named deploy.yml to define your deployment workflow.

Step 3: Define the GitHub Actions Workflow

In the deploy.yml file, specify the deployment workflow. This includes checking out your code, installing dependencies, building your Node.js application (if needed), and deploying it to your VPS via SSH.

Here’s a sample deploy.yml:

name: Deploy Node.js Application to VPS

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
      run: npm install
    - name: Build
      run: npm run build # If your app requires a build step
    - name: Deploy to VPS
      uses: appleboy/scp-action@master
      with:
        host: ${{ secrets.VPS_HOST }}
        username: ${{ secrets.VPS_USERNAME }}
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        port: ${{ secrets.SSH_PORT }}
        source: "./build/"
        target: "/destination/path/on/server"
    - name: Restart Node.js application
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.VPS_HOST }}
        username: ${{ secrets.VPS_USERNAME }}
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        port: ${{ secrets.SSH_PORT }}
        script: |
          cd /destination/path/on/server
          npm install --production
          pm2 restart all # Using PM2 for process management

Step 4: Configure GitHub Secrets

Use GitHub Secrets to securely store your SSH credentials and server information, protecting this sensitive data. Add the following secrets to your repository:

  • VPS_HOST: Your VPS IP address or domain name.
  • VPS_USERNAME: SSH username.
  • SSH_PRIVATE_KEY: Your SSH private key.
  • SSH_PORT: The SSH port, typically 22.

Step 5: Deploy Your Application

With the workflow configured, push your changes to the main branch to initiate deployment. You can track the deployment process in the “Actions” tab of your GitHub repository.

Conclusion

Using GitHub Actions to automate the deployment of your Node.js application to a VPS via SSH streamlines your workflow, ensuring your deployment is both secure and reliable. This method reduces manual intervention, promoting a more efficient deployment process.

Leave a Reply

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