Deploy Node.js website with git

If we are using node.js , you need to start and stop for running app. So, stop the node.js process. upload the files. start the node.js process again. If we do like that everything , I feel it’s waste of time. We can do automatically with git + service for our life easier.

Setup Git

First we need to setup git on server.

sudo apt-get install git
mkdir /opt/git
mkdir /opt/git/sampleNode.git
cd /opt/git/sampleNode.git
git --bare init

Now, we have created the git repo at /opt/git/sampleNode.git

After that , we need to setup the git hook.

sudo vi /opt/git/sampleNode.git/hooks/pre-receive

In the pre-receive

echo "Stop sampleNode service"
service sampleNode stop

Later we will create the sampleNode service. Now, just put like this.

pre-receive hook when push to the server from client. We can put shell script or python script can write at pre-receive hook. For us, we just want to stop the node.js app service. So, just wrote service stop.

Next, we need to create post-receive

sudo vi /opt/git/sampleNode.git/hooks/post-receive

In the post-receive

echo "Checkout the Files"
GIT_WORK_TREE=/var/www/domain/ git checkout -f
echo "start service"
service sampleNode start

After receive the file , git will checkout the file to /var/www/domain/ After finish checkout , service will start again.

Now, we need to update the permission

sudo chmod +x /opt/git/sampleNode.git/hooks/pre-receive
sudo chmod +x /opt/git/sampleNode.git/hooks/pre-receive

Ok, now git service is complete.

Test push data to server from client

In client,

cd  sampleNode
git init
git add .
git commit -m 'init commit'
git remote add deploy root@yourIP:/opt/git/sampleNode.git
git push deploy master

After finish pushing , check the file at /var/www/domain/ . If the file is there , we are almost OK. Of course, we will see error message at push to server. You also should see “Stop sampleNode service” , echo “Checkout the Files” , echo “start service” .

Create the service in linux

If you don’t have forever , you should install forever first.

npm install forever -g

Create the file under /etc/init.d . My app name is sampleNode , so I create like following

$ vi /etc/init.d/sampleNode

In sampeNode

#! /bin/sh
# /etc/init.d/nsmag

export PATH=$PATH:/usr/local/bin/

case "$1" in
    echo "Starting $NAME" PORT=3001 $forever --minUptime 5000 --spinSleepTime 2000 -a -l $LOG start $APP
    echo "Stopping script $NAME"
    $forever stop $APP
    echo "List"
    $forever list
    echo "Usage: /etc/init.d/sampleNode {start|stop|list}"
    exit 1

exit 0

I am using HOSTNAME and PORT for Node.js ENV. After adding the file , we need to update permission.

sudo chmod +x /etc/init.d/sampleNode

Ok. It almost finish and let we test.

sudo service sampleNode start

And then check your website. Everything OK , you can deploy your website from your client.

git add .
git commit -m 'Production'
git remote add deploy root@yourIP:/opt/git/sampleNode.git
git push deploy master

Setup Nginx for Node.js

First we need to create nginx site-available

sudo vi /etc/nginx/sites-available/


server {
    listen   80;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_redirect off;

Now , symbolic link to site enable

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

After finish , reload the nginx like following

sudo service nginx reload

We have finished everything and you can test your website at ( is example domain)

Now, we don’t need FTP for file upload to deploy. We can be done with git push and easy to deploy node.js website now.

comments powered byDisqus