Monthly Archives: March 2012

Deploying nodejs application on Apache web server

In this post I will explain how to deploy your own NodeJS application on Apache server. I’ll also briefly discuss how to use supervisor to manage your multiple node-driven applications.

There are many possible ways for deploying your node apps and redirecting input/output streams from a node process to your Apache/nginx web server. In this post we use a simple python tool called: supervisor. To use supervisor, we need to setup a startup script for our application, install and configure supervisor, and configure VirtualHost on the apache or nginx web server.

start.sh script
Create a simple Bash script, called start.sh, in the root directory of your application. This script will start up your application.

#! /bin/sh
cd /var/www/vhosts/{my_first_app.com}/{path_to_my_first_app}
/usr/local/bin/node app.js

Make sure that the path to node and the path to your application folder are correct. Test by executing the start.sh script.

supervisor install and configuration
supervisor is a tool that provides an easy way for managing processes. Installing supervisor is easy. You simply follow your standard python package manager installation procedure: eg. easy_install supervisor or pip install supervisor

To configure supervisor first generate the standard configuration file:
echo_supervisord_conf > /etc/supervisord.conf

and then fill-in appropriate fields to match to your application paths:

[program:my_first_app]
command=/var/www/vhosts/{my_first_app.com}/{path_to_my_first_app}/start.sh
autostart=true
autorestart=true
startretries=3
stdout_logfile=/var/www/vhosts/{my_first_app.com}/{path_to_my_first_app}/log/server.log
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=5
stderr_logfile=/var/www/vhosts/{my_first_app.com}/{path_to_my_first_app}/log/error.log
stderr_logfile_maxbytes=10MB
stderr_logfile_backups=5

To launch the supervisor you run supervisor command. To check the status of your managed application, use supervisorctl These executables live in your python path (where your particular python package manager installed them).

Apache VirtualHost configuration
Inside your VirtualHost entry, setup the following:

ServerName myfirstapp.com:80
ServerAlias *.myfirstapp.com
DocumentRoot /var/www/vhosts/my_first_app.com/my_first_app/public
AllowOverride FileInfo Indexes
RewriteEngine On

# redirect all non-static requests to node
RewriteCond %{DOCUMENT_ROOT}/${REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ http://#{server_ip}:#{application_port}%{REQUEST_URI} [P,QSA,L]

 

Full deployment automation
With the above instructions you have to manually copy, build and deploy your application into the appropriate directory on the server, in the examples above, at /var/www/vhosts/{my_first_app.com}/{path_to_my_first_app}

This is quite a lot of work, and ideally, a single push to a remote repo should update your application deployment on your production web server. To achieve that, we need automation. I will explain it in the next post – stay tuned.