This guide walks you through automating the deployment of your Node.js Express server to a self-hosted Ubuntu Virtual Private Server (VPS) using GitHub Actions.
Ensure you have the following ready:
- A GitHub account.
- A VPS running Ubuntu (e.g., AWS EC2, DigitalOcean, Linode, etc.).
- Your Node.js Express server code pushed to a GitHub repository.
- Basic knowledge of the command-line interface (CLI).
-
Log in to your VPS:
ssh username@your-vps-ip
-
Update the system and install necessary tools:
sudo apt update && sudo apt upgrade -y sudo apt install -y curl git tar -
Install Node.js and PM2:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - sudo apt install -y nodejs sudo npm install -g pm2 -
Optional - Set up a firewall: Configure your firewall to allow SSH, HTTP, and your application port (e.g.,
3000):sudo ufw allow OpenSSH sudo ufw allow 3000 sudo ufw enable
- Go to GitHub and create a new repository.
- Push your Node.js Express server code to the repository:
git init git remote add origin https://github.com/your-username/your-repo.git git add . git commit -m "Initial commit" git branch -M main git push -u origin main
-
Navigate to your GitHub Repository:
- Go to Settings > Actions > Runners and click Add Runner.
-
Install the GitHub Runner: Follow the instructions for Linux provided on GitHub, or run:
cd ~ curl -o actions-runner-linux-x64.tar.gz -L https://github.com/actions/runner/releases/latest/download/actions-runner-linux-x64.tar.gz mkdir actions-runner && tar xzf actions-runner-linux-x64.tar.gz -C actions-runner cd actions-runner
-
Configure the Runner: Replace
REPO_URLandYOUR_TOKENwith your repository URL and the token provided during setup:./config.sh --url https://github.com/your-username/your-repo --token YOUR_TOKEN
-
Start the Runner:
./run.sh
-
Optional - Run as a Service: To ensure the runner starts on boot:
sudo ./svc.sh install sudo ./svc.sh start
-
Inside your repository, create the directory
.github/workflows. -
Add a new file called
deploy.ymlwith the following content:name: Deploy to Self-Hosted VPS on: push: branches: - main jobs: deploy: name: Deploy to Self-Hosted VPS runs-on: self-hosted steps: # Step 1: Checkout repository - name: Checkout repository uses: actions/checkout@v2 # Step 2: Use Node.js - name: Use Node.js uses: actions/setup-node@v2 with: node-version: '16.x' # Step 3: Install dependencies - name: Install dependencies run: | npm install --frozen-lockfile npm install -g pm2 # Step 4: Configure environment variables - name: Configure environment run: echo "SERVER_PORT=${{ secrets.SERVER_PORT }}" >> .env # Step 5: Build application (if applicable) - name: Build application run: npm run build # Step 6: Start the application with PM2 - name: Start application run: pm2 start npm --name "my-app" -- start
- Navigate to your repository on GitHub.
- Go to Settings > Secrets and click New repository secret.
- Add the required secrets:
SERVER_PORT: The port number your app will run on (e.g.,3000).- Add any other environment variables your app requires.
-
Push changes to the
mainbranch:git add . git commit -m "Deploy to VPS" git push origin main
-
Go to the Actions tab in your GitHub repository to monitor the workflow.
-
Check Runner Status: On GitHub, go to Settings > Actions > Runners. Ensure your runner is listed as online.
-
Check Workflow Logs: If the deployment fails, review the logs in the Actions tab to debug any issues.
-
Test Your Application: Visit your VPS IP or domain at the specified port (e.g.,
http://your-vps-ip:3000) to ensure your Node.js application is running.
-
Using a Custom Domain: Set up a reverse proxy like Nginx to serve your app over a custom domain with HTTPS.
-
Automate with PM2: PM2 ensures your application restarts if it crashes:
pm2 startup pm2 save
-
Monitoring: Use
pm2 monitto monitor your application in real-time.
This workflow ensures your Node.js Express server is automatically deployed to your Ubuntu VPS upon pushing code changes to your GitHub repository. Let me know if you need further clarification or help!