How to setup Node.js production application on Apache multiple virtual host server

When it comes to using Node.js on backend it’s quite different from what people used to do for PHP server configuration.

Process manager

First and most important is not to run your application directly with node app.js command instead use one of the following packages: forever, PM2, and some others. This will help maintain your Node application process and restart it if it crashes.

My own pick is PM2 as it’s the most popular and has lots of features, great app monitoring, easy installations and usage.

Installation is quite simple, just using npm or yarn package managers. You probably will install it globally under root user so it can be accessible for all server users.

[root@theserver ~]$ npm -g install pm2 
# Or
[root@theserver ~]$ yarn global add pm2 

Then switch to the user, your virtual host will be using, let’s call it theproject, and run pm2 startup

[theproject@theserver ~]$ pm2 startup

It will respond with a message like this:
[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:

It will require to run command with root privileges, using sudo user or root user directly:

[root@theserver ~]$ <the command from pm2 startup here>

If everything is successful it will add a PM2 service that will run on the system boot under your user theproject. It’s not recommended to install pm2 pm2 startup under the root user directly as it will run all your application scripts under root user as well.

Then you will need to add PM2 process configuration file, it supports json, js, yaml formats, here is the simplest example, process.yaml file:

apps:
  - name: theproject
    script: app.js
    exec_mode: fork

Check more info about process file here.

To run your application, cd to your project folder, and run pm2 start process.yml

[theproject@theserver ~]$ cd theproject
[theproject@theserver theproject]$ pm2 start process.yml

It will start your application and print current status table.

To make sure your app will start again after server restart, run:

[theproject@theserver theproject]$ pm2 save

You can nicely monitor your app using command pm2 monit, check current list of apps pm2 list and view last log records pm2 logs. To get further insights into your app, go to https://keymetrics.io/ setup account and follow instructions, the functionality is really awesome and must have for any production project.

Apache configuration

When our app is up and running on server we actually need to access it somehow using our domain name and 80 port, yet our app is running on some port like 3000 and is available only on 127.0.0.1 (localhost) ip (if not, it might be a security issue).

On 80 port we already have Apache running, so we can use Apache as a reverse proxy for our application. For you you will need to make sure Apache has mod_proxy installed and enabled.

Apache Virtual host configuration will be following:

<VirtualHost *:80>
  ServerName theproject.com
  ServerAlias www.theproject.com
  ErrorLog /home/theproject/logs/error_log
  CustomLog /home/theproject/logs/access_log combined

  ProxyRequests Off
  ProxyPreserveHost On

  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/

  # This is needed only if you want to use web sockets
  RewriteEngine On
  RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
  RewriteCond %{QUERY_STRING} transport=websocket    [NC]
  RewriteRule /(.*)           ws://localhost:3000/$1 [P,L]
</VirtualHost>

After restarting Apache you should be able to successfully access your app using your domain address http://theproject.com.

This configuration also supports socket.io in case you may use web sockets with your application, which is very common with Node apps.