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

#!/bin/sh
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

#!/bin/sh
echo "Checkout the Files"
GIT_WORK_TREE=/var/www/domain/sampleNode.comquas.com git checkout -f
echo "start service"
service sampleNode start

After receive the file , git will checkout the file to /var/www/domain/sampleNode.comquas.com. 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 [email protected]:/opt/git/sampleNode.git
git push deploy master

After finish pushing , check the file at /var/www/domain/sampleNode.comquas.com . 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
#

NAME=sampleNode
APP=/var/www/domain/sampleNode.comquas.com/app.js
forever=/usr/local/bin/forever
export PATH=$PATH:/usr/local/bin/
LOG=/var/log/sampleNode.log

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

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 [email protected]:/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/samplenode.comquas.com

In sampleNode.comquas.com

server {
    listen   80;
    server_name samplenode.comquas.com;

    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_pass http://127.0.0.1:3001/;
            proxy_redirect off;
    }
}

Now , symbolic link to site enable

sudo ln -s /etc/nginx/sites-available/samplenode.comquas.com /etc/nginx/sites-enabled/samplenode.comquas.com

After finish , reload the nginx like following

sudo service nginx reload

We have finished everything and you can test your website at http://samplenode.comquas.com (samplenode.comquas.com 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