Deploying a Node.js application
on Passenger + Apache

This guide teaches you how you can deploy your app to a server. This guide assumes that you have already done the following, and that you know how to do them:

  • Passenger and Apache are already installed on the server.
  • You have already transferred the application's code to the server.
  • You have already installed Node.js.
  • You have already installed all necessary dependencies that your application needs.

If you miss one of these prerequisites or don't know how to perform it, then we recommend you to follow the Deployment tutorial. The deployment tutorial is end-to-end guide and teaches you how to install every component.

Table of contents

  • Loading...

Deploying an app to a virtual host's root

To deploy an app to an Apache virtual host's root path, the following steps must be taken:

  • Add a virtual host entry to your Apache configuration file.
  • The virtual host's document root must point to your application's public subdirectory.
  • The Apache per-directory permissions must allow access to this directory.
  • MultiViews must be disabled for this directory.

Here is an example:

<VirtualHost *:80>
    ServerName yourserver.com

    # Tell Apache and Passenger where your app's code directory is
    DocumentRoot /path-to-your-app/public
    PassengerAppRoot /path-to-your-app

    # Tell Passenger that your app is a Node.js app
    PassengerAppType node
    PassengerStartupFile app.js

    # Relax Apache security settings
    <Directory /path-to-your-app/public>
      Allow from all
      Options -MultiViews
      # Uncomment this if you're on Apache >= 2.4:
      #Require all granted
    </Directory>
</VirtualHost>

Replace yourserver.com with your server's host name, replace /path-to-your-app with your application's code directory path and replace app.js with your app's entry point file.

When you are done, restart Apache:

$ sudo apachectl restart

(Depending on your operating system, the right command may be apache2ctl instead of apachectl.)

You are now done. If you run into any problems, please refer to the troubleshooting guide.

Deploying an app to a sub-URI or subdirectory

Sub-URI deployments in Node.js require framework-specific adjustments in the application. For example, in Express 4.0+, you should use a router. An alternative is to use url rewriting to avoid the need for sub-URIs altogether.

You can also deploy an app to a sub-URI instead of the root URI. For example, suppose that you already have a virtual host for the application /websites/phusion:

<VirtualHost *:80>
    ServerName www.phusion.nl
    DocumentRoot /websites/phusion/public
    <Directory /websites/phusion>
        Allow from all
        Options -MultiViews
        # Uncomment this if you're on Apache >= 2.4:
        #Require all granted
    </Directory>
</VirtualHost>

And you want your application, located in /websites/secondapp, to be accessible from the URL http://www.phusion.nl/subpath. To do this, you need to perform the following:

  • Set Alias {SUBURI} {PATH TO YOUR APPLICATION'S PUBLIC DIRECTORY}.
  • Create a <Location /{SUBURI}> block.
  • Inside the Location block, set PassengerBaseURI /{SUBURI}.
  • Inside the Location block, set PassengerAppRoot {PATH TO YOUR APPLICATION ROOT}.
  • Create a <Directory {PATH TO YOUR APPLICATION PUBLIC SUBDIRECTORY}> block.
  • Inside the Directory block, set Allow from all, and (if you’re on Apache >= 2.4) Require all granted.
  • Inside the Directory block, disable MultiViews.

Here is an example:

<VirtualHost *:80>
    ServerName www.phusion.nl
    DocumentRoot /websites/phusion/public
    <Directory /websites/phusion>
        Allow from all
        Options -MultiViews
        # Uncomment this if you're on Apache >= 2.4:
        #Require all granted
    </Directory>

    # These have been added:
    Alias /subapp /websites/secondapp/public
    <Location /subapp>
        PassengerBaseURI /subapp
        PassengerAppRoot /websites/secondapp

        PassengerAppType node
        PassengerStartupFile app.js
    </Location>
    <Directory /websites/secondapp/public>
        Allow from all
        Options -MultiViews
        # Uncomment this if you're on Apache >= 2.4:
        #Require all granted
    </Directory>
</VirtualHost>

When you are done, restart Apache:

$ sudo apachectl restart

(Depending on your operating system, the right command may be apache2ctl instead of apachectl.)

You are now done. If you run into any problems, please refer to the troubleshooting guide.