DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

CI/CD Setup for Node.js on Shared Hosting (cPanel)

Shared hosting is primarily designed for PHP websites, but you can run and deploy Node.js apps with the right setup. This guide walks you through securing your hosting, setting up Node.js, and creating a CI/CD pipeline with GitHub Actions.


πŸ”’ Step 1: Secure Your Hosting

If your subdomain shows a directory listing ("Index of /") or allows file downloads, add a .htaccess file to block directory browsing and secure sensitive files.

Create .htaccess inside /home/username/qiz-api.example.com/:

# Disable directory listing Options -Indexes # Block access to environment and config files <FilesMatch "^(\.env|.*\.json|.*\.lock|.*\.md)$">  Order allow,deny Deny from all </FilesMatch> 
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Step 2: Set Up Node.js on cPanel

  1. Log in to cPanel.
  2. Search for Setup Node.js App.
  3. Click Create Application and select:
  • Node.js Version: Choose Node 18/20/22 (depending on host support).
  • Application Root: /home/username/qiz-api.example.com
  • Application Startup File: index.js
    1. Click Create.
    2. Start your app from cPanel.

βš™οΈ Step 3: Prepare Your Project for Deployment

Make sure your .env is not committed to Git and that npm install --production works.

Your folder structure might look like this:

β”œβ”€β”€ auth_files/ β”œβ”€β”€ node_modules/ β”œβ”€β”€ uploads/ β”œβ”€β”€ whatsapp_instances/ β”œβ”€β”€ .env β”œβ”€β”€ index.js β”œβ”€β”€ package.json β”œβ”€β”€ package-lock.json 
Enter fullscreen mode Exit fullscreen mode

πŸš€ Step 4: Configure GitHub Actions CI/CD

In your project root, create .github/workflows/deploy.yml:

name: Deploy Node.js API to cPanel on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Node.js uses: actions/setup-node@v3 with: node-version: '22' # Change to 18 or 20 if your host doesn't support 22 - name: Install dependencies run: npm install --production - name: Build (if needed) run: npm run build || echo "No build script" - name: Deploy to cPanel via SSH uses: appleboy/scp-action@master with: host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USERNAME }} password: ${{ secrets.SSH_PASSWORD }} port: ${{ secrets.SSH_PORT }} source: "." target: "/home/username/qiz-api.example.com" - name: Restart Node.js App uses: appleboy/ssh-action@master with: host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USERNAME }} password: ${{ secrets.SSH_PASSWORD }} port: ${{ secrets.SSH_PORT }} script: | cd /home/username/qiz-api.example.com npm install --production touch tmp/restart.txt || echo "Restart signal sent" - name: βœ… Deployment Success run: echo "πŸŽ‰ Deployment to cPanel was successful!" 
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ Step 5: Add GitHub Secrets

In your GitHub repository settings, add these secrets:

  • SSH_HOST: Your cPanel server IP or hostname.
  • SSH_USERNAME: Your cPanel username.
  • SSH_PASSWORD: Your cPanel password.
  • SSH_PORT: Usually 22.

πŸ”₯ How It Works

  1. Push code to your main branch.
  2. GitHub Actions checks out your code, installs dependencies, and uploads files to your hosting.
  3. Your Node.js app is restarted automatically.

βœ… Best Practices

  • Use .htaccess to block directory listing and sensitive files.
  • Exclude .env and secrets from version control.
  • Keep node_modules out of Git; let deployment handle installs.
  • Use Node.js versions supported by your host (18/20/22).

Top comments (0)