Ubuntu Intrepid - using mod_rails to serve your application

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.

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 — mod_rails offers a great deal to the Ruby on Rails community.

Mike

Article Comments:

toord commented Wed Feb 11 02:38:13 UTC 2009:

As usual, Mike, GREAT, GREAT tutorial. Excellent.

conficker commented Sat Feb 14 05:00:11 UTC 2009:

That was a good tutorial. I only wish I was better at Linux admin stuff. These tutorials give me hope, but then I remember the network security, and firewall stuff, and all the AHHH stuff in the background that I'm such a noob at. Here's the best place to learn though to those of you other noobs out there.

Chad FOley commented Sun Mar 01 07:46:48 UTC 2009:

I love passenger so far, but I'm getting an error on a missing production.rb file. My development.rb is good, but I'm stuck on something that should be so simple in making my app a production app and not a development app. I feel like this is ruby 101, but for the life of me I can't a production.rb file out there.

Alex commented Wed Mar 04 22:31:32 UTC 2009:

What happens when you delete the index.html file in the directory? The passenger doesn't work. Why is that?

Luke commented Sun Apr 19 23:58:40 UTC 2009:

I'm following the directions for the install, but I can't change the following message:

  • Apache 2 development headers... not found
    • fastthread... found
    • Apache Portable Runtime (APR) development headers... not found
    • Apache Portable Runtime Utility (APU) development headers... not found

The next screen is:

  • To install Apache 2 development headers: Please run apt-get install apache2-prefork-dev as root.

    • To install Apache Portable Runtime (APR) development headers: Please run apt-get install libapr1-dev as root.

    • To install Apache Portable Runtime Utility (APU) development headers: Please download it from http://httpd.apache.org/ (APR Utility is an integrated part of Apache.)

I've tried the first two steps suggested, haven't been able to figure out the third. Has anyone else run into this?

David Salinas commented Tue Apr 21 12:49:09 UTC 2009:

I followed all instructions in this site but i keep getting this error message everytime i try to access my rails app:

We're sorry, but something went wrong.

We've been notified about this issue and we'll take a look at it shortly.

Log files doesn't take me anywhere

Perplexed commented Sun May 10 11:32:10 UTC 2009:

Some necessary configuration information is missing from this tutorial. I followed the instructions (except I replaced my domain with "domain1") but after retarting apache, I get a the default ("It works!") response when trying to access the new site's url.. (curl http://localhost from the slicehost terminal). Do I need to add a symbolic link? Would I have to do so for every new virtual host added???

Thank you, in advance, for your insights.

Kevin commented Fri May 29 01:04:21 UTC 2009:

@Luke: Apparently "Apache Portable Runtime Utility (APU) development headers" are installed along with either of the first two steps in that error msg ("Apache 2 development headers" or "Apache Portable Runtime (APR) development headers"). I got the same error, then just re-ran the install and it worked...

bryand commented Fri Jul 03 14:50:54 UTC 2009:

Perplexed, I had a similar problem. After reading the following article I discovered you need to disable the default vhost. Run "sudo a2dissite default" and restart Apache and it should work.

https://articles.slicehost.com/2008/12/11/ubuntu-intrepid-apache-config-layout

Justin Louie commented Thu Aug 13 03:00:03 UTC 2009:

Thanks Mike for another great article that helped a lot.

@bryand - thanks for the tip, I appreciate that one, was having the same trouble.

Katie Yowler commented Tue Aug 18 18:27:27 UTC 2009:

@David I had the same problem. Just went back and ran rails testapp again (allowing it to overwrite what I already had) and that cleared things up.

Matt commented Wed Aug 19 02:08:31 UTC 2009:

A few additional notes: 1) If you want to serve from a subdirectory: a) Create a ln -s /apps/railsapp/public /var/www/railsapp (the public folder to a subdir in the apache documentroot) b) Add: RailsBaseURI /railsapp ...inside your virtualhost file

2) If (like me) you haven't done DNS set up yet, you can just add the line above to the 'default' apache file, and then serve from http://[yourip]/railsapp

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)