Running Node js App on Ubuntu 16.04

Node js is one of the most popular platforms for building server-side networking applications. It can run on major operating systems (Windows, OS X, Linux and FreeBSD). Applications built with Node js can run at the command line and in this article, we will be showing how to run the service on a live Linux based server.

We will show how to set up a production-ready Node js app on Ubuntu 16 Server. The server will be managed by a node Process Manager (PM2) and provide access to the user through a reverse proxy using Nginx. We will be using Let’s Encrypt free certificate with the Nginx server.

Requirements:

We will assume that:

  • You have installed Ubuntu 16.04 server with root access following installing Ubuntu server with whogohost
  • A domain name is registered and pointed to the server’s IP address.
  • Nginx is installed on the server.
  • You know how to use Linux editors (nano, vim).
  • You have access to the root

When you are sure that the above is ready, we can begin with the Node.js installation if Node.js is not already installed on your server. You can learn how to setup Ubuntu 16.04 with Node.js preinstalled.

Install Node.js

  • Update Packages:
           sudo apt-update && sudo apt-upgrade
  • Install Git
           sudo apt-get install git
  • Change to the home directory
           cd ~
  • Run the command
           sudo apt-get install nodejs

The package will install the node.js binary and npm. You need to run an optional installation for some npm packages to work.

           sudo apt-get install build-essential

Create a Node js Test Application.

We will create a simple Hello world application that returns “Hello world” to HTTP requests. Please, note that it’s a sample application to test that the app is running on the server. You can replace your server with your own application but ensure you change or modify the listen to IP address and port.

Create Hello world.

Let’s create and edit our Node.js Application using nano as our editor.  Create a sample application called hello.js

           cd ~
           nano hello.js

copy and paste the following code into the hello.js app. You may use another port other than port 8080 for your Node.js application

hello.js
var http = require('http');

http.createServer(function (req, res) {

  res.writeHead(200, {'Content-Type': 'text/plain'});

  res.end('Hello World\n');

}).listen(8080, 'localhost');

console.log('Server running at http://localhost:8080/');

Save and exit the file.

Install PM2

We will use process manager to manage how node.js applications because it makes it easy to run and manage our node.js applications. We will install pm2 with node package manager (npm) and use it for other package installation.

Use npm to install pm2 on our server.

          sudo npm install pm2 -g

Manage the application with pm2.

We will start our hello.js application to run in the background

         pm2 start hello.js

Your application will be added to a list of processes that will be displayed when you run a new node.js application.

Here’s a sample output.

Pm2 will automatically assign an app id and app name that is based on the application’s name. It maintains other information such as the process id, memory usage, uptime etc.

Note: pm2 automatically restart any running application that crashes and allows us to run startup commands.

Startup subcommands configure the startup script that launches node.js process manager and all processes when it boots. Pm2 gives us an easy way to use system subcommand which is required to launch our application when we boot or reboot our server.

Run pm2 startup

             sudo pm2 startup systemd

You should see something like this in the command line.

Every application will now start automatically after restarting the server.

Other PM2 Usage (Optional)

With pm2, you can run a couple of other subcommands that allow you to get information about your running application and also manage your list of processes. Like most commands, running it without an argument will display the help page.

To get the list of running applications:

pm2 list

Restart the App:

pm2 restart App or ID

Stop the App:

pm2 stop App or ID

To get more information about a process:

pm2 info App or ID

pm2 has a process monitor that can be pulled with the monit subcommand. It displays the memory, CPU usage and the status of the running application.

sudo apt-get update pm2 monit

Congratulations, your node.js application is running using pm2. Let’s setup reverse proxy on our Nginx server.

Set up the Reverse Proxy Server with NGINX

Now that our Node.js application is running on a local address, we need to make it accessible for users on the web and to achieve this, we will set up a reverse proxy with Nginx server to give access to our users.

Install Nginx server:

sudo apt-get install nginx

Modify the nginx default server configuration:

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

You should see that we have an existing location / block within the nginx server block. You will need to modify the listen block if your application uses another port number.

/etc/nginx/sites-available/default

upstream nodejs { server IP_address:8080; }

server {
server_name localhost;
root ~/app_directory;

location /
{
try_files $uri $uri/ @nodejs;
}
location @nodejs
{
proxy_redirect off;
proxy_http_version 1.1;
proxy_pass http://nodejs;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

The configuration allows the server to responds to all request at its root e.g myapp.com or http://myapp.com or http://IP_Address. You can add more location blocks to serve other application with the same server. You will simply modify the port settings and access the app in http://myapp.com/new_application.

location /new_application
{
try_files $uri $uri/ @nodejs;
}
location @nodejs
{
proxy_redirect off ;
proxy_http_version 1.1 ;
proxy_pass http://nodejs;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Restart NGINX:

sudo service nginx restart

Conclusion

Your node.js application is now running on pm2 behind the reverse proxy on Ubuntu 16.04 server. The reverse proxy gives the users the flexibility to access other applications and static pages/web contents you want users to have access to.

Leave a Reply

Previous Post
N50,000 saved my business

How N50,000 saved my business… A life changing story by Rukayat

Next Post

Meet Omotola Adeyina – February Employee Of The Month

Related Posts