How to install Ruby on Rails as a fastcgi process under Apache
The procedure below have been tested with Apache 2.0 under CentOS 4.0 and 4.6.
This is my favorite way of running Ruby on Rails under Apache, until I'll take the time to evaluate Mongrel memory consumption and behaviour when proxyied by Apache.
1) fastcgi : download, compile & install
- Download the latest version of fcgi from (fcgi-2.4.1-SNAP-0311112127.tar.gz at time of writing)
- untar somewhere and cd
- ./configure
- make install
By default, this step will copy the include & lib files in /usr/local/ - Which are needed by the ruby-fcgi library described below.
2) mod_fastcgi : download, compile & install
- Download the latest version from www.fastcgi.com/dist (mod_fastcgi-2.4.6.tar.gz at time of writing)
- untar somewhere and cd
- make
- make install
You certainly will need to provide a valid directory where the make process can find a suitable apache build directory.
If you get an error like this one 'make: *** No rule to make target `/usr/local/apache2/build/special.mk'. Stop.', then add a top_dir='/dir/to/build' to the make and make install commands.
Note that you must have the httpd-devel package installed.
eg. for CentOS 4.6 : make top_dir=/usr/lib/httpd
3) Edit Apache's configuration files
- Create a new file under /etc/httpd/cond.f named rails.conf
- edit this file and add
<IfModule mod_fastcgi.c>
FastCgiIpcDir /tmp/fcgi_ipc FastCgiServer /usr/local/www/my_ror_app/public/dispatch.fcgi -initial-env RAILS_ENV=development -idle-timeout 30
</IfModule>
Change RAILS_ENV depending on your environment (development or production) - You might add the -processes N directive to have more processes running on fastcgi startup.
- edit /etc/httpd/conf/httpd.conf and move the 'user apache' and 'group apache' directives before the section where additionnal configuration files are loaded (Include conf.d/*.conf)
4) Create /tmp/fcgi_ipc directory
Then chown apache fcgi_ipc
5) Install the fastcgi library for Ruby
When I installed fastcgi for the first time (a long long time ago ;-)) the gem version of fastcgi was broken so I needed to install it by hand. Since then I've never changed my way of installing it but you might give 'gem install fcgi' a try !
- Download the latest version from raa.ruby-lang.org/project/fcgi
-
Untar the source file somewhere
- Then read the readme file (!) which points you to install.rb config, install.rb setup and install.rb install
6) Create an Apache virtual host
A dumb and meaningless example...
<VirtualHost MyIP:MyPort >
DocumentRoot /usr/local/www/my_ror_app/public
ServerName rails.mydomain.com
<Directory /usr/local/www/my_ror_app/public>
AllowOverride All
</Directory> </VirtualHost>
7) edit my_ror_app/public/.htaccess file
Replace the line RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
with RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
8) Access rights for Apache
Under my_ror_app the /tmp and /log directories must be writeable by Apache.
9) Final cleanup
rake log:clear
rake tmp:clear
And... apachectl graceful. If everything is right, you're done !!