ManagedCloud Servers

High performance handled and monitored by us 24/7/365. A complete solution to provide you with our in house expertise 24/7 tailored to your specific needs. We'll setup a bespoke server for your site using the latest tehnologies so you can get the most out of your hardware and get your website loading quickly and reliably. Find out more..

cPanelCloud Servers

Recommended - High performance cloud servers with no technical knowledge required. If you're hosting multiple websites already and you're looking to consolidate, or if you're looking to isolate yourself from the shared hosting environment but you don't have the time or knoweldge to manage a server, then the Managed cPanel Servers are for you. Find out more..

UnmanagedCloud Servers

Our unmanaged range gives you complete control at rock bottom prices and our cloud platform boasts super fast multipath 40Gb/s network, the latest Intel Xeon V3 CPUs and enterprise grade redundant SSDs. If you're a sysadmin look no further, we offer some of the best specification to price ratio servers available. Find out more..

Need Web Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on. From £1.60 / month.

View Plans

How to Install and Use Pm2 for Express Application

pm2 is a production process manager for Node.js applications with a built-in load balancer.

pm2 allows you to keep your site up by restarting the application if it crashes. It allow us to reload them without downtime and to facilitate common system admin tasks.

It is a simple and powerful tool that can be used to create a cluster of your node app.

Here, we will learn how to install and use pm2 for Express application and configure Nginx as a reverse proxy for the node application.

Requirements

  • A server running Ubuntu 16.04 server.
  • A non root user with sudo privileges setup on your server.

Getting Started

Before installing other packages, it is recommended to update your system with the latest version.

You can do this with the following command:

sudo apt-get update -y sudo apt-get upgrade -y

Once your system is updated, you can proceed to install Node.js.

Install Node.js

By default, nodejs is not available in Ubuntu repository. So you will need to add nodejs repository first.

You can do this with the following command:

sudo curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

Next, install nodejs with the following command:

sudo apt-get install nodejs

Once nodejs is installed, you can check node and npm version with the following command:

node -v npm -v

Generate Express App

Here, we will generate sample express app using express-generator. You can install express-generator with the following command:

sudo npm install express-generator -g

Next, create normal user for sample express app:

sudo useradd -m -s /bin/bash test sudo passwd test

Login to test user with the following command:

su - test

Next, generate a new simple web application with the express command:

express hakase-app

The above command will create new project directory "hakase-app".

Change the directory to hakase-app and install all dependencies needed by the app.

cd hakase-app npm install

Next, start new application with the following command:

DEBUG=myapp:* npm start

By default, express application will run on port 3000. You can test it by typing the URL http://your-server-ip:3000 on your web browser, you should see the following page:

Install pm2

First, you will need to install pm2. You can install it with npm command:

sudo npm install pm2 -g

Next, login to test user:

su - test

Change the directory to hakase-app and run the express application with the pm2 command below:

pm2 start ./bin/www

You should see the following output:

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

PM2 process manager

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____ _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___ _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__ _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___ _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____ _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________ _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________ _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_ _\///______________\///______________\///__\///////////////__

Getting started

Documentation http://pm2.io/

Start PM2 at boot $ pm2 startup

Daemonize Application $ pm2 start <app>

Monitoring/APM solution https://app.keymetrics.io/

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

[PM2] Spawning PM2 daemon with pm2_home=/home/test/.pm2 [PM2] PM2 Successfully daemonized [PM2] Starting /home/test/hakase-app/bin/www in fork_mode (1 instance) [PM2] Done. ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??? App name ??? id ??? mode ??? pid ??? status ??? restart ??? uptime ??? cpu ??? mem ??? watching ??? ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??? www ??? 0 ??? fork ??? 26755 ??? online ??? 0 ??? 0s ??? 6% ??? 21.4 MB ??? disabled ??? ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Use `pm2 show <id|name>` to get more details about an app

You can get more details about the application running under pm2 with the following command:

pm2 show www

You should see the following output:

 Describing process with id 0 - name www 
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??? status            ??? online                               ???
??? name              ??? www                                  ???
??? restarts          ??? 0                                    ???
??? uptime            ??? 50s                                  ???
??? script path       ??? /home/test/hakase-app/bin/www        ???
??? script args       ??? N/A                                  ???
??? error log path    ??? /home/test/.pm2/logs/www-error-0.log ???
??? out log path      ??? /home/test/.pm2/logs/www-out-0.log   ???
??? pid path          ??? /home/test/.pm2/pids/www-0.pid       ???
??? interpreter       ??? node                                 ???
??? interpreter args  ??? N/A                                  ???
??? script id         ??? 0                                    ???
??? exec cwd          ??? /home/test/hakase-app                ???
??? exec mode         ??? fork_mode                            ???
??? node.js version   ??? 6.10.2                               ???
??? watch & reload    ??? ???                                    ???
??? unstable restarts ??? 0                                    ???
??? created at        ??? 2017-04-23T12:04:42.827Z             ???
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
 Code metrics value 
??????????????????????????????????????????????????????????????????
??? Loop delay ??? 0.6ms ???
??????????????????????????????????????????????????????????????????
 Add your own code metrics: http://bit.ly/code-metrics
 Use `pm2 logs www [--lines 1000]` to display logs
 Use `pm2 monit` to monitor CPU and Memory usage www

Next, enable pm2 service to start at boot with the following command:

pm2 startup systemd

Install and Configure Nginx

Here, we will use Nginx as a reverse proxy for the node application. First, install Nginx with the following command:

sudo apt-get install nginx -y

Next, create a new virtual host configuration file for hakase-app:

sudo nano /etc/nginx/sites-available/hakase

Add the following lines:

upstream hakase-app {
    # Nodejs app upstream
    server 127.0.0.1:3000;
    keepalive 64;
}

# Server on port 80 server { listen 80; server_name yourdomain.com; root /home/test/hakase-app;

location / { # Proxy_pass configuration 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_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_max_temp_file_size 0; proxy_pass http://hakase-app/; proxy_redirect off; proxy_read_timeout 240s; } }

Save and close the file then activate the configuration by creating a symlink in the sites-enabled directory.

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

Next, start Nginx and enable it to start at boot time:

sudo systemctl start nginx sudo systemctl enable nginx

Access Hakase App

Once everything is configured, it's time to test hakase app.

Open your web browser and type the URL http://yourdomain.com. You will see the express application is running under the nginx web server.

Next, save the pm2 configuration and reboot your server. then check the node app is running at the boot time:

pm2 save sudo reboot

Next, check the node app is running at the boot time:

su - test pm2 status www

Need Web Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on. From £1.60 / month.

View Plans