You probably have heard of Node.js at some points in your life. Maybe it is at stack overflow or somewhere else. No surprise that Node.js became really popular as there are increasing interest in JavaScript on Github. Normally, people use HTML, CSS, and JavaScript for front-end development. In the back-end, some popular programming languages like PHP on WordPress or Symfony, Java on spring or J2EE, Python on django or Ruby on Rails. However, today we are going to talk about how to set up a Node.js application for production on Ubuntu 14.04 using Apache2 server. Thanks to Chrome’s V8 JavaScript engine, as the runtime environment interprets JavaScript. It allows the developer to write JavaScript in the back-end. One benefit would be that normally a developer who came from front-end who already knew JavaScript can easily pick up and implement a back end system using JavaScript and do not need to know other languages like frameworks that we talked about above. In this tutorial, we will cover setting up a production-ready Node.js environment that is composed of two Ubuntu 14.04 servers; one server will run Node.js applications managed by PM2, while the other will provide users with access to the application through an Apache2 reverse proxy to the application server. This guide uses two Ubuntu 14.04 servers with private networking in the same server. For simplicity, we also assume that you already have a domain name that points to your web server, so that you may access your web server via a domain name instead of its public IP address. Once you have those set up, then we may continue on this tutorial. We all know that in the local development for Node.js, we usually make a few changes along the way and simply uses the localhost IP address, i.e., wherever the app server’s private IP address is used. Here, is the simplest hello world version of Node.js with Express.

Hello world version of Node.js

  var express = require('express');
  var app = express();

  app.get('/', function (request, response) {
    response.send("Hello World");


source code hosted on GitHub

When you run it, you should see something like this,

chat example for hello world version

This Node.js application simply listens on the specified IP address and port, and returns “Hello World” with a 200 HTTP status code. This means that the application is only reachable from servers on the same private network, such as our web server. Let’s test the application through the private IP address. Open another a terminal session and connect to your web server. Because the web server is on the same private network, it should be able to reach the private IP address of the application using curl. Be sured to change the PRIVATE_IP_ADDRESS to your private IP address and PORT_THAT_Node_JS_APP_LISTEN if you changed to something else.

curl to your Node.js application via private IP address


This should return Hello World! Once this is good. Then, we may proceed and install PM2. Now, we will install PM2, which is a process manager for Node.js application that is running, and configured to listen on the proper IP address and port your specify earlier. And, we will be using npm to install our PM2.

installing PM2 using npm

  sudo npm install pm2 -g

Once this is installed, then we may use PM2 to start a Node.js as simple as this, also be sured to replaced YOUR_NODE_JS_SERVER_NAME_FILE with the name of the Node.js server file you named.

start Node.js application with PM2


Then, you should be able to see that PM2 is now running. You may use the following command to check if the Node.js application is running or not via PM2.

check if Node.js application is running or not with PM2

  sudo pm2 list

Now, you should be able to see your application is running in that output from the command above.
Normally, PM2 will restart automatically if the application crashes or is killed (thanks to PM2), but additonal step need to get the application running again if the web server boot or reboot itself. Hence, be sured to run the following command, so that your PM2 will restart your node.js application say one day you reboot your web server.

run the command so that one day when you reboot your web server, the PM2 will automatically restart your Node.js application

  sudo pm2 startup ubuntu

Once this is done, then we may proceed and set up a reverse proxy server so that we can access our Node.js application via the domain name that you have. In order for this to work, you need to have Apache2 installed in your web server and it can be achieved like this,

installing Apache2 on a web server via a command line

  sudo apt-get update
  sudo apt-get install apache2

Then, you may use the following command to see if it is running,

checking if Apache2 is running

  sudo service apache2 status

It should print something like apache2 is running. If it is not already running, use the following command to start the Apache2,

start the Apache2 server

  sudo service apache2 start

Now, we need to go to Apache2 folder and insert the following configuration for the Apache2 server to work with PM2. Do the following command to get to Apache2 folder and modify the following file,

about to make changes to the config file for Apache2, so that we may access Node.js application via a domain name

  cd /etc/apache2/sites-enabled/
  sudo vi 000-default.conf

add the following configuration in the 000-default.conf file

  <VirtualHost *:80>
    ServerAdmin webmaster@localhost

    ProxyRequests off

    <Proxy *>
      Order deny,allow
      Allow from all

    <Location />
      ProxyPassReverse http://PRIVATE_IP_ADDRESS:PORT_THAT_Node_JS_APP_LISTEN/

source code hosted on GitHub

Now, we need to restart the Apache2 server to take effect.

restart your Apache2 server

  sudo service apache2 restart

Assume that your Node.js application is running using PM2, and your application and Apache2 configuration are correct, you should now be able to access your application via the reverse proxy of the web server. Try it out by accessing your web server with the domain name that you have (the one you put in the Apache2 config file).

Wrapping Up

Hopefully this guide has given you the confidence to do configuration setting with running Node.js application using PM2, and access the application via a reverse proxy of the web server with Apache2. If you would like to learn more and do this in Nginx way, please go here. As our tutorial is greatly inspired from it. I am sure that with this guide, you will now be more comfortable with doing the server side configuration with your Node.js application and Apache2 server on Ubuntu 14.04. I hope that this post has helped you and thank you for reading!


I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.

Getting started