Make NodeJS service with git repos
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://dev@(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):
ServerName (DEV'S_DOMAIN)
ProxyPreserveHost On
ProxyPass "/" "
-
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! 0 -
Como puedo hacer la instalaci"n de PM2 en el CPANEL? How can I install PM2 on CPANEL? 0 -
We use passenger for NodeJS is there a reason you want to use PM2 instead? 0 -
We use passenger for NodeJS is there a reason you want to use PM2 instead?
I want to install PM2 to upload a node.js API so that I can put a live application and thus be able to reload them without downtime and facilitate the administration tasks of the system0
Please sign in to leave a comment.
Comments
4 comments