Difference between revisions of "Ruby on Rails"

From ArchWiki
Jump to: navigation, search
(The Perfect Rails Setup (unfinished).)
m
Line 8: Line 8:
 
Ruby on Rails requires [[Ruby]] to be installed, so read that article first for installation instructions.
 
Ruby on Rails requires [[Ruby]] to be installed, so read that article first for installation instructions.
  
== Installation via RubyGems (Recommended) ==
+
== Option A: Installation via RubyGems (Recommended) ==
 +
 
 
{{Box Note | If this command is run without being root (using sudo or otherwise), the gem will be installed into the home directory of the user.}}
 
{{Box Note | If this command is run without being root (using sudo or otherwise), the gem will be installed into the home directory of the user.}}
  
Line 16: Line 17:
 
  # gem install rails --no-ri --no-rdoc
 
  # gem install rails --no-ri --no-rdoc
  
===Updating gems===
+
=== Updating Gems ===
 +
 
 
gem is a package manager for Ruby modules, somewhat like pacman is to Arch Linux. To update your gems, simply run:
 
gem is a package manager for Ruby modules, somewhat like pacman is to Arch Linux. To update your gems, simply run:
 
  # gem update
 
  # gem update
  
== Installing via the AUR ==
+
== Option B: Installing via the AUR ==
 +
 
 
{{Warning|This is not recommended, as this might not include the latest Rails version, and additional dependencies may be introduced that may require you to run {{Codeline|gem install}} anyway.}}
 
{{Warning|This is not recommended, as this might not include the latest Rails version, and additional dependencies may be introduced that may require you to run {{Codeline|gem install}} anyway.}}
 
There is a {{Package AUR|rails}} package available in the [[AUR]]. Note that this is not in an [[Official Repositories|official repository]], so you will need to [[AUR#Build_the_package|build it manually]].
 
There is a {{Package AUR|rails}} package available in the [[AUR]]. Note that this is not in an [[Official Repositories|official repository]], so you will need to [[AUR#Build_the_package|build it manually]].
  
 
== Configuration ==
 
== 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 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:
  
Line 34: Line 38:
 
  $ rails server
 
  $ 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/
+
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".
 
A test-page should shown greeting you "Welcome aboard".
  
 
== Web servers ==
 
== Web servers ==
 +
 
While the default Ruby On Rails HTTP server (WeBrick) is convenient for basic development it is not recommended for production use. Here are some suitable alternatives:
 
While the default Ruby On Rails HTTP server (WeBrick) is convenient for basic development it is not recommended for production use. Here are some suitable alternatives:
  
 
=== Mongrel ===
 
=== Mongrel ===
 +
 
Mongrel is a drop-in replacement for WeBrick, that can be run in precisely the same way, but offers better performance.
 
Mongrel is a drop-in replacement for WeBrick, that can be run in precisely the same way, but offers better performance.
  
Line 52: Line 58:
 
Alternatively, you can just run "ruby script/server" again, as it replaces WeBrick by default.
 
Alternatively, you can just run "ruby script/server" again, as it replaces WeBrick by default.
  
Generally, Mongrel is used in a production environment by running multiple instances of mongrel_rails, which are load-balanced behind an [[nginx]] or [[Apache]] reverse proxy. However, you might find Phusion Passenger (see below) a much simpler solution for running a production environment.
+
Generally, Mongrel is used in a production environment by running multiple instances of mongrel_rails, which are load-balanced behind an [[Nginx]] or [[Apache]] reverse proxy. However, you might find Phusion Passenger (see below) a much simpler solution for running a production environment.
  
=== Phusion Passenger (Recommended for production) ===
+
=== Apache/Nginx (using Phusion Passenger) ===
[http://www.modrails.com/ Passenger] also known as {{Filename|mod_rails}} is a module available for [[NginX]] and [[Apache]], that greatly simplifies setting up a Rails server environment.
+
 
 +
[http://www.modrails.com/ Passenger] also known as {{Filename|mod_rails}} is a module available for [[Nginx]] and [[Apache]], that greatly simplifies setting up a Rails server environment.
  
 
Start by installing the Passenger gem:
 
Start by installing the Passenger gem:
Line 61: Line 68:
  
 
If you are aiming to use [[Apache]] with Passenger, run:
 
If you are aiming to use [[Apache]] with Passenger, run:
  # passenger-install-apache2-module
+
# passenger-install-apache2-module
  
For [[NginX]]:
+
For [[Nginx]]:
  # passenger-install-nginx-module
+
# passenger-install-nginx-module
  
 
The installer will provide you with any additional information regarding the installation (such as installing additional libraries).
 
The installer will provide you with any additional information regarding the installation (such as installing additional libraries).
  
{{Note|See [[NginX#Ruby_.28Rack-based_and_Rails.29]] for more information on configuring a Rails/ Passenger/NginX web stack.}}
+
{{Note|See [[NginX#Ruby_Integration_.28Ruby on Rails and Rack-based.29]] for more information on configuring a Rails Nginx-Passenger web stack.}}
  
 
== Databases ==
 
== 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.
 
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 ===
 +
 
SQLite is the default lightweight database for Ruby on Rails. To enable SQLite, simply install {{Package Official|sqlite3}}.
 
SQLite is the default lightweight database for Ruby on Rails. To enable SQLite, simply install {{Package Official|sqlite3}}.
  
=== MySQL 5+ ===
+
=== PostgreSQL ===
{{Note|You must first install [[MySQL]] with the appropriate headers in {{Filename|/usr/include}} (just installing {{Package Official|mysql}} is fine) before attempting to install the Ruby MySQL extensions }}
+
 
 +
(Stub.)
 +
 
 +
Install {{Package Official|postgresql}}.
 +
 
 +
=== MySQL ===
 +
 
 +
{{Note|You must first install [[MySQL]] with the appropriate headers in {{Filename|/usr/include}} (just installing {{Package Official|mysql}} is fine) before attempting to install the Ruby MySQL extensions.}}
  
 
Please refer to [[MySQL]] on how to install MySQL Server.
 
Please refer to [[MySQL]] on how to install MySQL Server.
Line 100: Line 116:
 
If no errors are shown, then your database has been created and Rails can talk to your MySQL database.
 
If no errors are shown, then your database has been created and Rails can talk to your MySQL database.
  
== The Perfect Rails Setup ==
+
== Option C: The Perfect Rails Setup ==
  
 
* [http://www.archlinux.org/ Archlinux]: A simple, lightweight distribution. ;)
 
* [http://www.archlinux.org/ Archlinux]: A simple, lightweight distribution. ;)
Line 150: Line 166:
  
 
For everything that is not Ruby, use [[Nginx]] as usual to serve static pages, PHP and Python. Check the wiki page for more information.
 
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 nginx service by default at start-up just add {{Codeline|nginx}} to the {{Codeline|DAEMONS}} array in {{Filename|/etc/rc.conf}}:
 +
DAEMONS=(ntpd syslog-ng ... nginx)
  
 
=== Step 4: Gemsets and Apps ===
 
=== Step 4: Gemsets and Apps ===
  
 
== See also ==
 
== See also ==
 +
 
* [[Ruby]]
 
* [[Ruby]]
 
* [[Nginx]]
 
* [[Nginx]]
Line 160: Line 180:
  
 
== References ==
 
== References ==
* Ruby on Rails http://rubyonrails.org/download
+
 
* Mongrel http://mongrel.rubyforge.org/
+
* Ruby on Rails http://rubyonrails.org/download.
 +
* Mongrel http://mongrel.rubyforge.org.

Revision as of 10:36, 26 November 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Ruby 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.

Option A: Installation via RubyGems (Recommended)

Template:Box Note

# gem install rails

Building the documentation takes a while. If you want to skip it, append the parameters --no-ri --no-rdoc to the install command.

# gem install rails --no-ri --no-rdoc

Updating Gems

gem is a package manager for Ruby modules, somewhat like pacman is to Arch Linux. To update your gems, simply run:

# gem update

Option B: Installing via the AUR

Warning: This is not recommended, as this might not include the latest Rails version, and additional dependencies may be introduced that may require you to run Template:Codeline anyway.

There is a Template:Package AUR package available in the AUR. Note that this is not in an official repository, so you will need to build it manually.

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".

Web servers

While the default Ruby On Rails HTTP server (WeBrick) is convenient for basic development it is not recommended for production use. Here are some suitable alternatives:

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.

Generally, Mongrel is used in a production environment by running multiple instances of mongrel_rails, which are load-balanced behind an Nginx or Apache reverse proxy. However, you might find Phusion Passenger (see below) a much simpler solution for running a production environment.

Apache/Nginx (using Phusion Passenger)

Passenger also known as Template:Filename is a module available for Nginx and Apache, that greatly simplifies setting up a Rails server environment.

Start by installing the Passenger gem:

# gem install passenger

If you are aiming to use Apache with Passenger, run:

# passenger-install-apache2-module

For Nginx:

# passenger-install-nginx-module

The installer will provide you with any additional information regarding the installation (such as installing additional libraries).

Note: See NginX#Ruby_Integration_.28Ruby on Rails and Rack-based.29 for more information on configuring a Rails Nginx-Passenger web stack.

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 Template:Package Official.

PostgreSQL

(Stub.)

Install Template:Package Official.

MySQL

Note: You must first install MySQL with the appropriate headers in Template:Filename (just installing Template:Package Official is fine) before attempting to install the Ruby MySQL extensions.

Please refer to MySQL on how to install MySQL Server.

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 Template:Codeline 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:

 development:
   adapter: mysql
   database: my_application_database
   username: development
   password: my_secret_password

Note that you do not have to actually create the database using MySQL, as this can be done via Rails with:

# rake db:create

If no errors are shown, then your database has been created and Rails can talk to your MySQL database.

Option C: The Perfect Rails Setup

  • Archlinux: A simple, lightweight distribution. ;)
  • 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—.
  • SQLite: The default lightweight database for Ruby on Rails

Step 1: RVM

Make a multi-user RVM installation as specified here.

In the 'adding users to the rvm group' step, do

$ sudo usermod -a -G rvm http
$ sudo usermod -a -G rvm nobody

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

Step 2: Rubies

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

$ 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

Note

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:

$ 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.)

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

http { 
  ...
  passenger_root /usr/local/rvm/gems/ree-1.8.7-2011.03/gems/passenger-3.0.9;
  passenger_ruby /usr/local/rvm/wrappers/ree-1.8.7-2011.03/ruby;
  ...
}

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 nginx service by default at start-up just add Template:Codeline to the Template:Codeline array in Template:Filename:

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

Step 4: Gemsets and Apps

See also

References