Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Make NodeJS service with git repos

Discussion in 'Workarounds and Optimization' started by erfg1234, Jul 13, 2018.

  1. erfg1234

    erfg1234 Member

    Joined:
    Dec 12, 2014
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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://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):

    <VirtualHost (SERVER_IP):80>
    ServerName (DEV'S_DOMAIN)
    ProxyPreserveHost On
    ProxyPass "/" "http://localhost:3000/"
    ProxyPassReverse "/" "http://localhost:3000/"
    </VirtualHost>
     
    #1 erfg1234, Jul 13, 2018
    Last edited: Jul 13, 2018
  2. cPanelLauren

    cPanelLauren Forums Analyst II
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    3,137
    Likes Received:
    222
    Trophy Points:
    173
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    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!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    Infopro likes this.
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice