Ruby on Rails Guide

From Livedoc - The Documentation Repository
Revision as of 19:03, 4 July 2008 by Brandon Vargo (talk | contribs) (Introduction)
Jump to: navigation, search

This is a guide to running ruby on rails applications on the TJ webserver. This is not a general guide to creating and running ruby on rails applications. The ruby on rails website has plenty of information on getting started with ruby on rails.

Logging into www.tjhsst.edu

Log into www.tjhsst.edu just as you would any other host with ssh. If accessing www from a workstation or remote, you should automatically be authenticated. If accessing www from another host, or if you are not automatically authenticated, use the same username and password that you would use to access a workstation.

Suggested Configuration for our Webserver

As of July, 2008, our webserver is running version 2.1.0 of ruby on rails and ruby 1.8.5. Please ensure your application works with this configuration.

When deploying your rails application, it is desirable to not have your entire application, including configuration files, to be available to everyone. For this reason, when building a rails application, we suggest that you protect access to your files with an .htaccess file, or actually store your application in a directory that is not in your web-docs directory.

Protecting your Application with an .htaccess File

For this example, let's assume that I have my rails application, rails, which I want to appear at http://www.tjhsst.edu/~user/rails/. Instead of putting my application in a directory named rails, I am going to put it in a directory named rails-app.

Next, in order for my application to appear at /~user/rails/, create a symbolic link to the public directory under rails-app, naming the link rails.

# In my web-docs directory
ln -s rails-app/public/ rails

Next, create an .htaccess file similar to the following in your rails-app directory:

Order allow,deny
Deny from *

Now our application directory is safe, so the code and configuration details (including the database username and password) cannot be viewed in a web browser.

Making your Website Accessible through the Webserver

Another .htacccess file is necessary in order to correctly redirect webserver requests into your rails application. Place the following .htaccess file example in your applications' public directory. If you are trying to configure an existing application that already has an .htaccess file, do not replace the existing file, but merge the differences between the two files in order to not overwrite any custom modifications for your particular application.

Example .htaccess file:

RewriteBase (URL of your application)
# Example:
#    RewriteBase /~user/myrailsapp

# Make sure the server knows about .fcgi scripts
AddHandler fcgid-script .fcgi

# Follow symbolic links and execute CGI fils
Options +FollowSymLinks +ExecCGI

# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests
#
# Example:
#   RewriteCond %{REQUEST_URI} ^/notrails.*
#   RewriteRule .* - [L]

# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
#
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
# Please refer to the "Server Process Instances" section at the bottom of the
# ruby on rails guide in Livedoc (http://www.tjhsst.edu/admin/livedoc/)
# before uncommenting the following line.
#RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

# In order to run rails in a mode other than production
#
# Example:
#   SetEnv RAILS_ENV development

# In case Rails experiences terminal errors, specify the error to display
#
# Example:
#   ErrorDocument 500 /500.html
#   ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
#
# If this is not specified, the generic error page will be displayed

File Permissions

Ruby on rails needs write permissions to certain directories in the application. Execute the following command from within your application's main directory in order to enable write access.

# From your application's main directory
find log tmp -type d -exec fs sa {} www-data write \;

If you find that other directories need to have writable from the webserver, add the names of the directories to the list between find and -type.

Other Ruby on Rails Notes

Gem Installation

If you need any ruby gems installed on our server, contact a sysadmin. In most cases, we are happy to install the gems needed by your application. Alternatively, you can install the gems locally for your installation.

Server Process Instances

In order to accelerate the performance of ruby on rails applications, we use something called fcgid (a binary-compatible FastCGI implementation) to keep ruby instances running in the background for a certain timeperiod. As a result, if you make certain modifications to your rails configuration, such as changing the database username and password, these changes may not take effect immediately. The server process will not shutdown as long as there is traffic viewing your page, so allow your page to time out, or make the noted change in the second .htaccess file above. If you make the modification in the .htaccess file, please reverse the change as soon as possible. While the change is in place, your site will take longer to service requests, and our server will experience an additional load for each request. The sysadmins can reset the server process, so please contact a sysadmin as soon as possible in order to speed-up the server for everyone.