Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


Java/Open Source Daily

jobs.webdeveloper.com

e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Using Phusion Passenger to Deploy a Rails Application on Apache

Bookmark and Share

by Saurabh Bhatia

April 14, 2010

Ready to deploy your Rails app? Learn how to set up Phusion Passenger, configure Apache Web Server, and deploy multiple Rails applications on Apache.

Introduction

When you have written and tested a Rails application, the next step is to host and deploy it to a Web server. The most common solution for this step is using the open source Apache HTTP Server for hosting (it's fast and works with PHP) and using Phusion Passenger to deploy the application to Apache (it makes scaling Apache easy).

In this tutorial, I will explain how to set up Phusion Passenger, configure Apache, and deploy multiple Rails applications on Apache. I assume you are running a Linux box, as most modern-day hosting solutions include a VPS (virtual private server) instance or a dedicated server instance, and you should be comfortable with the basics of using a Linux command line.

Setting Up Phusion Passenger

Phusion Passenger uses mod_rails, a module of Apache, to serve Rails applications via the Apache2 Web server. The first prerequisite for setting up the Passenger module is to install Apache2.

Phusion Passenger can be installed in three different ways. The first way is to install a typical gem like this:

saurabh@saurabh-laptop:~$ sudo gem install passenger 
Building native extensions.  This could take a while... 
Building native extensions.  This could take a while... 
Successfully installed fastthread-1.0.7 
Successfully installed passenger-2.2.11 
2 gems installed

When the gem is installed, you can install the Apache2 module for Rails. A few other dependencies are required in order to install the Apache2 module. They are apache2-prefork-dev (The Apache2 Development headers), libapr1-dev (Apache Portable Runtime), and libaprutil1-dev (Apache Portable Runtime Utility). Make sure you install all of these as follows before you continue.

saurabh@saurabh-laptop:~$ passenger-install-apache2-module

You will be guided through some installation steps as shown in Figure 1-3:


Welcome to Phusion Passenger Apache 2 Module Installer
Click here for larger image

Figure 1. Welcome to Phusion Passenger Apache 2 Module Installer


Phusion Passenger Apache 2 Module Installer Checking for Required Software
Click here for larger image

Figure 2. Checking for Required Software


Phusion Passenger Apache 2 Module Successfully Installed
Click here for larger image

Figure 3. Apache 2 Module Was Successfully Installed

The second way to install Passenger is from source:

  1. Download the Passenger source from RubyForge and download it to a folder where it will permanently reside, as follows:
    saurabh@saurabh-laptop:/opt$ wget http://rubyforge.org/frs/download.php/69546/passenger-2.2.11.tar.gz
  2. Unzip the downloaded archive like this:
    saurabh@saurabh-laptop:~$ tar xvzf passenger-2.2.11.tar.gz
  3. Change the directory to bin inside the extracted Passenger folder and run the binary to install the Apache2 module, as follows:
    saurabh@saurabh-laptop:~/opt/passenger-2.2.11/bin$ ./passenger-install-apache2-module

The third way to install Passenger is by using the native Deb package. (The steps for this are the same as those shown in Figures 1-3.) This approach is specific to Debian and Debian-based distributions like Brightbox Ubuntu, a UK-based hosting repository that provides a package tested on Ubuntu 8.04.

First, open your favorite editor (nano in my case), so you can add the ppa line to the sources file.

saurabh@saurabh-laptop:~$ nano /etc/apt/sources.list

Next, add the ppa line at the end of the file and add the signature like this:

deb http://apt.brightbox.net hardy main

sudo sh -c 'wget -q -O - http://apt.brightbox.net/release.asc | apt-key add -'

saurabh@saurabh-laptop:~$sudo apt-get update

saurabh@saurabh-laptop:~$sudo apt-get install libapache2-mod-passenger

When these steps are complete, you are ready to serve your applications via Apache2 mod_rails and you can proceed to configuring your Apache server and deploying your app to it.

Apache Configuration

Move the Rails application to the /var/www folder on your server and start editing the Apache configuration file to add a virtual host. Point the document root to the public folder of the application, as follows:

saurabh@saurabh-laptop:~$ nano /etc/apache2/sites-enabled/000-default 

<VirtualHost *:80> 
        ServerAdmin webmaster@localhost 

        ServerName www.mywebsite.com
        DocumentRoot /var/www/mywebsite/public 
        <Directory /var/www/mywebsite/public> 
                Options Indexes FollowSymLinks -MultiViews 
                AllowOverride all 
                Order allow,deny 
                allow from all 
        </Directory> 

        ErrorLog /var/log/apache2/error.log 
 
        LogLevel warn 

        CustomLog /var/log/apache2/access.log combined 

</VirtualHost>

Next, simply start your Apache server like this:

saurabh@saurabh-laptop:~$ /etc/init.d/apache start

Now, navigate to your domain in the browser and you will see your application running in the browser.

Deploying Multiple Rails Applications in Passenger

You can deploy multiple Rails applications in two ways, depending on the type of URI you want to deploy to. The first one is deploying to a sub URL, which looks something like this: www.mywebsite.com/suburl. The second one is deploying to a subdomain, which looks like this: www.subapplication.mywebsite.com.

Let's take the first way, serving your application at www.mywebsite.com/suburl. Suppose your apps reside in /var/www/mywebsite/ and your document root is set to /mywebsites/. First, create a symbolic link from the Public folder of the app to the directory created inside the document root of the mywebsites folder as shown below:

ln -s /var/www/mywebsite/suburl/public /var/www/mywebsite/suburl

You have already seen how to create a virtual host for your Rails app. This is how it looks in this case:

<VirtualHost *:80> 
        ServerAdmin webmaster@localhost 
 
        ServerName www.mywebsite.com 
        DocumentRoot /var/www/ mywebsite
        <Directory /var/www/mywebsite> 
                Options Indexes FollowSymLinks -MultiViews 
                AllowOverride all 
                Order allow,deny 
                allow from all 
        </Directory> 
 
        ErrorLog /var/log/apache2/error.log 
 
        LogLevel warn 
 
        CustomLog /var/log/apache2/access.log combined 
 
</VirtualHost>

Before proceeding further, verify two things:

  1. Apache has the appropriate permissions for every application directory.
  2. Multiviews is disabled for this folder.

When you are sure of these requirements, add a RailsBaseURI directive to the virtual host configuration as follows. You can define as many RailsBaseURIs as you want.

<VirtualHost *:80> 
        ServerName www.mywebsite.com 
        DocumentRoot /var/www/ mywebsite
        <Directory /var/www/mywebsite> 
                Options Indexes FollowSymLinks -MultiViews 
                AllowOverride all 
                Order allow,deny 
                allow from all 
        </Directory> 

    RailsBaseURI /suburl
    <Directory /var/www/mywebsite/suburl>   
        Options -MultiViews               
    </Directory>                        

        ErrorLog /var/log/apache2/error.log 
 
        LogLevel warn 
 
        CustomLog /var/log/apache2/access.log combined 
 
</VirtualHost>

When you edited and saved your virtual host configuration, restart the Apache server with the following command:

/etc/init.d/apache2 restart

Conclusion

Phusion Passenger is the most popular way to deploy Rails applications to Apache HTTP Server, because it offers multiple Apache scaling techniques. But its biggest advantage is that PHP apps can run in the same server. So, if a person wants to run his or her WordPress blog in the Rails application, Apache makes that possible also.

About the Author

Saurabh Bhatia has been working with Rails since early 2006 and has a startup company, Safew Labs, that offers Rails consulting. He likes to write clean code, read and write.



Up to => Home / Authoring / Tutorials / Ruby on Rails