erfg1234

Member
Dec 12, 2014
7
1
1
Cape Coral, Florida, United States
cPanel Access Level
DataCenter Provider
This guide is for making a NodeJS service with git repos. When you commit to the git repos the nodejs service will restart. This guide assumes you have a WHM/CPanel CentOS server, and will be making an account called dev.

- Replace (PROJECT) with a git project name. Ex: newagesoldier_website
- Replace (SERVICE) with a service name. Ex: newagesite
- Replace (FILE) with your nodejs main file name. Ex: app.js


- In WHM -> Create new account -> username: dev

- In WHM -> Manage Shell Access: Set user dev to "Normal Shell*". (Jailed Shell causes issues with PM2).

- Disable shell fork bomb protection in WHM -> Shell Fork Bomb Protection.

- SSH in to your server as root.

- Install nodejs & NPM:

- If GIT is missing (it shouldn't be), install it. Then install PM2 and make it startup on system reboot:

npm install -g pm2
pm2 startup --user dev --hp /home/dev

- In CPanel, login as user dev. Go to SSH Access -> Manage SSH Keys -> Generate a New Key.

- On Windows Client PC(s): Download the Private Key and place it in "%userprofile%/.ssh".

- SSH in to the server as user dev and make a new git project:

mkdir /home/dev/git/
mkdir /home/dev/git/(PROJECT)
mkdir /home/dev/git/(PROJECT).git
cd /home/dev/git/(PROJECT).git
git init --bare

- On your client computer, clone this repo and do a test push using the URL:

ssh://[email protected](DOMAIN):/home/dev/git/(PROJECT).git

- For each new git project you have to start the pm2 process and save (as dev, not root):

cd /home/dev/git/(PROJECT)
pm2 start (FILE) --name (SERVICE)
pm2 save

- Make a post-receive hook script to restart the PM2 service on new code commit:

nano /home/dev/git/(PROJECT).git/hooks/post-receive

- Copy and paste the script below. Fill in the brackets with your project/service names:

#!/bin/bash
export GIT_WORK_TREE=/home/dev/git/(PROJECT)
echo 'post-receive: Triggered.'
cd /home/dev/git/(PROJECT)
echo 'post-receive: git check out'
git --git-dir=/home/dev/git/(PROJECT).git --work-tree=/home/dev/git/ne$
echo 'post-receive: npm install'
sudo npm install
echo 'post-receive: done. starting app'
pm2 stop (SERVICE)
pm2 start (FILE) --name (SERVICE)
echo 'post-receive: app started.'

- Save, close and mark the post-receive file as executable:

chmod a+x /home/dev/git/(PROJECT).git/hooks/post-receive

- If this is a NodeJS web service, set your NodeJS script's port to something like 3000 and go to:

WHM -> Apache Configuration -> Include Editor -> Pre VirtualHost Include -> All Versions


- and paste this code in (replace brackets):

<VirtualHost (SERVER_IP):80>
ServerName (DEV'S_DOMAIN)
ProxyPreserveHost On
ProxyPass "/" "http://localhost:3000/"
ProxyPassReverse "/" "http://localhost:3000/"
</VirtualHost>
 
Last edited:
  • Like
Reactions: PluggedP93

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,304
1,250
313
Houston
Hi @erfg1234

Thank you very much for taking the time to share this guide!

For others viewing this thread please keep in mind that this is a user submitted guide and has not been tested nor is it supported by cPanel staff.

Thanks!
 
  • Like
Reactions: Infopro