DEV Community

SWAPNIL AHMMED SHISHIR
SWAPNIL AHMMED SHISHIR

Posted on

CI/CD pipeline for deploying a Node.js + MySQL project on cPanel using GitHub

Sure! Here's a step-by-step guide to set up a CI/CD pipeline for deploying a Node.js + MySQL project on cPanel using GitHub. This will automate the deployment process each time you push code to your repository. ๐Ÿš€


๐Ÿ›  Prerequisites

Before we dive in:

โœ… You have a Node.js + MySQL project

โœ… Your cPanel hosting supports SSH access and Node.js apps

โœ… You have access to MySQL DB via cPanel

โœ… GitHub repo is ready

โœ… You can log into cPanel terminal (SSH or built-in Terminal)


๐Ÿš€ Step-by-Step Guide

1. Set Up Your Node.js App on cPanel

a. Login to cPanel

  • Go to Software โ†’ Setup Node.js App
  • Click Create Application
  • Set:
    • App Mode: Production
    • Node.js Version: (e.g., v18+)
    • Application Root: (e.g., myapp)
    • Application Startup File: index.js
  • Click Create

๐Ÿ“ This sets up a private Node.js environment and gives you a start/stop link.


2. Connect cPanel App to GitHub Repo

a. SSH into your cPanel server

ssh username@yourdomain.com
Enter fullscreen mode Exit fullscreen mode

Use the credentials provided by your host.

b. Clone the GitHub repository

cd ~/myapp  # go to the app directory set in cPanel
git clone https://github.com/yourusername/your-repo.git .
Enter fullscreen mode Exit fullscreen mode

Make sure to clone into the same folder your Node.js app is looking in (Application Root).


3. Install Dependencies

npm install
Enter fullscreen mode Exit fullscreen mode

Make sure your package.json is in the root.


4. Set Environment Variables

You can use .env file or cPanel environment variables:

  • In Setup Node.js App, click Edit Environment Variables
  • Add variables like:
    • DB_HOST, DB_USER, DB_PASS, DB_NAME, PORT

OR use .env:

PORT=8080
DB_HOST=localhost
DB_USER=myuser
DB_PASS=mypass
DB_NAME=mydb
Enter fullscreen mode Exit fullscreen mode

And read them in your app using dotenv.


5. Auto-Restart the App After Pull

Edit post-receive Git hook or create a custom deploy script:

a. Create a script: deploy.sh in the project root

#!/bin/bash

echo "Pulling latest changes..."
git pull origin main

echo "Installing dependencies..."
npm install

echo "Restarting app..."
~/nodevenv/myapp/18/bin/pm2 restart index.js
Enter fullscreen mode Exit fullscreen mode

Update paths based on your cPanel environment.

b. Make it executable

chmod +x deploy.sh
Enter fullscreen mode Exit fullscreen mode

6. Enable GitHub Webhook for Auto-Deploy

a. On GitHub:

  • Go to Repo โ†’ Settings โ†’ Webhooks โ†’ Add webhook
  • Payload URL: https://yourdomain.com/deploy.php (or any URL you set up to run deploy script)
  • Content type: application/json
  • Select: Just the push event

b. Create deploy.php on your server:

<?php
shell_exec("cd /home/youruser/myapp && ./deploy.sh");
?>
Enter fullscreen mode Exit fullscreen mode

This runs the deploy script when GitHub webhook triggers.


7. Setup MySQL

Use cPanel > MySQLยฎ Databases:

  • Create your DB, user, and assign user to DB
  • Update your .env or environment variables accordingly

โœ… Done! Now What?

  • Every time you git push, GitHub triggers the webhook.
  • cPanel runs your deploy.sh, pulls the latest code, installs deps, and restarts your app.
  • Your server is always up to date automatically ๐Ÿ’ช

๐Ÿ›ก Pro Tips

  • Use PM2 to manage the Node.js process: auto-restart, logs, etc.
  • Add logs in deploy.sh to debug.
  • Secure your webhook URL to prevent unauthorized access.

Let me know if you want me to generate the files (deploy.sh, deploy.php, .env) for you or help with setting up MySQL connection in Node.js.

Top comments (0)