Configuring Passenger (mod_rails) on SliceHost with Ubuntu 7.10
I had an opportunity to play around with Phusion Passenger (mod_rails) which was just released yesterday. Before yesterday all that was available was an introductory video showing Passenger being configured, and I was quite impressed by it’s ease. Clearly one of the problems with Rails as a major platform right now is it’s hosting situation. Currently the best solution is to proxy HTTP requests from Apache or Nginx to a cluster of mongrels, which is tricky to set up and somewhat tedious - certainly not as as easy as a mod_php in Apache where things “just work”. Phusion set out to overcome this problem and release a module for Apache that hosts Rails applications easily. Here is what took me from a clean Ubuntu 7.10 slice from SliceHost to a working server with Passenger:Starting OutTo start out, I used the following *excellent* SliceHost articles to get some of the basic packages installed such as Ruby and MySQL:Ubuntu Setup - Part 1Ubuntu Setup - Part 2MySQL and RailsApache and PHPApache Virtual HostsVirtual Hosts & PermissionsNote my permissions gotcha below - you may not want to strictly follow their permissions advice (where the global does not have read permission).I also installed sqlite3 as this is now the default database package used in Rails 2.0:
$ sudo aptitude install sqlite3$ sudo gem install sqlite3-ruby
Installing the Passenger Apache ModuleUsing the simple instructions on the Passenger website, I ran:
$ sudo gem install passenger$ passenger-install-apache2-module
The passenger-install-apache2-module binary walks you through the installation of the Passenger. On the first run, it told me I didn’t have some software that was required:
* GNU C++ compiler... found* Ruby development headers... found* OpenSSL support for Ruby... found* RubyGems... found* Rake... found at /usr/bin/rake* Apache 2... found at /usr/sbin/apache2* Apache 2 development headers... not found* Apache Portable Runtime (APR) development headers... not found* fastthread... foundSome required software is not installed.But don't worry, this installer will tell you how to install them....* 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.
Clearly the installer detected I was running Debian-based Linux and told me exactly what to do. No compile errors or nasty dependency issues, just simple and clear instructions. After installing the requisite software the installer compiled the Apache module and spit out this:
The Apache 2 module was successfully installed.Please edit your Apache configuration file, and add these lines:LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/ext/apache2/mod_passenger.soRailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/passenger-spawn-serverRailsRuby /usr/bin/ruby1.8After you restart Apache, you are ready to deploy any numberof Ruby on Rails applications on Apache, without any furtherRuby on Rails-specific configuration!
Note that it didn’t find or attempt to find where apache modules are installed as the actual compiled modules is kept within the Ruby gems directory, it just told me what lines to include in my Apache configuration file. The last part of the installation says:
Deploying a Ruby on Rails application: an example.Suppose you have a Ruby on Rails application in /somewhere.Add a virtual host to your Apache configuration file,and set its DocumentRoot to /somewhere/public, like this:<VirtualHost> ServerName www.yourhost.com DocumentRoot /somewhere/public</VirtualHost>And that's it!
Now this is pretty cool… Passenger doesn’t even require virtual hosts to configure themselves as Rails applications. It will automatically detect a Rails application (I’m unsure of the actual criteria here) based on the DocumentRoot that you provide.So I added the necessary lines specified above to /etc/apache2/apache2.conf to load the module. Note that some deployments of Apache have their own way of managing modules; in Ubuntu there is a mods-available directory with individual configuration files for each module. You could split up the above commands to follow the convention for your system of choice, but for my purposes I just dumped the three lines into apache2.conf.Then I created a new Rails application and pointed DocumentRoot to the Rails public directory. Restart apache, and the site should then immediately work with no further configuration.Permissions Gotcha - Important!If you get:
RAILS_ROOT is not a directory.
The first time I got to this point I was running into an extremely obscure problem - Rails was reporting that RAILS_ROOT wasn’t a directory. After some debugging which involved hacking up the gem source code of both Rails and Passenger, I realized it was a permissions issue (what do you know). Passenger reports that it will run your Rails application as whatever user owns environment.rb. However, it will set the effective group to that user’s primary UNIX group, NOT the group that environment.rb is owned by. In my case, I set the group of all of the web files to www-data and no read or execute permissions whatsoever for other. I figured this would be sufficient since Apache runs as www-data. But in my case when Passenger got spawned, it got spawned running as bhughes:bhughes NOT bhughes:www-data, so it effectively couldn’t find the RAILS_ROOT directory and didn’t think it existed. You can design your permissions scheme however you want, but make sure you keep this in mind. I have an enhancement in Passenger’s ticketing system for fixing this and allowing you to manually set the effective group (you already can explicitly set the effective user through the RailsUser option).More Passenger ConfigurationThe Passenger User’s Guide is pretty good and includes some more specific configuration options you can choose, but the default appears to work fine. The Rails environment is by default set to “production” though this can be changed (sadly, only server-wide, not on a virtual-host basis - but there is an enhancement in the issue tracker for this feature).Restarting Your AppRestarting a Rails app hosted by Passenger is easy - simply touch a file called tmp/restart.txt within the Rails application root:
$ touch tmp/restart.txt
CapistranoBrian Ketelsen pointed out this Capistrano configuration which uses the touch tmp/restart.txt approach to restart and removes the .htaccess file which can cause issues:
namespace :deploy do task :start, :roles => :app do end task :stop, :roles => :app do end task :restart, :roles => :app do run "touch #{release_path}/tmp/restart.txt" end task :after_update_code, :roles => :app do run "rm -rf #{release_path}/public/.htaccess" endend
Diagnosing ProblemsOne of the absolutely wonderful things about Passenger is that problem diagnostics (regarding starting your Rails app) is fairly easy because Passenger includes its own nice error pages:Database Error ExampleFramework Initialization ExampleMemory ManagementOne of the issues with mongrel or thin is that they are constantly running and sucking up memory, even for sites that get very little traffic. This leads to a situation where hosting very low-traffic sites is very inefficient from a system resources perspective. This isn’t the case with PHP where memory usage spikes during traffic but is otherwise unused, allowing multiple sites to leverage the on and offs and maintain a pretty efficient hosting platform as far as memory is concerned. One of the great feature of Passenger is that you can configure it to automatically shut down your Rails application if it is idle for a certain period of time. Doing this obviously exerts a cost then on the next hit, but this may be an acceptable tradeoff for a server hosting many low-traffic sites. The configuration option is RailsPoolIdleTime and there is more information on this in the user guide.ConclusionObviously my introduction here doesn’t go in-depth into performance and ability of Passenger to scale. Time will tell as people begin using this whether it is a viable solution for Rails hosting, but for me installing this and getting it to work was dirt simple, at least compared to the mod_proxy_balancer setup with mongrel clusters I have used in the past. I’m not quite ready to actually host my sites using this but I’ll definitely be keeping my eye on this - it could be a major step forward in the proliferation of Rails hosting.


April 14th, 2008 at 1:13 pm
Ben, Excellent write up. Thanks for working out some of the kinks and documenting them so thoroughly.
I’m quite excited about passenger for many of the reasons you suggest. I have quite a number of low traffic Rails sites. And would prefer this solution too paying for a bigger slice.
April 14th, 2008 at 9:04 pm
Great article, looks like an excellent solution.
April 16th, 2008 at 1:51 pm
Ben, thanks, I used this to set up my slice just Sunday… One thing that would be nice to add would be the capistrano changes necessary to support this config. I found I had to remove Deprec completely, and strip 99% of my cap recipes.
namespace :deploy do
task :start, :roles => :app do
end
task :stop, :roles => :app do
end
task :restart, :roles => :app do
run "touch #{release_path}/tmp/restart.txt"
end
task :after_update_code, :roles => :app do
run "rm -rf #{release_path}/public/.htaccess"
end
end
April 17th, 2008 at 12:45 am
I had a situation where it couldn’t read my javascripts, css, image files. All I had to do is the following
ServerAdmin localhost
ServerName http://www.test_mod_rails.com
DocumentRoot /var/www/testapp/public
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
RailsBaseURI /
April 17th, 2008 at 6:37 pm
Ben, thanks for the great write up. I was able to build by slice and get passenger running with minimal trouble.
April 22nd, 2008 at 3:41 am
Hi Ben,
Thank you for the tips about installing Passenger. I just successfully set up Passenger on Ubuntu 7.10 with few problems. This is great.
-Nick
May 4th, 2008 at 4:05 am
Hi All,
I’m not sure what I’m doing wrong…I have everything installed and then I created a simple rails test app. I have the following configs:
in: /etc/apache2/sites-available/test.mysite.com:
ServerName test.mysite.com
DocumentRoot /home/me/public_html/test.mysite.com/testapp/public
in: /etc/apache2/apache2.conf:
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.3/ext/apache2/mod_passenger.so
RailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.3/bin/passenger-spawn-server
RailsRuby /usr/bin/ruby1.8
ServerName mysite
NameVirtualHost *:80
I do the following:
sudo /etc/init.d/apache2 restart
Please confirm that I don’t need to start rails using ruby script/server if I’m using passenger.
Problems:
I can’t see passenger or ruby anywhere in a ps aux - is this ok, I thought I should at least see passenger?
The production.log in my rails app is not getting written to (and its not a permissions issue).
The only thing I see in /var/log/apache2.error is:
/home/me/public_html/test.mysite.com/testapp/public/.htaccess: Invalid command ‘RewriteEngine’, perhaps misspelled or defined by a module not included in the server configuration.
Please give me a pointer of where to look next if you have a sec.
Thanks,
Mark
May 5th, 2008 at 2:27 am
Hi Ben,
I have a comment that is awaiting moderation, but I’ve solved the problem - so you can get rid of this post as well as my last if you like. The fix was to simply remove the .htaccess file that rails generates in the app’s public directory. Once I removed that file, the rails app was accessible at my domain name.
Thanks,
Mark
May 14th, 2008 at 2:44 am
Thanks Mark for your response. I too had the error related to .htacces and after deleting that file, things are cool now. I wonder if this is a Rails 2.0 thingy or something to do with my Ubuntu (Hardy Heron) install. Did not see any other post on the net for this error message.
May 14th, 2008 at 4:40 am
I’m getting an error when I start my app:
The application /home/mark/public_html/test.choresforrewards.com/c4r requires Ruby on Rails version 1.2.2, which is not installed. Please install it with the following command:
sudo gem install rails –version 1.2.2
I think the problem is that my app was developed on 1.2.2 and I can’t update my linux server with:
sudo gem install rails –version 1.2.2
So, I tried to update my local workstation to 2.0.2, but when I do gem list on my local workstation I get:
rails (2.0.2, 1.2.2)
so I think even though 2.0.2 is on my local workstation, my app is still using 1.2.2.
How do I change my local workstation app to use 2.0.2?
Once I do that, I can tar it up and send it to my linux server and I suspect it will run.
Thanks,
Mark
May 15th, 2008 at 3:41 pm
Thanks for the good guide. I’ve deployed my rails app in a short time using mod_rails.
Unfortunately I’m experimenting a severe problem.
The rails app works fine using firefox or konqueror, but with internet explorer I’m unable to perform the login operation.
I’m using restfull_authentication plugin under rails 2.0.2.
The strange thing is that, hosting the site under webrick, ie works fine.
Any hint?
May 17th, 2008 at 3:15 pm
What if i run typo on my own machine while I don’t have a static IP. I just wanna share my typo blog in local area network. Under this situation, how to configure my Apache configuration file? Cause I don’t have something like “ServerName http://www.yourhost.com“. Looking forward to your reply…
May 26th, 2008 at 8:50 am
It was very simple for me today and i had no trouble on dreamhost, however i want to add basic authentication using the .htaccess file
ie.
AuthName “Dialog prompt”
AuthType Basic
AuthUserFile /home/xxxxxxxx/.htpasswd
Require valid-user
When i add this, rails intercepts the request and processes it - yeilding a 404 as there is no route to handle index.html
Is there another way to accomplish this? (i want the entire site behind basic authentication on dreamhost using mod_rails)
May 26th, 2008 at 10:39 am
I think i have answered my question
http://www.modrails.com/documentation/Users%20guide.html#RailsAllowModRewrite
But i don’t think i can configure this on dreamhost?
May 30th, 2008 at 12:37 pm
Hi all, today I probe install passenger in Ubuntu 8.04
If just run #passenger-install-apache2-module, bash says: “unknown command”
Few hour later I run #/var/lib/gems/1.8/gems/passenger-1.0.5/bin# ruby passenger-install-apache2-module
and success!
(sorry for my english, I am from Russia)
June 7th, 2008 at 3:57 am
Thanks for great artcle! from Japan.
I could deply my Typo blog system according to your entry and passenger official instruction.
At first, my application can’t read subfolder, but deleting .htaccess solves this problem.
There are a few problems(sometimes internal error occurs, etc) on my Typo blog yet, but it will be solved soon…
Anyway, great thanks!
June 17th, 2008 at 7:50 am
Passenger is running smoothly on my server and I successfully installed a rails application (redmine) to it.
But does anybody know how to run two or more rails applications on the same server using passenger?
Like this:
http://192.168.0.100/tracks
http://192.168.0.100/redmine
Any hint would be appreciated.
Thanks