Install Phusion Passenger (a.k.a. mod_rails) on cPanel Server

words by Brian Racer

Although cPanel has built in support for running Ruby or Rails apps, it uses Mongrel as the server and doesn’t allow more than one instance per user. That makes it pretty useless for any application that gets even a moderate amount of traffic. Instead we can install Phusion Passenger (a.k.a mod_rails), which in my opinion is a much nicer solution anyway.

First we need to make sure Ruby is installed via a cpanel script:

sudo /script/installruby

Now we can install the passenger gem:

sudo gem install passenger

Next, compile the apache2 module

sudo passenger-install-apache2-module

The installer may tell you that the the Apache development headers are needed and will suggest ‘yum install httpd-devel’. Since cPanel compiles it’s own version of apache, yum is configured to ignore that package. That is OK, because the program we need is already installed, we just have to tell Passenger where to find it.

APXS2=/usr/local/apache/bin/apxs PATH=$PATH:/usr/local/apache/bin passenger-install-apache2-module

Everything should go OK this time, and the installer will give you a few lines to add to your apache config file. It’s best practice with cPanel not to put these in your main httpd.conf, but rather the pre_main_global.conf:

vi /usr/local/apache/conf/includes/pre_main_global.conf
 
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-X.X.X/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-X.X.X
PassengerRuby /usr/bin/ruby

Now we need to setup passenger to run on a per virtual host basis. Open up the httpd.conf file and find the virtual host you want to run a Rails app and add this line:

Include "/usr/local/apache/conf/userdata/std/2/username/domain_name/*.conf"

Replace username with the username of the account.

Now we need to create the directory we just specified, and also create a configuration file letting passenger know it should load for this host:

mkdir -p /usr/local/apache/conf/userdata/std/2/username/domain_name/
vi /usr/local/apache/conf/userdata/std/2/username/domain_name/rails.conf
 
RailsBaseURI /

To make sure those files load, run this:

/scripts/ensure_vhost_includes --user=username

We need to make sure cPanel records the changes we have for when it rebuilds those files, so run the following two commands:

/usr/local/cpanel/bin/apache_conf_distiller --update
/usr/local/cpanel/bin/build_apache_conf

We can now restart apache:

/etc/init.d/httpd restart

Since by default the Apache Document Root for each host is /home/username/public_html, you will probably need to symlink that to your applications public directory:

ln -s /home/username/railsapp/public /home/username/public_html

To restart that application, you just need to touch the restart.txt file:

touch /home/username/railsapp/tmp/restart.txt

And there you have it, a working high performance rail application server on cPanel! For more information on tuning the Passenger configuration, read the complete docs.

  • Pingback: Jetpack Flight Log » Munin Graphs for Phusion Passenger (a.k.a. mod_rails)()

  • Pingback: AK47 » links for 2009-11-28()

  • Pingback: Install Phusion Passenger (a.k.a. mod_rails) on cPanel Server | Kroko()

  • http://gerente.zocka.com.br Deguir

    Thanks for the tutorial was very helpful.

    But me only worked when I changed the DocumentRoot in Apache’s httpd.conf file to the directory that is the application Rais.

    Only with “ln-s /home/username/railsapp/public/home/username/public_html” simply does not work, returns the error:

    Can not start Ruby on Rails application
    The directory “/home/username” does not appear to be a valid Ruby on Rails application root.

    You know what would be wrong to work without having to change the setting that made the cPanel?
    Thank you!

  • http://gerente.zocka.com.br Deguir

    Thanks for the tutorial was very helpful.

    But me only worked when I changed the DocumentRoot in Apache’s httpd.conf file to the directory that is the application Rais.

    Only with “ln-s /home/username/railsapp/public/home/username/public_html” simply does not work, returns the error:

    Can not start Ruby on Rails application
    The directory “/home/username” does not appear to be a valid Ruby on Rails application root.

    You know what would be wrong to work without having to change the setting that made the cPanel?
    Thank you!

  • http://www.l-lysine.info Daisy Jackson

    It is also easy to backup and transfer all your websites from one server to another server if you have cPanel installed,”,

  • http://www.l-lysine.info Daisy Jackson

    It is also easy to backup and transfer all your websites from one server to another server if you have cPanel installed,”,

  • http://www.wet-saw.org Diego Gray

    if you are going to get a VPS server make sure that it has cPanel coz it makes server maintennance easier.*”*

  • http://www.wet-saw.org Diego Gray

    if you are going to get a VPS server make sure that it has cPanel coz it makes server maintennance easier.*”*

  • Mladjan Antic

    When I want to start passenger with “passenger start” I get this error:

    cl.ly/453x2O1g2z2e1y211E3Z

    I have gcc compiler: http://cl.ly/0E2I1B2h0B45312X1q0I

    What to do now? :(

  • http://jetpackweb.com Brian Racer

    Maybe related to this? http://ubuntuforums.org/showthread.php?t=986408 I’m not sure, my article was using it with Apache, not nginx.

  • Mladjan Antic

    I want to use Passenger with Apache, and I installed all from your article, but app don’t work. After that, I tried to start app from app dir with command “passenger start” and this starts to install nginx and this fucked my server :|

  • http://jetpackweb.com Brian Racer

    running `passenger start` is trying to run passenger in standalone mode which requires nginx. You need to install it as an apache module. They have very good docs on setting that up, check them out at http://www.modrails.com/documentation/Users%20guide%20Apache.html

  • Mladjan Antic

    I do this again and this is installed as apache module.I installed this just for one user, one domain, http://www.taksiko.com
    But, if I go to some different user, for example http://www.cweb.rs I will get error “Ruby on Rails application could not be started”. But when I go to http://www.taksiko.com, I get 403 Forbiden page. Of course, directory is readable (755).

    Where is the problem?

  • Mladjan Antic

    This works for me guys now. Sorry for panic :)

  • Anonymous

    Hello All,
    This turned out to be a great post, thanks! But I did hae some problems, namely passenger wouldn’t compile with my cpanel. I followed the directions here and it worked great! Enjoy…

    http://groups.google.com/group/phusion-passenger/browse_thread/thread/19cdb1493e8ba6ad

  • TXGruppi

    I have the same problem…

    Any solution?

  • Mladjan Antic

    Do this again, it must to work. I tried again, and now works.

  • TXGruppi

    I erased all ‘ruby’ and ‘rails’ from the server then I did this tutorial again and now it works…

    thanks for the tip.

    PS: Sorry about the english if it’s wrong

  • http://txgruppi.com TarcĂ­sio Gruppi

    If the Rails App will be in a sub uri (http://domain.com/railsapp) the ‘RailsBaseURI /railsapp’ can be put in the /home/username/public_html/.htaccess

    PS: Sorry about the english

  • Pingback: Resources for setting up Passenger and RVM on a CentOS cPanel / WHM Server | zzzzzzzzz()