Site Loader

上一篇成功連結本機的 MySQL 環境之後,我們已經可以透過 Rails 本身的 Webrick 伺服器來做開發了。但下一步我還希望把 Apache 也跟 Rails 連成一氣,花了大半天的時間,終於成功,以下記錄:
再列出一次PC環境:
Windows XP 32-bit
AppServ 2.5.10 (PHP 5.2.6, Apache 2.2.8, MySQL 5.0.51b)
Ruby on Rails (RailsInstaller 2.1: Ruby 1.9.3, Rails 3.2)

首先,我們找一個 ruby web server 來取代內建的 webrick。
目前與 Apache 整合最好的套件應該是 Passenger
但很遺憾的他無法在 Windows 上面使用。
而另一個之前較多數人使用的選擇 Mongrel,現在無法支援 Rails 3.+,
因此我們大約只剩下 thin 這個選項。

安裝 thin 的步驟與安裝其他 module 相同:

C:\>gem install eventmachine --pre
C:\>gem install thin

也記得要在Gemfile新增module:

gem 'thin'

並執行 bundle:

C:\AppServ\www\ruby\test> bundle install

之後可以在你建立的專案資料夾底下啟動 thin 看看是否成功:

C:\AppServ\www\ruby\test> thin -p 3000 -e development start

正確啟動的話應該可以透過 http://localhost:3000 瀏覽。
接著是 Apache 的設定部分,我們要使用 proxy module 來把客戶端的要求轉給 thin 伺服器處理。
為了方便起見,把以下的 module 都打開:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so

可以選擇用 VirtualHost 的設定方式:

< VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/AppServ/www/test/public"

BalancerMember http://127.0.0.1:3000
#        BalancerMember http://127.0.0.1:3001 # 可以用來作 load balance
#        BalancerMember http://127.0.0.1:3002

ProxyRequests Off
ProxyPreserveHost On
ProxyPass /images !
ProxyPass /stylesheets !
ProxyPass /javascripts !
ProxyPass / balancer://thinservers/
ProxyPassReverse / balancer://thinservers/
ErrorLog logs/error.log
LogLevel warn
CustomLog logs/access.log combined
< /VirtualHost>

重新啟動 apache 後,打開 http://localhost 看到的應該就是 rails 的首頁了。
但因為我希望用同一個 domain 和 port 處理 rails 和其他原本的專案,
所以找到以下使用 Location 的設定方式:

< Location /test>
ProxyPass http://127.0.0.1:3000/test/
ProxyPassReverse http://127.0.0.1:3000/test/
< /Location>

啟動 thin 的時候也要指定 url prefix,不然 render 出來的網頁連結 route 會錯誤:

C:\AppServ\www\ruby\test> thin --prefix=/test -p 3000 -e development start 

完成之後打開 http://localhost/test/ 就可以看到 rails 首頁,
但是後來發現雖然 linkto 連結是正確的了,但是 /assets (.js及.css的資料夾)卻沒有包含 /test 這個 prefix 。
網路上的資料紛雜,看來 Rails 從 2.x 到 3.2 中間曾經有不少設定上的更改,
我自己最後是在 config/application.rb 中加入以下這行,就可以讓 /assets 的路徑正確了:

config.action_controller.relative_url_root = '/test'

這樣啟動後也不會影其他原本放在 localhost 底下的 html/php 專案囉。
延伸閱讀:
[RoR] AppServ 2.5.10 (MySQL 5.0.24) 整合 Ruby on Rails (1.9.3)
參考資料:
Thin:
http://stackoverflow.com/questions/3649252/cannot-install-thin-on-windows
http://www.cnblogs.com/lwm-1988/archive/2012/06/19/2554377.html

Using Apache with Mongrel or Thin and Rails 3


http://serverfault.com/questions/345133/running-thin-behind-apache2
http://serverfault.com/questions/379667/apache-proxypass-ignore-static-files
Mongrel:
http://stackoverflow.com/questions/148838/how-do-i-configure-apache-2-2-for-ruby-on-rails-in-windows
http://stackoverflow.com/questions/2025449/how-to-install-ruby-on-rails-alongside-wampserver
http://stackoverflow.com/questions/3250406/msvcrt-ruby18-dll-was-not-found-with-ruby
Some conclusion:
http://stackoverflow.com/questions/5268794/deploying-rails-3-applications-on-windows

Post Author: starshine