zh-CN:Ruby on RailsRuby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language. It is intended to be used with an Agile development methodology that is used by web developers for rapid development.

This document describes how to set up the Ruby on Rails Framework on an Arch Linux system.

Ruby on Rails requires Ruby to be installed, so read that article first for installation instructions.

Configuration

Rails is bundled with a basic HTTP server called WeBrick. You can create a test application to test it. First, create an application with the rails command:

$ rails new testapp_name

This makes a new folder in your current working directory. Next start the web server. It listens on port 3000 by default:

$ cd testapp_name
$ rails server

Finally open your server address on port 3000 in your web browser. For example, if you are working on your local machine, visit http://localhost:3000.

A test-page should shown greeting you "Welcome aboard".

Application servers

While the default Ruby On Rails HTTP server (WeBrick) is convenient for basic development it is not recommended for production use. Generally, you should choose between installing the Phusion Passenger module for your webserver (Apache or Nginx), or use a dedicated application-server (such as Mongrel or Unicorn) combined with a separate web-server acting as a reverse proxy.

Mongrel

Mongrel is a drop-in replacement for WeBrick, that can be run in precisely the same way, but offers better performance.

First install the Mongrel gem:

# gem install mongrel

Then start it using:

# mongrel_rails start

Alternatively, you can just run "ruby script/server" again, as it replaces WeBrick by default.

Databases

Most web applications will need to interact with some sort of database. ActiveRecord (the ORM used by Rails to provide database abstraction) supports several database vendors, the most popular of which are MySQL, SQLite, and PostgreSQL.

SQLite

SQLite is the default lightweight database for Ruby on Rails. To enable SQLite, simply install sqlite3.

A gem with some native extensions is required, probably best installed as root:

# sudo gem install mysql

You can generate a rails application configured for MySQL by using the -d parameter:

$ rails new testapp_name -d mysql

You then need to edit config/database.yml. Rails uses different databases for development, testing, production and other environments. Here is an example development configuration for MySQL running on localhost:

Option C: The Perfect Rails Setup

Nginx: A fast and lightweight web server with a strong focus on high concurrency, performance and low memory usage.

Passenger (a.k.a. mod_rails or mod_rack): Supports both Apache and Nginx web servers. It makes deployment of Ruby web applications, such as those built on Ruby on Rails web framework, a breeze.

Ruby Enterprise Edition (REE): Passenger allows Ruby on Rails applications to use about 33% less memory, when used in combination with REE.

Ruby Version Manager (RVM): A command-line tool which allows you to easily install, manage, and work with multiple Ruby environments from interpreters to sets of gems. RVM lets you deploy each project with its own completely self-contained and dedicated environment —from the specific version of ruby, all the way down to the precise set of required gems to run your application—.

Step 1: RVM

. http and nobody are the users related to Nginx and Passenger, respectively.

Note: Maybe adding the 'nobody' user to the 'rvm' group is not necessary.

Step 2: Rubies

Once you have a working RVM installation in your hands, it is time to install the Ruby Enterprise Edition interpreter

Note: During the installation of Ruby Enterprise Edition patches will be applied. Consider installing base-devel beforehand.

$ rvm install ree

. Also take the chance to include other interpreters you want to use, like the last Ruby version

$ rvm install 1.9.3

Advice

I have found useful to delete the 'global' gemsets of the environments that have web applications. Their gems were somehow interfering with Passenger. Do not do

$ rvm ree do gemset delete global
$ rvm 1.9.3 do gemset delete global

now, but consider this later if you encounter complications.

Step 3: Nginx with Passenger support

Do not install Nginx via pacman. This web server does not support modules as Apache, so it must be compiled from source with the functionality of mod_rails (Passenger). Fortunately this is straightforward thanks to the passenger gem. Get it:

$ rvm use ree
$ gem install passenger

. The gem will be put into the 'default' gemset. Now execute the following script:

Note: The current nginx package (1.2.1-1) in the official repos actually was compiled with the passenger module. So you can install it via pacman and skip this step. The config files are stored in /etc/nginx/conf/.

$ rvmsudo passenger-install-nginx-module

. It will download the sources of Nginx, compile and install it for you. It will guide you through all the process. (The default location for Nginx is /opt/nginx.)

Note: if you encounter a compilation error regarding Boost threads, see this article: [2]

After completion, the script will require you to add two lines into the 'http block' at /opt/nginx/conf/nginx.conf that look like:

For everything that is not Ruby, use Nginx as usual to serve static pages, PHP and Python. Check the wiki page for more information.

To enable the Nnginx service by default at start-up just add nginx to the DAEMONS array in /etc/rc.conf:

DAEMONS=(ntpd syslog-ng ... nginx)

Note: It is possible that your Nginx installation has not come with an init script; check your /etc/rc.d/ directory for a file called nginx, if that is your case manually create it. Help yourself with Nginx/Init_script. If you installed nginx to another location, such as /opt/nginx, you will need to edit the init script accordingly.

Step 4: Gemsets and Apps

For each Rails application you should have a gemset. Suppose that you want to try RefineryCMS against BrowserCMS, two open-source Content Management Systems based on Rails. Then you should do:

Passenger for Nginx and Passenger Standalone

Observe that the passenger gem was installed three times and with different intentions; in the environments

ree => for Nginx,

ree@refinery => Standalone, and

1.9.3@browser => Standalone.

The strategy is to combine Passenger for Nginx with Passenger Standalone. One must first identify the Ruby environment (interpreter plus gemset) that one uses the most; in this setup the REE interpreter and the default gemset were selected. One then proceeds with setting up Passenger for Nginx to use that environment. All applications that are to use a different Ruby version and/or gemset can be served separately through Passenger Standalone and hook into the main web server via a reverse proxy configuration.

Step 5: .rvmrc files and ownerships

This step is crucial for the correct behaviour of the setup. RVM seeks for .rvmrc files when changing folders; if it finds one, it reads it. In these files normally one stores a line like

rvm <ruby_version>@<gemset_name>

so the specified environment is set at the entrance of applications' root folder.

Create /srv/http/refineria/.rvmrc doing

sudo sh -c 'echo "rvm ree@refinery" > /srv/http/refineria/.rvmrc'

, and /srv/http/navegador/.rvmrc with

sudo sh -c 'echo "rvm 1.9.3@browser" > /srv/http/navegador/.rvmrc'

You have to enter to both application root folders now, because every first time that RVM finds a .rvmrc it asks you if you trust the given file, consequently you must validate the two files you have just created.

These files aid the programs involved to find the correct gems.

Apart, if applications' files and folders are not owned by the right user you will face database write-access problems. The use of rvmsudo produces root-owned archives when generated by Rails; in the other hand, nobody is the user for Passenger —if you have not changed it—: who will use and should posses them. Fix this doing

$ sudo chown -R nobody.nobody /srv/http/refineria /srv/http/navegador

Step 6: Reverse proxies

You have to start the Passenger Standalone web servers for your applications. So, do