Ubuntu Hardy - 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, reload Apache:

sudo /etc/init.d/apache2 reload

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.

Although it is relatively new, mod_rails offers a great deal to the Ruby on Rails community.

PickledOnion

Article Comments:

PeterG commented Thu May 01 20:41:22 UTC 2008:

I have an existing rails app running in hardy using apache2 mongrel etc

Can it hurt to simple redeploy using passenger using this article protocols

PickledOnion commented Fri May 02 09:18:48 UTC 2008:

Peter,

I don't see why that would be an issue.

I would create a new vhost so you could quickly pop back to the mongrel setup if required and then disable the old one and enable the new one.

In theory it should be straight forward....

PickledOnion

Rodrigo commented Fri May 02 14:12:12 UTC 2008:

What about memory usage (specially in a 256mb slice)? mod_rails' default configuration seems to spawn up to 20 instances of the application if requested, and that would cripple the slice... maybe you should recommend tweaking the RailsMaxPoolSize option according to the slice size. Or am I undestanding this the wrond way?

PickledOnion commented Fri May 02 14:14:44 UTC 2008:

Hi,

I would always recommend adjusting any installation to your site and Slice needs.

I have been using mod_rails for a few hours and haven't seen any issues with memory but as I say, you will always need to optimise anything you install.

PickledOnion

fractious commented Fri May 02 17:11:45 UTC 2008:

Thanks for taking the time to post this Pickled. Any tips you can give on how to go about optimising RailsMaxPoolSize for best performance would be a great addition :)

PickledOnion commented Fri May 02 17:13:49 UTC 2008:

Hi,

After Rodrigo's comment and yours I think I will do a follow up article going through the various options.

I must admit I've not put it under any sort of load but I'll see what I can come up with :)

PickledOnion

fractious commented Fri May 02 17:23:14 UTC 2008:

Nice one. Looking forward to it.

PeterG commented Sat May 03 04:45:05 UTC 2008:

ok I finally got my 2 websites up and served by passenger using rails. did free -m mem total 256 used 187 free 69

get this

Each website has ONE index.html page with exactly 10 characters

This is using sqlite3 and mysql is not even installed yet.

And i followed the hardy instructions from page one on a new install. ??????? not being critical here I am just stating the facts man just the facts.

As simple as passenger may be I would also prefer an apache mongrel mysql tutorial before something that as yet is unproven. Keep up the good work this is great stuff.

PickledOnion commented Sat May 03 12:03:38 UTC 2008:

Hi Peter,

Not really sure what your point is?

You have two Ruby on Rails applications up and running on a 256 Slice with memory to spare..

You can't serve a site with one mongrel (not if you want more than 1 visitor at a time anyway) so you would need 4 mongrels to serve the 2 sites. A 256 cannot handle 4 mongrels when under load. It is something we see all the time and reducing the mongrels to 3 allows a 256 to work within parameters.

Sure, this is new stuff but I honestly don't see what your point is?

PickledOnion

Ken commented Mon May 05 16:09:42 UTC 2008:

Hi PickledOnion,

Thanks for this and all the other great work you've done. I'm curious - if we're using mod_rails what is the impact on deployments? What do we need to adjust from the Capistrano side? FYI, I would also like to see an article laying out the traditional mongrel/MySQL setup.

Thanks, Ken

Dave commented Tue May 06 02:48:28 UTC 2008:

I'm curious how extensive your rails app is to be able to have 3 mongrels on a 256M slice. With my app, I can barely get 1 mongrel running, and even then I'm still swapping. I do have a background rails task, so that counts for another rails instance. Still, 3 is out of the question for a 256M slice. On my 512M slice, I can barely get 2 mongrels going.

I posted my disappointment in the forums since I came from a 32 bit distro where the exact same application could have 7-8 mongrels going in 1 Gig with plenty of room to spare (~55M per mongrel).

I wonder how mod_rails would help me.

PickledOnion commented Tue May 06 09:24:58 UTC 2008:

Hi Dave,

I don't know what is going on with your Slice or what may be misconfigured (it's all I can think of at the moment) but something is very wrong with your setup.

On a 512 Slice you should be able to get 5 or 6 mongrels running with no issues whatsoever as far as memory is concerned.

It is well beyond the comments to be able to diagnose what is wrong but if you are running 1 mongrel and it is swapping then it is possibly the application code?

Just a guess on my part but I have never seen the figures you mention (1 mongrel and swapping, etc) before.

PickledOnion

Ohamana commented Wed May 07 10:03:05 UTC 2008:

Hi, thanks for the nice article.

Anyway, I'm experiencing a strange problem.

I configured apache2 and passenger on my ubuntu 7.10 box with no difficulties, but when I access to my rails app(just for testing) it gives an error message saying that I don't have permission to my public directory.

So I even changed whole directories' permissions to 777, and set it's owner to 'www-data' but that doesn't helped.

What can be the problem? I just modified 'apache2.conf' and 'sites-available/default' files. Apache is running well.

Cody commented Sun May 18 17:21:20 UTC 2008:

I would highly recommend updating the article and including the bit about RailsMaxPoolSize. Adding something like:

RailsMaxPoolSize 2

To the vhost is very important on a 256MB slice. As an example, for a slice serving subversion repositories and running Warehouse (rails app) as the front end, I'm seeing up to 230MB of RAM being used, that's with only 2 Passenger processes. This includes of course MySQL, 8 Apache processes, and 2 Passenger processes. So to leave the RailsMaxPoolSize at the default (something like 20 processes could be spawned) would cause some trouble for a lot of folks. I'd recommend RailsMaxPoolSize of 2, maybe 3 depending on the rails app in question and the amount of Apache processes that can be spawned (make sure MaxClients isn't set to crazy number!), the database server in use, etc.

Ralph commented Tue May 20 20:04:50 UTC 2008:

I've found both the article and the comments quite helpful. Thanks.

Whenever you can get to it, I suggest updating the "Configuring Capistrano" articles with information about deploying with Passenger instead of Mongrel.

dynamethod commented Mon Jun 09 05:00:37 UTC 2008:

Im getting this quiet a bit with RoR and Mysql + Apache2.2

once i click on "About you application's enviroment"

i get this:

Routing Error

No route matches "/rails/info/properties" with {:method=>:get}

mez commented Wed Jun 11 06:21:10 UTC 2008:

I am having issues loading multi rails app with mod_rails. Also The only way I can get one app running is if I add the VirtualHost to the /etc/apache2/http.conf. If I try running what you have above it simply does not work. Any help would be great.

cheers

PickledOnion commented Wed Jun 11 09:37:08 UTC 2008:

Hi mez,

One simple question: Are you using Ubuntu Hardy?

I suspect you are not as you are using http.conf which Ubuntu Hardy does not use.

If you are not using Hardy then it won't work - as the title suggests this article is for Ubuntu Hardy only.

PickledOnion

Rick Sandhu commented Sat Jun 14 21:58:43 UTC 2008:

I am getting the same error as 'mez' and it is quite annoying:

Message:

Routing Error

No route matches "/rails/info/properties" with {:method=>:get}

System Configuration:

Ubuntu LTS 8.04 Apache 2.2 Ruby 1.8.6 Rails 2.1.0 Passenger 1.0.5

Rob Clarke commented Sun Jun 22 11:01:18 UTC 2008:

I have just tested on my local machine and "About your application's environment" (and /rails/info/properties) don't work in production. So it is normal that it doesn't work because passenger uses the production environment.

Fadhli commented Mon Jun 23 00:51:08 UTC 2008:

Any articles on using mod_rails and capistrano?

Robert Felty commented Wed Jun 25 13:31:17 UTC 2008:

@Fadhli - This was a pretty good article on mod_rails and capistrano: http://jimneath.org/2008/05/10/using-capistrano-with-passenger-mod_rails/

Andy commented Sun Jul 06 03:45:56 UTC 2008:

Hello. I am deploying from github. I set up modrails and the setup looks good, but Capistrano keeps trying to run script/spin and fails because I haven't created this. I did this one other time using Apache+Mongrels from another slicehost article, however with modrails, I would not expect to have to create a script/spin. Am I incorrect? Rails 2.1.01 Capistrano v2.4.3

Andy commented Tue Jul 08 04:03:09 UTC 2008:

In reply to myself, my new mod_rails deploy recipe does not use script/spin, so I needed to override the deploy:start task. http://tinyurl.com/6b8cwt

bbgun commented Tue Jul 22 03:19:48 UTC 2008:

How do you configure the virtual host file so that an html.erb/template file outside of /public in a rails app is targeted as the root?

Ryan Townsend commented Wed Aug 20 22:39:21 UTC 2008:

I was also having the same problem as Ohamana, until I ran 'sudo chown -R www-data:www-data APPNAME' then rebooted the application by 'touch APPNAME/tmp/restart.txt', don't know whether that is of any use.

Scitesy commented Fri Sep 26 04:09:56 UTC 2008:

Use "sudo a2dissite default" to disable the default virtual host.

Andrea commented Sun Nov 02 14:12:23 UTC 2008:

Hi sorry, the supernoobs question, but how should i create the public_html directory? I should create a simple mkdir public_html in my home path?

PickledOnion commented Mon Nov 03 11:17:11 UTC 2008:

Andrea,

That is correct. The vhosts article linked to above will show how to create the public_html directories:

http://articles.slicehost.com/2008/4/29/ubuntu-hardy-apache-virtual-hosts-1

PickledOnion

Jose Castaneyra commented Mon Dec 15 02:39:05 UTC 2008:

Hi all, I recently tried to install mod_rails in my 256 slice with Ubuntu Hardy, I have running apache 2.2.8, php5, imagemagick, RoR 2.2.2, and when I try to run a testing rails app from a url I get the listing files.

What could be?

Thanks in advance.

Jose Castaneyra commented Mon Dec 15 05:51:53 UTC 2008:

Sorry guys, I found what was the problem, it was an error in the configuration file of the virtual hosting, I was omitting the "public" part at the end:

DocumentRoot /home/demo/public_html/testapp/public

I promise to read more carefully :), anyway, if anybody found something like this, please verify you configuration twice.

Jim Jeffers commented Mon Dec 15 21:01:54 UTC 2008:

You may also want to include an explanation on this option: RailsAutoDetect off

If you are hosting non multiple applications including non rails applications you will want to set the autodetect feature to off. If you do this than any vhost that is to use passenger will need to include a rails base URI. For example:

DocumentRoot /myapp/public RailsBaseURI /

If you don't turn off auto detect you will get some really strange behavior. I had a rials app on a subdomain and a wordpress install on the other. The wordpress app was redirecting requests to the rails app on the other subdomain unless an absolute path to the file was being passed. I suspect it's because modrails now overwrites modrewrite. Simply turning off rails autodetect effectively disables mod_rails on a given vhost.

Nate Bird commented Sat Dec 20 21:12:53 UTC 2008:

For those looking to a solution to the cap deploy:start problem with mod_rails - just run cap deploy. For some reason Capistrano really wants to run the spinner script all the time. Here is my generic deploy script to slicehost: http://gist.github.com/38430

alan commented Thu Jan 08 04:12:29 UTC 2009:

Does anyone have the answer to why the

"About you application's enviroment"

link not work?

i get this:

Routing Error

No route matches "/rails/info/properties" with {:method=>:get}

PickledOnion commented Thu Jan 08 11:17:20 UTC 2009:

alan,

It is standard for the link to not work when on a remote host and/or when in production mode.

it is a simple security procedure.

PickledOnion

Colm O'Gairbhith commented Tue Jan 20 23:38:22 UTC 2009:

Hello, Firstly, thanks for the clarity of the articles, its the first time I've been able to follow an entire series of articles without a single false step.

Until the very end, that is.I'm hoping to host a webapp on slicehost which I'll then show in an iframe on a page served from my existing hosting company. As such I'll only initially be accessing my webapp using the IP address of my virtual slicehost server. I've installed the webapp in the ~/publichtml/myappname directory. At this point I don't know what to put in the /etc/apache2/sites-available/myappname file for ServerName and/or ServerAlias. The DocumentRoot points to /home/myuser/publichtml/myappname/public/

My next question is, if someone can help me with the above, what then do I put as the URL in my broswer ? Is it http://172.0.0.1/myappname or something like that ? ( thats obviously not the real IP address, just to illustrate ) ?

To have come so close is both fantastic and frustrating as I feel ( or at least hope ) that its not a big deal to solve the problem I'm facing.

Best regards, Colm

Colm O'Gairbhith commented Wed Jan 21 23:05:07 UTC 2009:

I forgot to mention in the above post that I am using Intrepid not Hardy, in case that makes a difference.

/ Colm

Matias Pelenur commented Sun Jan 25 15:41:19 UTC 2009:

Hi PickledOnion, Thanks for the article. You may also want to mention installing the optional Ruby Enterprise Edition, also made by Phusion. It supposedly uses less memory and gives better performance, and very easy to use with Passenger.

http://www.rubyenterpriseedition.com/

One caveat that I found just after installing REE is that you need to make sure the gems are installed in the right place (for example if you use a different rails version than the latest one). You can do this by running gem install -i /opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8 ... .

chovy commented Wed Apr 01 15:05:53 UTC 2009:

Add this to your vhost to define which environment to load:

<Directory "/x/www/myapp.dev/public"> RailsEnv "development" Order allow,deny Allow from all </Directory>

Eric commented Sat May 02 05:02:28 UTC 2009:

So I can only use mod_rails to serve an app that is in production mode?

I get:

We're sorry, but something went wrong.

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

When I try to click the "About your application's environment" link and whenever I attempt to navigate to any controller.

Anyone see this?

Eric commented Sun May 03 20:51:31 UTC 2009:

Ok here are the steps I had to follow to get an app in development, created using the command "rails testapp", served.

In the config directory of the rails app: chown www-data environment.rb

In <virtualhost> add the line: RailsEnv development

Then I created the file db/development.sqlite3 by hand

Bill commented Mon May 04 18:02:03 UTC 2009:

It may also be useful to know that if you use /var/www (the default) as your DocumentRoot, you should make sure your rails app is chown to 'nobody'.

Mark commented Mon Aug 16 21:01:07 UTC 2010:

Great tutorial series, thanks for all the great info!

I am having an issue with serving pages other than index.html. I'm currently serving only from my slice's IP address. The only page it serves are the static files in the app/public folder.

My vhost file looks like:

<virtualhost> ServerName {SLICE_IP} ServerAlias {SLICE_IP} DocumentRoot /mypath/publichtml/testapp/public <directory /> AllowOverride all Options -MultiViews </directory> </virtualhost>

One thing to note, I've tried to locate a .htaccess file in the project but can't seem to find one. Could this be the issue?

Thanks in advance.

volkan commented Sun Jul 24 12:37:34 UTC 2011:

Hi,

When i navigate to my ip address i see the "it works!" message instead of my app. However, if I add the port number 3000 at the end of the ip address i get to my app.

Is there something I need to do to get this working?

Also, when I start my app, it starts using Webrick, is this normal or it should be using something else?

Kindest Regards, Volkan

Jered commented Mon Jul 25 21:00:30 UTC 2011:

Volkan, what appears to be happening is that the app is running fine, but passenger isn't forwarding the connection from the web server back to the app. You might look over the steps from this article and the previous one to make sure passenger installed okay and the rewrite module is enabled as well.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)