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
-
App Mode:
- 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
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 .
Make sure to clone into the same folder your Node.js app is looking in (
Application Root).
3. Install Dependencies
npm install
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
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
Update paths based on your cPanel environment.
b. Make it executable
chmod +x deploy.sh
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");
?>
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
.envor 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.shto 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)