Debian Lenny - using passenger to serve your applications with Apache

Following from the first article, we now have passenger (mod_rails) installed.

As such, we can move on and create a Ruby on Rails application and see how easy it is to serve using passenger.


Rails application

Move into the public_html folder (if you don't have one, then simply create it):

cd ~/public_html

Then create a simple Ruby on Rails application:

rails testapp

Done.

Virtual Host

To serve the Rails application, we need to create a virtual host:

sudo nano /etc/apache2/sites-available/testapp

Although already mentioned, one of the exciting things about mod_rails is that you don't need any special settings in the virtual host configuration.

The contents can be as simple as this:

<VirtualHost *:80>

  ServerName  domain1.com
  ServerAlias www.domain1.com

  DocumentRoot /home/demo/public_html/testapp/public

</VirtualHost>

Of course, you can add custom log file locations and other settings but the main thing to understand is the lack of proxy and port settings.

Once done, we can enable the new site:

sudo a2ensite testapp

Rewrite

As you may know, Rails applications make use of an .htaccess file for various rewrite rules.

If this is a fresh Slice and you do not have the Apache rewrite module enabled, now would be a good time to enable it:

sudo a2enmod rewrite

Reload

Finally, restart Apache:

sudo /etc/init.d/apache2 restart

Note: If you get any port and NameVirtualHost errors on reloading Apache, please ensure you read the Apache Virtual Hosts article.

Done.

Is that it?

Yup. It really is that simple to serve Ruby on Rails applications with passenger (mod_rails).

Additional Rails apps can be configured in the same way — create a vhost and it's done.

Note: you may have noticed that we didn't even need to do anything special with regards to permissions here, as passenger will automatically run the application as the user who owns directory that the virtual host points to. In my case, the 'public_html' directory is in the 'demo' users home directory, as such, my application is being run as the 'demo' user. Which is a good thing. We don't want it to be running as root.

Changes to the application

Whenever you deploy changes to your application all you need to do to is:

touch /home/demo/public_html/testapp/tmp/restart.txt

That will enable the new content to be served — the command can be used in Capistrano or any script you use to deploy your applications.

Summary

Phusion's passenger (mod_rails) is easy to install and even easier to use.

There are no ports or proxies or any other complicated configurations — passenger offers a great deal to the Ruby on Rails community.

Ben B

Article Comments:

Evgeny commented Mon Sep 14 05:01:04 UTC 2009:

Many thanks for article! Has stayed with a problem of binding Ruby On Rails to web-server Apache 3 days, and now, having proceeded on specified, to algorithm, all has turned out. Only at last I would like to add something as without this thing for me did not work. The matter is that, probably, it is required to establish the rights to record to a folder and the enclosed subdirectories where there is our rails-appendix.

chmod-R 777/home/demo/public_html/testapp/

Good luck! Vladimir, Russia.

Staffpoll commented Fri Jan 21 11:40:27 UTC 2011:

for latest passenger, testapp config should be like this:

<VirtualHost *:80> ServerName www.testapp.com DocumentRoot /home/demo/public_html/testapp/public <Directory /home/demo/public_html/testapp/public> AllowOverride all
Options -MultiViews
</Directory> </VirtualHost> </code>

Ben commented Sat Aug 13 13:52:31 UTC 2011:

To create a rail app you need the "new". So rail new testapp

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)