Ruby on Rails 的 環境設定 / 安裝架設, 建制 Rails + Apache + MySQL 環境.
Rails 官方文件
- Rails 官方文件庫(Wiki): Ruby on Rails
- Ubuntu 安裝 Rails 文件: RailsOnUbuntu in Ruby on Rails
- Debian 安裝 Rails 文件: RailsOnDebian in Ruby on Rails
- 其它系統安裝: Getting Started With Rails
套件說明(名詞需知)
- Ruby: Ruby Programming Language
- Ruby on Rails: Web framework
- Gem: A gem is a packaged Ruby application or library
- rake: a ruby build program with capabilities similar to make (Ruby Make)
架設步驟
- 安裝所需 Package
- 系統升級 (rails / gems)
- 設定 Apache 或 啟動 rails 的 server
- 測試
安裝設定步驟(Debian / Ubuntu 套件安裝)
Ubuntu 所需的 Package
- apt-get install ruby rails rubygems rake # 安裝 Ruby / Rails 相關套件
- apt-get install apache2 mysql-server libapache2-mod-ruby # 安裝 Aapche / MySQL
- apt-get install libdbi-ruby libdbd-mysql-ruby # 安裝 Ruby 與 MySQL 連結所需 Library
- apt-get install vim-rails # 使用 vim 的話就裝吧~
- 或 apt-get install ruby rails rubygems rake apache2 mysql-server libapache2-mod-ruby libdbi-ruby libdbd-mysql-ruby vim-rails # 把上面匯整成一行, 一次安裝
Debian 所需的 Package
- apt-get install ruby rails rubygems rake irb libapache2-mod-ruby libdbi-ruby libdbd-mysql-ruby
設定 Apache module
- a2enmod rewrite # 開啟 Apache 的 rewrite module
Ubuntu 8.04 問題排除 (gem)
問題: 執行 gem 時, 於 Ubuntu 8.04 出現 "uninitialized constant Gem::GemRunner (NameError)"
解法:
- vim /usr/bin/gem
- 於 require 'rubygems' 下面加上 require 'rubygems/gem_runner
- 如下述:
require 'rubygems'
require 'rubygems/gem_runner - 這樣子就能執行 gem 了.
Rails 系統升級 (使用 gem)
建議安裝完後, 就馬上執行下述升級
- gem install -v=2.1.0 rails # 升級成 2.1.0
- gem update --system
- gem update
Gem 常用指令
- gem list -r yahoo # search
- gem cleanup # 清除 cache
- gem install ruby-openid # 安裝 ruby-openid package
- gem uninstall ruby-openid # 移除 ruby-openid package
- gem list --local # 看 local 裝多少 package
- ls /usr/lib/ruby/gems/1.8/gems/ # Gem 安裝完成的 Package 都放在這邊
測試 Ruby 與 MySQL 連線
- vim /tmp/simple.rb
# simple.rb - simple MySQL script using Ruby DBI module
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("dbi:Mysql:mysql:localhost", "root", "")# get server version string and display it
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end - 執行 ruby /tmp/simple.rb # 看到 MySQL 版本就正確囉~
測試 rails
- cd /var/www # 到 /var/www 來建立專案
- rails project_name # 建立一個 Project
- mysql> create database test; # 建一個 test 的 DB
- cd project_name
- rm public/index.html # 不砍掉的話, 預設畫面都會出現這個
- vim config/database.yml # 修改 development: "database: test"
於 Controller 吐資料
- script/generate controller Blog # 建立一個 Blog 的 controller
- script/server # 啟動 server, 就可以由 http://localhost:3000/ 連線了
- # Rails 的 Hello world
- vim app/controllers/blog_controller.rb # 加入下述
def index
render :text => "Hello world"
end - 於 http://localhost:3000/ # 就可以看到 Hello world 了.
於 view 吐資料
- vim views/blog/index.rhtml # 於 View 吐 Hello world, 記得將上述 controller 的 render :text => "Hello world" 註解掉.
- 看到 Hello world 就正確囉~
設定 Apache 存取 Rails 專案
Apache 設定重點是下述內容:
ServerName DOMAIN_NAME
DocumentRoot /var/www/project_name/RubyRequire apache/ruby-run
# Options +ExecCGI
<Files *.rbx>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
設定使用自己寫的 ruby file (不跑 rails framework), 透過 Apache 測試看看.
- cp /tmp/simple.rb /var/www/ruby/simple.rbx # 剛剛上面有個 simple.rb 檔, 直接 copy 過來測試
- 於 pache error log(/var/log/apache2/error.log) 若有發現下述錯誤:
[Sat Oct 04 14:10:36 2008] [error] access to /var/www/ruby/test.rbx failed for (null), reason: file permissions deny server execution
- 執行 chmod +x /var/www/ruby/test.rbx 即可.
設定 Rails 專案步驟
- vim /etc/apache2/site-enable/test.conf # 建議於 sites-available 建, 再 ln 到 site-enable
<VirtualHost *>
ServerAdmin webmaster@localhostServerName DOMAIN_NAME
DocumentRoot /var/www/project_name/public/
<Directory /var/www/project_name/public/>
AddHandler cgi-script .cgi
AddHandler fastcgi-script .fcgi .fcg .fpl
Options +ExecCGI
AllowOverride All
Order allow,deny
Allow from allRewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
</Directory>RubyRequire apache/ruby-run服器 <Files *.rbx>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warnCustomLog /var/log/apache2/access.log combined
ServerSignature On
</VirtualHost> - 再 /etc/init.d/apache2 restart 即可, 就可以使用 http://DOMAIN_NAME/ 連到囉~ 🙂
相關網頁
- Ruby on Rails on Debian
- Ubuntu 8.04灌ruby on rails
- Ruby on Rails (ROR) - 蠻不錯的 RoR 簡介
- Ruby on Rails on XAMPP with FastCGI for Ubuntu Hoary
- HowTo: Install Ruby, RubyonRails, and RubyGems on Ubuntu 5.10 (Breezy)
- Ruby on Rails(Windows),從安裝到 Helloworld
- 在Ubuntu/Debian上面安装Ruby on Rails的生產伺服器環境
- 我想寫 Ruby on Rails (Windows 篇)
- 在 Rails project 上導入 OpenID