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>
π§ Step 2: Set Up Node.js on cPanel
- Log in to cPanel.
- Search for Setup Node.js App.
- 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
- Click Create.
- 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
π 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!"
π 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
: Usually22
.
π₯ How It Works
- Push code to your
main
branch. - GitHub Actions checks out your code, installs dependencies, and uploads files to your hosting.
- 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)