How To Deploy Phusion Passenger To A Subdirectory, Routing Errors, And Restarting

With the addition of Phusion Passenger to all our servers, deploying Ruby on Rails applications is now much easier. Phusion Passenger is now the preferred way to use Rails at Site5, but FastCGI is still provided. For most users, adding Phusion is as simple as creating a blank .htaccess file in your application “public” directory with the following lines:

PassengerEnabled on
PassengerAppRoot /path/to/your/application/directory

As an example, if your application root directory is located at “/home/username/my_apps/forum” then your PassengerAppRoot line would look like this:

PassengerAppRoot /home/username/my_apps/forum

It is very important that you put the path to your application root directory, not the path to the “public” directory within your application directory.

If you were recently using FastCGI, you need to remove the previous FastCGI mod_rewrite lines in your .htaccess file as they will interfere with the Passenger configuration. The FastCGI lines will look something like this:

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

Deploying Phusion Passenger To A Sub Directory

NOTICE: The section below involving modifications to the environment.rb file are not needed if you are deploying to a subdomain (rails.domain.tld) or a main domain (domain.tld). It is only needed if you are deploying to a sub-directory (domain.tld/subdirectory/).

You will need to add one line to the environment.rb file for your application (if you have not already). This file is found in the “config” directory in your application directory. Here is the line you need to add:

config.action_controller.relative_url_root = "/approot"

The “approot” line should be the name of the root of your application directory. This line must be added within the config block or it will not work correctly. The start of the config block will look like this:

Rails::Initializer.run do |config|

The “approot” part would be what you want to use as the root URL of your application. For example, let’s say your application resides in “/home/user/mygreatapp/” and you want it to be accessible via http://yourdomain.tld/approot/.

You would first make a symbolic link in your public_html directory (/home/user/public_html/) called “approot” that would point to the “public” directory in your application. The symbolic link creation would look like this:

ln -s /home/user/mygreatapp/public /home/user/public_html/approot

So, now that you have added the necessary lines to your .htaccess file and modified your application accordingly, you should now be up and running with Phusion Passenger.

Routing Errors & 404 Code

If you receive a 404 “page doesn’t exist” error when accessing your application, this is most likely due to your routes not being configured properly (config/routes.rb file). The Rails log file is usually very informative and will tell you why your application is not working correctly. Check the log file in the “log” directory for more information. The routing error will probably look something like this:

ActionController::RoutingError (No route matches "/approot/" with {:method=>:get}):

If you receive an error similar to that, you will need to adjust your routes accordingly to accommodate the base URL. You can get a good overview of your routes by running “rake routes” from anywhere within your application directory.

Restarting Your Application

Since adding Phusion, we have received quite a few requests regarding the best way to restart a Rails application. Phusion has provided a very simple mechanism for accomplishing this. All you need to do is create or modify a “restart.txt” file within the “tmp” directory of your application. If your application is located at “/home/user/mygreatapp/” you would simply run:

touch /home/user/mygreatapp/tmp/restart.txt

In previous versions of Phusion, the restart.txt file would be automatically deleted after the restart was accomplished, but this is no longer the case. Phusion just checks if the file was modified, so you can keep the empty file there and touch it whenever you want to force a restart.

For more tips and tricks on using Phusion Passenger, please check out the official Phusion documentation. Site5 offers both Phusion Passenger web hosting and full support for Ruby On Rails.


Similar Posts

8 Responses to “How To Deploy Phusion Passenger To A Subdirectory, Routing Errors, And Restarting”

  1. bbebop  on April 15th, 2009

    Based on my recent experience, these instructions work if you’re deploying a subdirectory (as in http://mysite.com/subdirectory), but won’t work if the subdirectory is actually a subdomain (as in http://subdirectory.mysite.com). In the latter case, “RailsBaseURI /approot” and ‘onfig.action_controller.relative_url_root = “/approot”‘ weren’t necessary.

    The other thing that caused problems were the rewrite rules in my .htaccess file. They worked with fcgi, but interfere with Phusion Passenger.

    Hope this helps!

  2. Graham McMillan  on April 16th, 2009

    hey bbebop,

    Those are all good points and I have just updated the blog post with those details.

    Thanks!

    -Graham

  3. matthewfarmery  on April 16th, 2009

    Awesome article, and not before time, I tried several attempts to getting typo blog software running, was about to give up, but thanks to the above, I have finally got typo 5.3 working

    my only mistake was, I pointed config.action_controller.relative_url_root = “/approot”
    to the application root, not the sub domain / directory, as soon as I did that, I will welcomed by the login screen

    http://matthewfarmery.net/

    so again awesome article, I would suggest the above gets posted in the ruby forum on site5

  4. Ben Welch-Bolen  on April 17th, 2009

    I’m glad to hear that Matthew, yep we will try to get more of this info in the forum and we are working on a new customer oriented wiki or help project.

    Thanks! Ben

  5. ktulhu  on April 18th, 2009

    Nice article, thanks.
    Seems something wrong with javascript on this page,
    it reloads every few seconds at my browser.

  6. Ben Welch-Bolen  on April 18th, 2009

    That is odd, we tested it on every browse we have and no problems. What browser and version are you using?

    Thanks, Ben

  7. explainer  on April 29th, 2009

    I am attempting to deploy two rails apps to my server, named ‘depot’ and ‘audit’. I am using capistrano to deploy each, and deploy.rb for each is identical except for the :application and :repository settings.

    I am including my .htaccess file for ‘audit’ below:
    ==================
    PassengerEnabled on

    RailsBaseURI /audit
    ==================
    The .htaccess file for ‘depot’ differs only in the contents of /approot.

    I have added the
    config.action_controller.relative_url_root = “/audit” to my environment.rb, and have done the same for depot.

    What am I missing?

  8. Graham McMillan  on April 30th, 2009

    Your .htaccess file looks fine, so it should be working. It sounds like there may be some other problem (gems missing?). Could you send in a ticket so we can take a look at your specific account to determine the problem?

    Also, after you submit the ticket, please e-mail me at gmcmillan _AT_ site5 DOT com with the ticket number and I’ll take a look, too.

    Thanks,

    Graham