Ruby on Rails

出自 Arch Linux 中文维基

本文內容或本節內容已經過期。

原因: 請提供模板的第一個位置參數以概括原因。 (在Talk:Ruby on Rails討論)

這篇文章或章節的翻譯不反映原文。

原因:Only halfway translated, still references rc.conf(在 Talk:Ruby on Rails# 中討論)

Ruby on Rails,通常簡寫為 Rails 或者 RoR,是一個 Ruby 語言的開源的 web 應用框架。它通常被 web 開發人員使用敏捷開發方法做快速開發使用。

本文檔描述如何在Arch Linux下建立Ruby on Rails開發環境。

Ruby on Rails 需要首先安裝 Ruby,所以閱讀首先閱讀那篇文章查看安裝說明。

選項A:通過RubyGems來安裝 (推薦)[編輯 | 編輯原始碼]

注意: 如果這個命令不是用root運行的,那麼gem會把它安裝到用戶的家目錄中。
# gem install rails

創建文檔需要一會時間. 如果不想要它,加上 --no-ri --no-rdoc 參數到安裝命令中.

# gem install rails --no-ri --no-rdoc
注意: 如果安裝完成後提示警告在PATH中沒有~/.gem/ruby/2.0.0/bin,gem executables will not run,那麼要使用命令export PATH="$PATH:$(ruby -rubygems -e 'puts Gem.user_dir')/bin"將rails加入環境變量,最後可以通過命令rails -v查看安裝的rails版本確認rails可用。

更新gems[編輯 | 編輯原始碼]

gem 是一個ruby模塊包管理器, 類似pacman. 去更新gem, 簡單的使用gem命令:

# gem update

選項B:通過 AUR 安裝[編輯 | 編輯原始碼]

警告: 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 gem install anyway.

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

配置[編輯 | 編輯原始碼]

Rails 自帶了一個基本的HTTP伺服器 WeBrick. 你可以創建一個測試應用來測試它. 首先, 創建一個應用使用rails 命令:

# rails new testapp_name

然後開始Web伺服器. 它默認監聽3000端口 :

# cd testapp_name
# rails server 

完成後打開瀏覽器輸入你伺服器地址和3000端口. 例如, 如果伺服器在本機 :

在浏览器地址栏输入http://127.0.0.1:3000/ 

你將看到Rails默認歡迎界面 "Welcome aboard".

Web 伺服器[編輯 | 編輯原始碼]

Ruby On Rails 默認HTTP 伺服器(WeBrick) 可以方便去測試,但是不推薦在產品中使用。以下是一些合適的選擇:

Mongrel[編輯 | 編輯原始碼]

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

通過gem來安裝mongrel:

# gem install mongrel

然後開始mongrel:

# 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 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:

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

數據庫[編輯 | 編輯原始碼]

多數Web應用都需要數據庫去保存數據。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 是Ruby on Rails默認的數據庫. 要去激活 SQLite, 安裝ruby-sqlite3

# pacman -S ruby-sqlite3

PostgreSQL[編輯 | 編輯原始碼]

(待補充) 安裝 postgresql.

MySQL[編輯 | 編輯原始碼]

注意: 在嘗試安裝 Ruby MySQL 擴展之前,你必須首先安裝帶有合適頭文件(位於/usr/include) 的 MySQL(只需要安裝 mysqlAUR 就可以了)。

關於如何安裝 MySQL 伺服器請參考 MySQL

首先安裝通過gem安裝mysql模塊:

# gem install mysql

你可以創建使用mysql的Rails應用使用 -d 參數:

$ rails new testapp_name -d mysql

然後你需要編輯 config/database.yml. Rails 為production/tests/development使用3種數據庫。下面示例使用development數據庫. 填寫數據庫,用戶名,密碼字段:

 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:migrate

如果沒有錯誤顯示, 那麼表示建立正確,並且Rails可以和MySQL數據庫通信了。

選項C:完美的 Rails 安裝[編輯 | 編輯原始碼]

Phusion Passenger running multiple Ruby versions.

  • Arch Linux: 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.

步驟 0: SQLite[編輯 | 編輯原始碼]

Easy as:

$ sudo pacman -S sqlite
注意: Of course SQLite is not critical in this setup, you can use MySQL and PostgreSQL as well.

步驟 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.

注意: Maybe adding the 'nodody' user to the 'rvm' group is not necessary.

步驟 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

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.

步驟 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 Nnginx service by default at start-up just add nginx to the DAEMONS array in /etc/rc.conf:

DAEMONS=(ntpd syslog-ng ... nginx)
注意: 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.

步驟 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:

$ rvm use ree@refinery --create
$ gem install rails -v 3.0.11
$ gem install passenger
$ gem install refinerycms refinerycms-i18n sqlite3

Deploy a RefineryCMS instance called refineria:

$ cd /srv/http/
$ rvmsudo refinerycms refineria

Again:

$ rvm use 1.9.3@browser --create
$ gem install passenger
$ gem install browsercms sqlite3

Deploy a BrowserCMS instance called navegador:

$ cd /srv/http/
$ rvmsudo browsercms demo navegador
$ cd /srv/http/navegador
$ rvmsudo rake db:install

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.

步驟 5: .rvmrc 文件和所有者[編輯 | 編輯原始碼]

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 and put

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

while in /srv/http/navegador/.rvmrc,

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

步驟 6: 反向代理[編輯 | 編輯原始碼]

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

$ cd /srv/http/refineria
$ rvmsudo passenger start --socket refineria.socket -d

and

$ cd /srv/http/navegador
$ rvmsudo passenger start --socket navegador.socket -d

. The first time that you run a Passenger Standalone it will perform a minor installation.

Note that you are using unix domain sockets instead of the commonly-used TCP sockets; it turns out that unix domain are significantly faster than TCP sockets.

Launch Passenger Standalone daemons at system start-up[編輯 | 編輯原始碼]

Coming soon!

步驟 7: 部署[編輯 | 編輯原始碼]

Once again edit /opt/nginx/conf/nginx.conf to include some vital instructions:

## RefineryCMS ##

upstream refineria_upstream {
    server unix:/srv/http/refineria/refineria.socket;
}

server {
    server_name refinery.domain.com;
    root /srv/http/refineria/public;
    location / {
        proxy_pass http://refineria_upstream;
        proxy_set_header Host $host;
    }
}

## BrowserCMS ##

upstream navegador_upstream {
    server unix:/srv/http/navegador/navegador.socket;
}

server {
    server_name browser.domain.com;
    root /srv/http/navegador/public;
    location / {
        proxy_pass http://navegador_upstream;
        proxy_set_header Host $host;
    }
}

At this point you are in conditions to run Nginx with:

$ sudo rc.d start nginx

and to access both CMSs through refinery.domain.com and browser.domain.com.

參考文獻[編輯 | 編輯原始碼]

參見[編輯 | 編輯原始碼]

參考文獻[編輯 | 編輯原始碼]