Welcome to weblogs.com.pk Sign in | Join | Help

Staging Node Application

Staging Node Application

Node applications are “usually” deployed on Linux / Unix environments; and if you are not exposed to these environments and its one of the challenges due to which you avoiding Node; then lets take some time out and get our hands dirty a little bit. We will need a Linux Server to stage our app; I will be installing Ubuntu Server in a VM; that will be connected to a typical Wifi Router where DHCP server is already in place. We can give an ip in the DHCP server bound to our VM’s MAC address; this way the server will have a static ip and we can SSH / Browse to it conveniently. Lets make a simple hello.js while our VM gets ready!

image

  • Notice I have initialized Git as well

The Ubuntu Server already has GIT tools on clean installation. On the server I created two folders; hello.git that will be our git repository and the hello folder that will be the “root” of our node application. When we create GIT repository using $ git init –bare; it creates folder structures; the hooks folder is of our interest; because we need to create a “post-receive” script there that will “update” the hello folder @ the server whenever we will “push” the code changes from the “client” (development machine) The script looks like this

#!/bin/sh
GIT_WORK_TREE=/home/khurram/hello git checkout -f

  • We also need to make this script executable using $ chmod +x post-receive

For the “client” (development machine) I already have TortoiseGit; its a fantastic Windows Explorer extension; many folk likes command prompt; but I am aged now and have other things to remember SmileIn TortoiseGit; we can add “Remote”s to the folder from TortoiseGit > Settings context menu; I added my hello.git repository with URL; ssh://192.168.0.100/home/khurram/hello.git

image

Once this remote url is added; we can “push” to it; in TortoiseGit; after committing it gives us the Push option; and doing that it will push the code to the server and there our post-receive script will run and it checkout the repository into the GIT_WORK_TREE folder (our hello folder)

image

Now we are able to push our code to the server repository from where it automatically gets updated to the “Node Application Root folder” we have designated. Lets next install Node on to our server; on Ubuntu for this; we need to run the following commands

$ sudo apt-get install –y nodejs
$ sudo apt-get install –y npm

image

Once these two are installed; we can run our application using

$ nodejs hello.js

The nodejs process will run as long as we are sshing the server; we want to run node as daemon so it continue to run even if we are not sshing and for this we need PM2 that can be installed with command

$ sudo npm install pm2 –g

pm2 expects “node” name as the binary and we have nodejs binary; we can make a node link to nodejs so pm2 doesnt complaint; to start our application; we issue $ pm2 start hello.js giving a fancy output

khurram@ubuntu:~/hello$ whereis nodejs
nodejs: /usr/bin/nodejs /usr/lib/nodejs /usr/include/nodejs /usr/share/nodejs /usr/share/man/man1/nodejs.1.gz
khurram@ubuntu:~/hello$ cd /usr/bin
khurram@ubuntu:/usr/bin$ sudo ln -s nodejs node
khurram@ubuntu:/usr/bin$ ls -al node
lrwxrwxrwx 1 root root 6 May 18 14:15 node -> nodejs
khurram@ubuntu:/usr/bin$ cd ~khurram/hello
khurram@ubuntu:~/hello$ pm2 start hello.js

                        -------------

   Looking for a complete monitoring and management tool for PM2?
    _                             _        _            _
   | | _____ _   _ _ __ ___   ___| |_ _ __(_) ___ ___  (_) ___
   | |/ / _ \ | | | '_ ` _ \ / _ \ __| '__| |/ __/ __| | |/ _ \
   |   <  __/ |_| | | | | | |  __/ |_| |  | | (__\__ \_| | (_) |
   |_|\_\___|\__, |_| |_| |_|\___|\__|_|  |_|\___|___(_)_|\___/
             |___/

                          Features

                   - Real Time Dashboard
                   - CPU/Memory monitoring
                   - HTTP monitoring
                   - Event notification
                   - Custom value monitoring
                   - Real Time log display

                          Checkout

                   https://keymetrics.io/

                        -------------

[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized
[PM2] Starting hello.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ memory      │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ hello    │ 0  │ fork │ 9335 │ online │ 0       │ 0s     │ 20.238 MB   │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app

If you have been keen; we ran our http server on 127.0.0.1; we need to run it on the static ip that our server has so that we can test the running application from the client. Lets update the hello.git/hooks/post-receive script and add pm2 restart hello (hello is the app name)

#!/bin/sh
GIT_WORK_TREE=/home/khurram/hello git checkout -f
pm2 restart hello

With above arrangement in place; our application will restart itself whenever new code is pushed. Lets go ahead and update the code at the development machine and push it to the server through GIT which will automatically update the application root (~hello) and restart the application. If we commit + push the code changes through TortoiseGit; it even gives the output of our post-receive script

image

Published Wednesday, May 18, 2016 4:05 PM by khurram

Comments

No Comments

New Comments to this post are disabled