2013年4月22日 星期一

在Ubuntu12.04安裝Rails、Apache與Passenger

軟體版本

Ubuntu:12.04
Ruby:1.9.3
Rails::3.2.9
Passenger:3.0.19


※注意:不要使用管理者(root)身分安裝RVM,所有操作皆在使用者家目錄(home)

對Ubuntu進行系統更新

sudo apt-get update
sudo apt-get upgrade

 安裝版本控制系統(git)、CURL及其他相關套件

sudo apt-get install git
sudo apt-get install git-core
sudo apt-get install mysql-server libmysqlclient15-dev
sudo apt-get install curl
sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline5-dev
sudo apt-get install libreadline6-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev autoconf libgdbm-dev libncurses5-dev automake libtool bison libffi-dev

安裝RVM(Ruby Version Manager)

curl -L get.rvm.io | bash -s stable --auto
echo "[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile
. ~/.bash_profile
source ~/.bash_profile
安裝完RVM後,因為需要重新設定連線參數,所以需要重新登入連線,才能執行RVM 

安裝REE(Ruby Enterprise Edition)

rvm install ree
rvm ree --default
若安裝REE過程跑出下列訊息,則請重新sudo apt-get install build-essential安裝此套件
Error running './installer -a /home/kejyun/.rvm/rubies/ree-1.8.7-2012.02 --dont-install-useful-gems -c --disable-install-doc -c --enable-shared',
please read /home/kejyun/.rvm/log/ree-1.8.7-2012.02/1366641942_install.log
There has been an error while trying to run the ree installer. Halting the installation.

安裝libbuilder

sudo apt-get install libbuilder-ruby

安裝Rails

gem install rails -v 3.2.9

安裝MySQL套件

gem install mysql

安裝Passenger 

gem install passenger

安裝CURL with SSL 

sudo apt-get install libcurl4-openssl-dev

安裝Apache

sudo apt-get install apache2
sudo apt-get install apache2-prefork-dev 

在Apache安裝Passenger模組 

passenger-install-apache2-module
安裝完成後會有下列訊息,這些是等等要設定到Apache的設定值,訊息會依你安裝的軟體版本不同,而有不同的版本號碼,所以請不要直接複製
The Apache 2 module was successfully installed.
Please edit your Apache configuration file, and add these lines:
   LoadModule passenger_module /home/kejyun/.rvm/gems/ree-1.8.7-2012.02/gems/passenger-3.0.19/ext/apache2/mod_passenger.so
   PassengerRoot /home/kejyun/.rvm/gems/ree-1.8.7-2012.02/gems/passenger-3.0.19
   PassengerRuby /home/kejyun/.rvm/wrappers/ree-1.8.7-2012.02/ruby
After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!
Press ENTER to continue.

--------------------------------------------
Deploying a Ruby on Rails application: an example
Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:
   <VirtualHost *:80>
      ServerName www.yourhost.com
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /somewhere/public
      <Directory /somewhere/public>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
      </Directory>
   </VirtualHost>

在Apache設定讀取Passenger模組

sudo vim /etc/apache2/mods-enabled/mod_rails_passenger.conf
LoadModule passenger_module /home/kejyun/.rvm/gems/ree-1.8.7-2012.02/gems/passenger-3.0.19/ext/apache2/mod_passenger.so
PassengerRoot /home/kejyun/.rvm/gems/ree-1.8.7-2012.02/gems/passenger-3.0.19 PassengerRuby /home/kejyun/.rvm/wrappers/ree-1.8.7-2012.02/ruby

修改網站設定

sudo vim /etc/apache2/sites-enabled/rails.site
我是第一次玩Ruby的人,從PHP Framework跳轉到Ruby,在PHP Framework的DocumentRoot是需要設定到Framework的根目錄即可,而Rails的DocumentRoot則是需要設定在Rails專案資料中的app資料夾(/RubyProject/app)才可正常執行,剛設定的時候不知道以為是自己其他哪些設定設錯了...|||
<VirtualHost *:80>
  ServerName rails.site
  # !!! Be sure to point DocumentRoot to 'public'!
  DocumentRoot /home/kejyun/rubypro/site/app
  <Directory /home/kejyun/rubypro/site/app>
     # This relaxes Apache security settings.
     AllowOverride all
     # MultiViews must be turned off.
     Options -MultiViews
  </Directory>
</VirtualHost> 

啟動Apache Rewrite模組

 a2enmod rewrite

建立Rails專案

mkdir rubypro
cd rubypro
rails new site

產生一個測試的Controller

rails g controller test
若產生Controller時有下列ExecJS的錯誤訊息的話,則必須要輸入sudo apt-get install nodejs安裝node.js
/home/kejyun/.rvm/gems/ree-1.8.7-2012.02/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodet            ect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list             of available runtimes. (ExecJS::RuntimeUnavailable) 

建立測試頁面

打開剛剛建立的Controller(app/controllers/test_controller.rb)並建立welcome方法

class TestController < ApplicationController
def welcome
end
end

建立樣板(app/views/test/welcome.html.erb)

在樣板檔案輸入
<h1>Hello Ruby on Rails</h1>

設定預設路由

在site/config/route.rb中設定test Controller的Welcome為預設路由
root :to => "test#welcome"

測試頁面

http://rails.site/

若畫面顯示We're sorry, but something went wrong.,這個是Rails的錯誤畫面(site/public/500.html),表示您的Rails環境已經設定好了,所以才能看到這個畫面,只是有些設定錯誤導致無法讀取到真正我們想要讀取的頁面,這時候就需要看錯誤log,log檔會記錄在site/log/production.log,必須要看看出現了什麼錯誤訊息去修復這個錯誤

錯誤訊息除錯(若無錯誤可正常執行請跳過此步驟)

ActionView::Template::Error (application.css isn't precompiled):

site/config/environments/production.rb中,將config.assets.compile設定為true即可

ActionController::RoutingError (No route matches [GET] "/favicon.ico"):

site/app放置您的網站小圖示favicon.ico即可

重新啟動Apache Server

sudo service apache2 restart

參考資料

沒有留言:

張貼留言

ADS