Utilisation de FastCGI sous Apache2 pour Ruby.

It is easiest just to test ruby on rails with a the built in webrick or mongrel http server. But for those who want to integrate ruby on rails into your apache configuration, here are the instructions.

First, you must have Ruby and Rails installed, and your application should be working under WEBrick (<appdir>/script/server) server. This means you'll have all the necessary db-libraries etc. Read RubyOnRails or search the web if you haven't done this already. You also need to have Apache up and running. See ApacheMySQLPHP. apt-get install apache2 mysql-server would be a good thing to do here.

1. Enable mod_rewrite:

sudo a2enmod rewrite

2. Install FCGI for Apache and Ruby:

sudo apt-get install libapache2-mod-fcgid

3. After install mod_fcgid should be enabled, so you dont need to do sudo a2enmod fcgid

You can directly install the libfcgi-ruby1.8 package with apt-get, but the way that works best is to install it using ruby gems as follows.

sudo apt-get install build-essential ruby1.8-dev libfcgi-dev 
sudo gem install fcgi

I did'nt have to do this, but others have said you may have to modify the dispatch.fgi file. That must have been for an older version, but maybe would be helpful for troubleshooting. I didnt have to change any permissions either, so you can ignore this part.

4. Set file permissions:

sudo chgrp www-data -R <appdir> 
sudo chmod g+w -R <appdir>
sudo chmod g+x <appdir>/public/dispatch.fcgi

You can skip this too. You should check that the first line of this file points to a valid ruby interpreter. #!/usr/bin/ruby or #!/usr/bin/ruby1.8 should work. The former is just a symlink installed by ruby package which depends on the ruby1.8 package. If you have both 1.6 and 1.8 installed and you are switching between them (using alternatives or just rewriting the link yourself), make sure Rails is using the 1.8 version.

Next, you should decide whether you would rather have the application running in root of new virtual server or integrated into structure of your already running webserver.

Virtual Server Scenario

1. Create and edit new file:

sudo nano /etc/apache2/sites-available/<servername>

Contents of the new file should be:

<VirtualHost *>
       SetEnv RAILS_ENV development
       ServerName www.mysite.com
       DocumentRoot /home/myuser/www/myrailsproject/public
       <Directory /home/myuser/www/myrailsproject/public/>
               Options ExecCGI FollowSymLinks
               AddHandler fcgid-script .fcgi
               Order allow,deny
               Allow from all
               RewriteEngine On
               RewriteRule ^$ index.html [QSA]
               RewriteRule ^(^.+)$ $1.html [QSA]
               RewriteCond %{REQUEST_FILENAME} !-f
               RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]
               AllowOverride None
       </Directory>
       ServerSignature On
#       ErrorDocument 500 /500.html
       ErrorLog /var/log/apache2/www.mysite.com.error.log
       CustomLog /var/log/apache2/www.mysite.com.access.log combined
       LogLevel warn
</VirtualHost>

Of course replace those values with your own website, error log name, and local path to your rails project. Make sure to link to the 'public' directory.

So /var/www-rails/app would be /var/www-rails/app/public.

For the error and access logs, feel free to make a subdir for them, or place them elsewhere.

As everything is set, you don't need the .htaccess file in your app's public dir anymore. But I didnt have to do anything with them, and just left them alone.

If this is just a local setup, and you don't plan to have an DNS entry for your new virtual server just yet, you can add a new entry (ie. <servername>) to the line beginning with 127.0.0.1 in your /etc/hosts file (separate it from the rest of records either with space or tab).

2. Now lets enable the site:

sudo a2ensite <servername>

3. Afterwards:

sudo /etc/init.d/apache2 restart

Everything should work just fine. If you just added the <servername> into the /etc/hosts file, don't forget that this is just local change, and you have to make it on every other computer that will use the application (/etc/hosts on linux/unix and <windows_dir>\system32\drivers\etc\hosts on windows). On other computers the name of the virtual server must be prepended with your servers real IP address, not 127.0.0.1 though.