How To Add Ruby/Rails To Your Existing Apache Setup

Rails Logo Still very much in the process of learning Rails myself, I have pretty much mastered the process of running Webrick and Mongrel. Granted, it *is* an easy process.However, I wanted to be able to run my own Ruby applications along my other, mostly PHP-based, applications. That’s where the challenge usually begins. If you use a dedicated server process, you end up having to serve your pages from a non-standard port, such as 3000. This typically does not work for users who are behind a proxy. Additionally, this means that the simplest approach is to ignore virtual servers and run each application on a different port.

I was expecting the RoR Wiki to be helpful. And, overall, it really is; but in this case I found it mostly frustrating, due to some of its information being outdated and not really all that synthetic.

And that is why I decided, inspired by that Wiki, to create my own guide. Note that this guide describes using Ruby with mod_fastcgi.
Note that this guide describes using Ruby with mod_fcgid. I am sure that you’ve read that mod_fastcgi wasn’t working properly and this was a real concern, forcing you to instead use Apache proxying with a regular Ruby server. Not anymore. mod_cfgid fixes all that. No need for an additional server, no need for mod_ruby either.

Here we go: the following instructions are for a Linux system and you need to be a superuser.

.
Pre-Install

  1. Download Ruby-1.8.4.tar.gz and follow the install instructions
    .

or

  1. Simply use yum. Syntax:
    yum install ruby
    .
  2. Get RubyGems from http://rubyforge.org/projects/rubygems/
    Note: rubygems-0.8.11.tgz is recommended,
    rubygems-0.9.x is not compatible with Rails (or vice versa if you prefer)
  3. Install RubyGems
  4. Install Rails. Super-duper easy syntax:
    gems install rails

.
Set up Rail’s environment

  1. Create a rails user:
    adduser rails
  2. passwd rails
  3. Create a web directory:
    cd ~rails
  4. chmod a+x .
  5. mkdir public_html
  6. cd public_html
  7. Create the famous cookbook application for the sake of this howto:
    rails cookbook
  8. Create your virtual domain. I do not use a control panel, so in my case it looks like this:
    vi /var/name/yourdomainname.com.db
  9. And I add a ‘A’ record:
    cookbook 14400 IN A 11.22.33.44

.
Build Apache Modules

  1. Download and install fcgi. I am using ‘wget’ but any other download tool will work:
    wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
  2. tar zxvf fcgi-2.4.0.tar.gz
  3. cd fcgi-2.4.0
  4. ./configure
  5. make && make install
    .
  6. Download and install mod_fcgid. I am using ‘links’, which is a text web browser, but feel free to use Firefox or whatnot:
    links “http://prdownloads.sourceforge.net/mod-fcgid/mod_fcgid.2.0.tar.gz?download”
  7. tar zxvf mod_fcgid.1.10.tar.gz
  8. cd mod_fcgid.1.10
  9. Edit Makefile and update the value of top_dir with Apache’s home directory. In my case:
    top_dir = /usr/local/apache
  10. make && make install
    .
  11. Do not forget to retrieve the Ruvy fcgi package:
    gem install fcgi

.
Configure Apache

  1. Edit httpd.conf
    .
  2. Add some optional fcgid settings:
    <IfModule mod_fcgid.c>
    IPCCommTimeout 40
    IPCConnectTimeout 10
    DefaultInitEnv RAILS_ENV production
    SocketPath /tmp/fcgidsock
    </IfModule>
    .
  3. Tell Apache about your rails directory:
    <Directory /home/rails/public_html>
    Options ExecCGI
    AddHandler fcgid-script .fcgi
    AllowOverride all
    Order allow,deny
    Allow from all
    </Directory>

    Of course, you could setup more than one directory!
    .
  4. Add a virtual host, hopefully the first of many:
    <VirtualHost 11.22.33.44:80>
    ServerName cookbook.yourdomainname.com
    ServerAdmin wwwadmin@yourdomainname.com
    DocumentRoot /home/rails/public_html/cookbook/public
    ErrorLog /var/log/httpd/rails_error_log
    Options Indexes ExecCGI FollowSymLinks
    RewriteEngine On
    </VirtualHost>
    Note: ‘RewriteEngine On’ is of utmost importance. If you do not use mod_rewrite, Rails will be unable to route your queries.

.
Prepare your cookbook application:

  1. Edit ~rails/public_html/cookbook/public/.htaccess
    RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
    becomes:
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
    .
  2. Avoid nasty ‘500′ errors; make sure that Rails can create temporary files, such as cache and session files, and logs:
    chmod -R 777 tmp logs
    Of course, if you can use less permissive masks, even better!

Stop! You’re done.
Wasn’t it easy?

If you have any questions, just ask. I may even be able to give you some helpful answer!

Updates

I found out two interesting facts:

  1. Being meant for a production environment, fastcgi does what it does best: it caches your scripts. Therefore if you modify, say, a controller, you need to notify dispatch.fcgi before you can see the modified script in action:
    killall -TERM dispatch.fcgi
    .
  2. MySQL native bindings were not available in the default distro (?). I added them later:
    gem install mysql — –with-mysql-config=`which mysql_config`

Similar Posts:

If you enjoyed this post, make sure you subscribe to my RSS feed!

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] Caso você já tenha um servidor Apache rodando na sua máquina (ou num servidor de intranet, por exemplo), leia este artigo e veja como adicionar ao Apache já instalado a possibilidade de interpretar também páginas desenvolvidas em Ruby/Rails. [...]

hi all.

Good info. helped a lot. Please keep it goind and updated. Our best wishes to you. Learning Ruby is a great exp.

Cool.

Let a sleeping dog lie,

Hi,

How to configure Session in Apache Server. i am facing problem with multi user testing, some data are mixing when we are doing the transaction.

Thanks

Hi

I wrote a similar tutorial (and installation script) in case you have more constraints (no access to Apache’s configuration, different directories for scripts and writable files), applicable to people that want to install RoR on a hosted environment (SourceForge).

You can check it there:
http://weacemethod.sourceforge.net/wiki/index.php/HowToInstallRedmineSF.NET

Don’t hesitate to leave any comments/remarks/questions, I will update accordingly.

Thanks for your info, it helped me a lot

Muriel

Leave a comment

(required)

(required)


data recovery