Using Ruby On Rails With Apache2 On Debian Etch - Page 2

6 Modifying Our Application's .htaccess File

The web folder of testapplication is /var/rails/testapplication/public. It contains an .htaccess file that we must modify so that Apache2 can run RoR applications using mod-fcgid.

vi /var/rails/testapplication/public/.htaccess

Comment out the AddHandler fastcgi-script .fcgi and AddHandler cgi-script .cgi lines and add the line AddHandler fcgid-script .fcgi. Also comment out the line RewriteRule ^(.*)$ dispatch.cgi [QSA,L] and add the line RewriteRule ^(.*)$ dispatch.fcgi [QSA,L] (note that it's now dispatch.fcgi instead of dispatch.cgi). Afterwards, the file should look like this:

# General Apache options
#AddHandler fastcgi-script .fcgi
#AddHandler cgi-script .cgi
AddHandler fcgid-script .fcgi
Options +FollowSymLinks +ExecCGI

# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests
#
# Example:
#   RewriteCond %{REQUEST_URI} ^/notrails.*
#   RewriteRule .* - [L]

# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
#
# For better performance replace the dispatcher with the fastcgi one
#
# Example:
#   RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteEngine On

# If your Rails application is accessed via an Alias directive,
# then you MUST also set the RewriteBase in this htaccess file.
#
# Example:
#   Alias /myrailsapp /path/to/myrailsapp/public
#   RewriteBase /myrailsapp

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

# In case Rails experiences terminal errors
# Instead of displaying this message you can supply a file here which will be rendered instead
#
# Example:
#   ErrorDocument 500 /500.html

ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"

 

7 Creating A Virtual Host For Our RoR Application

(If you use ISPConfig, please go to chapter 8!)

Now it's time to create an Apache vhost for our application. I will use the hostname testapplication.example.com so that our application can be reached under http://testapplication.example.com (assuming that testapplication.example.com points to the correct IP address).

The easiest way to create such a vhost is to replace the existing default vhost in /etc/apache2/sites-available/default (assuming that this default vhost isn't needed!):

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/default_old
cat /dev/null > /etc/apache2/sites-available/default
vi /etc/apache2/sites-available/default

<Virtualhost *:80>
   ServerName testapplication.example.com
   DocumentRoot /var/rails/testapplication/public/

   <Directory /var/rails/testapplication/public>
      Options ExecCGI FollowSymLinks
      AllowOverride all
      Order allow,deny
      Allow from all
   </Directory>
</Virtualhost>

Of course, instead of replacing the default vhost, you can simply add your testapplication.example.com vhost at the end of /etc/apache2/sites-available/default so that you keep the default vhost.

Now we restart Apache:

/etc/init.d/apache2 restart

Next we open http://testapplication.example.com in a browser. We should see the default RoR page:

That's it! Now we can start to develop our RoR application in the /var/rails/testapplication directory.

 

8 RoR And ISPConfig

(If you don't use ISPConfig, skip this chapter!)

In this chapter I assume that you have created a web site called testapplication.example.com with the document root /var/www/web1/web in ISPConfig, and that your RoR testapplication is still in /var/rails/testapplication.

To make our RoR testapplication available in the testapplication.example.com vhost which we have created in ISPConfig, we do the following:

First, we put the following lines into the Apache Directives field of the testapplication.example.com web site in ISPConfig:

<Directory /var/www/web1/web>
  Options +ExecCGI +FollowSymLinks
  AllowOverride all
</Directory>

Then we rename /var/rails/testapplication/public to /var/rails/testapplication/web, copy the contents of /var/rails/testapplication to /var/www/web1, and make the Apache user the owner of /var/www/web1/web:

cd /var/rails/testapplication
mv public web
cp -pfr * /var/www/web1
chown www-data:web1 /var/www/web1/web

That's it. The RoR application should now work in the testapplication.example.com vhost which we have created in ISPConfig.

 

9 Links

Share this page:

7 Comment(s)

Add comment

Comments

From: at: 2007-11-20 03:45:21

This guy seems to be a sys admin, not a Ruby developer. Rubyists almost without exception use mongrel, not fcgi, and this tutorial results in an old version of Ruby (via apt-get). To get Ruby 1.8.6
on Debian you have to compile it -
see http://www.urbanpuddle.com/articles/2007/08/17/mongrel-cluster-1-0-2-and-ruby-1-8-5

From: gerard at: 2009-09-23 00:58:57

The point of the article was to show how to get RoR on apache2, not on mongrel, which is an entirely different HTTP server.

From: evden eve nakliyat at: 2010-01-27 02:53:29

Good work! Your post/article is an excellent example of why I keep comming back to read your excellent quality content that is forever updated. Thank you!

From: at: 2008-01-08 10:41:02

see rails wiki post: http://wiki.rubyonrails.org/rails/pages/RailsOnDebian

"Note:
Do not install rails using apt-get! The debian repository includes a very old and broken version."

From: Douglas at: 2012-05-05 15:08:13

Pretty good for the apache2 part even though this is old. I 'm running

debian squeeze now. All except for the rails part. I use rvm and do not install any ruby as a root user. An article on how to use this with rvm would be nice. Keep in mind that there is also passenger and passenger gem now.

Thanks! Good job.

OG1

From: mc0e at: 2012-08-12 08:41:37

This recipe looks like it'll create a rails app directory where all the code and templates are owned by the www-data user, which is poor practice from a security standpoint. 

gazmcghee raises concerns about the ruby version.  However, the link they point to is now lacking content.  In general, I've been concerned by the lack of secure download sources (ie cryptographically signed) for ruby other than those that come from the operating system vendors.  There's a lot of situations where you might ignore the issue, but I would have thought the ruby community would have matured enough by now to provide such things for those who do need it.

From: Johannes at: 2008-11-20 14:03:58

... for the HowTo: I just tried it and it just worked after desperately trying it myself. saved me a lot of time&troubles .. - johannes