wiki:RubyOnRailsApacheModRails

Version 8 (modified by joshuadf, 9 years ago) (diff)

--

Configuring mod_rails for Apache

This procedure explains how to configure Apache HTTPD as a front-end web server for Rails applications. This method relies on the mod_rails module for Apache. The example shown here works for Ubuntu, however installation instructions for other platforms can be found on the mod_rails web site.

Assumptions

  • OS version is Ubuntu 8.10 server.
  • Ruby 1.8.6 is installed (if not, follow this procedure first).
  • If PHP is installed, it is version 5.2.6.
  • Apache 2.2.9 is already installed and running.
  • Unless otherwise noted, all steps are carried out as user 'root'.
  • $RAILS_APP_ROOT is set to the path of your Rails application root directory.
  • $APACHE_DOC_ROOT is set to the path of Apache's htdocs directory.

Re-configure Apache

If you're intention is to install Rails alongside PHP, the makers of mod_rails (Brightbox) recommend using Apache MPM worker instead of MPM pre-fork (mostly for performance reasons). What this means, is that if you attempt to install Passenger from the Brightbox repository using apt-get, package libapache2-mod_php5 will be removed from your system and PHP support in Apache will likely cease to function.

The recommended configuration involves installing FastCGI instead of mod_php.

Replace mod_php with FastCGI

If you don't care about your Apache server supporting PHP, then you can skip this step.

Install/replace the following packages:

  • apache2-mpm-prefork (should be replaced with apache2-mpm-worker)
  • libapache2-mod-php5 (should be replaced with libapache2-mod-fcgid and php5-cgi)

By issuing the following commands.

apt-get update
apt-get install apache2-mpm-worker libapache2-mod-fcgid php5-cgi

Note the output of your install command, pay close attention to the NEW and REMOVED packages.

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  apache2-threaded-dev apache2.2-common
Suggested packages:
  apache2-doc apache2-suexec apache2-suexec-custom php-pear php5-timezonedb
The following packages will be REMOVED:
  apache2-mpm-prefork libapache2-mod-php5
The following NEW packages will be installed:
  apache2-mpm-worker libapache2-mod-fcgid php5-cgi
The following packages will be upgraded:
  apache2-threaded-dev apache2.2-common
2 upgraded, 3 newly installed, 2 to remove and 22 not upgraded.
Need to get 6199kB of archives.
After this operation, 5362kB of additional disk space will be used.
Do you want to continue [Y/n]? 

Then, create file /etc/apache2/conf.d/php-fastcgi and add this text.

<IfModule mod_fcgid.c>
  AddHandler fcgid-script .php
  DefaultMaxClassProcessCount 4
  DefaultInitEnv PHP_FCGI_MAX_REQUESTS 0
  DefaultInitEnv PHP_FCGI_CHILDREN 0
  FCGIWrapper /usr/bin/php-cgi .php
  <Files *.php>
    Options +ExecCGI
  </Files>
</IfModule>

You must restart the web server for changes to take effect.

/etc/init.d/apache2 restart

Install Phusion Passenger (mod_rails for Apache)

Add the Brightbox repository to package manager's sources and install package libapache2-mod-passenger.

echo "deb http://apt.brightbox.net hardy main" > /etc/apt/sources.list.d/brightbox.list
wget -q -O - http://apt.brightbox.net/release.asc | apt-key add -
apt-get update
apt-get install libapache2-mod-passenger

Afterward installation, check to make sure the module is enabled within Apache.

ls -lt /etc/apache2/mods-enabled/ | grep passenger
lrwxrwxrwx 1 root root 32 2009-07-03 14:55 passenger.conf -> ../mods-available/passenger.conf
lrwxrwxrwx 1 root root 32 2009-07-03 14:55 passenger.load -> ../mods-available/passenger.load

Set relative_url_root in Rails Application

Edit $RAILS_APP_ROOT/config/environment.rb and add the following to your initializer block. This instructs your Rails application to remove the leading '/my_app' portion of the requested URL before routing the client's request.

Rails::Initializer.run do |config|

  # For cases where the application is running in a subdirectory with Apache as the front-end
  # web server (i.e. when using Phusion Passenger mod_rails).
  # In this example, a soft-link would be created in the Apache document root called 'rails' that
  # points to the Rail application's public folder.
  config.action_controller.relative_url_root = "/my_app"
  .
  .
end

If you do not set relative_url_root, you will see errors similar to the following in your Rails log files.

ActionController::RoutingError (No route matches "/my_app/" with {:method=>:get}):

Launch your Rails application via Apache

Create a soft link within your Apache document root folder (e.g. /var/www by default on Ubuntu) to the public directory of your Rails application.

cd $APACHE_DOC_ROOT
ln -s $RAILS_APP_ROOT/public my_app

Change ownership of the Rails application to match web server user (e.g. www-data on Ubuntu).

chown -R www-data:www-data $RAILS_APP_ROOT
chmod -R o-rwx $RAILS_APP_ROOT

Add the following directive to your Apache httpd.conf file. On Ubuntu, this would instead be inserted into your virtual host file under /etc/apache2/sites-enabled/. The Phusion Passenger users guide has more information about these options and Apache configuration:

RailsEnv development
RailsBaseURI /my_app

Restart the web server for changes to take effect.

/etc/init.d/apache2 restart

Check that passenger is running.

ps -ef | grep -i passenger
13571 13567  0 12:50 ?        00:00:00 /usr/lib/passenger/ApplicationPoolServerExecutable 0 /usr/lib/passenger/passenger-spawn-server  /usr/bin/ruby  /tmp/passenger_status.13567.fifo
13572 13571  0 12:50 ?        00:00:01 Passenger spawn server                                                                                               
13663 13572  0 12:54 ?        00:00:01 Passenger FrameworkSpawner: 2.2.2   

References

General install instructions for Phusion Passenger
Installing mod_rails on Mac OS X