Linux 下的 Apache + FastCGI 部署 ROR 应用

介绍如何在 Linux 下部署 Apache + FastCGI 的 ROR 应用。

1) 安装 ROR 环境

这里假设你的机器为干净的机器,我使用的是 ubuntu-alternate 8.04 命令行安装模式,下面的脚本适合 debian 系的发行版,理论上不兼容 RedHat 系的发行版。

为方便起见,我将安装 ROR 环境写了一个 SHELL 脚本,直接执行即可方便的安装 ROR 环境,这里使用的 RAILS 版本为 2.0.2。

请使用 sudo 前缀执行该脚本:

install_ror.sh

 1#!/bin/bash
 2INSTALL_HOME=~/ror_installer
 3if [ -d "$INSTALL_HOME" ]; then
 4  echo INSTALL_HOME directory "~/ror_installer" already exist.
 5  exit 1
 6fi
 7mkdir $INSTALL_HOME
 8pushd $INSTALL_HOME
 9  # Download files
10  apt-get -d -y install ruby libruby-extras libyaml-ruby \
11    libzlib-ruby libiconv-ruby libxml-ruby libdbi-ruby \
12    libmysql-ruby libdbd-mysql-ruby libdbd-sqlite3-ruby libfcgi-ruby1.8
13
14  wget -t0 -T15 http://rubyforge.org/download.php/35283/rubygems-1.1.1.tgz
15  wget -t0 -T15 http://rubyforge.org/download.php/29353/actionmailer-2.0.2.gem
16  wget -t0 -T15 http://rubyforge.org/download.php/29350/actionpack-2.0.2.gem
17  wget -t0 -T15 http://rubyforge.org/download.php/29551/activerecord-2.0.2.gem
18  wget -t0 -T15 http://rubyforge.org/download.php/29552/activeresource-2.0.2.gem
19  wget -t0 -T15 http://rubyforge.org/download.php/29553/activesupport-2.0.2.gem
20  wget -t0 -T15 http://rubyforge.org/download.php/29554/rails-2.0.2.gem
21  wget -t0 -T15 http://rubyforge.org/download.php/29751/rake-0.8.1.gem
22
23  # Install ruby
24  apt-get -y install ruby libruby-extras libyaml-ruby \
25    libzlib-ruby libiconv-ruby libxml-ruby libdbi-ruby \
26    libmysql-ruby libdbd-mysql-ruby libdbd-sqlite3-ruby libfcgi-ruby1.8
27
28  # Install rubygems
29  tar xfv rubygems-1.1.1.tgz
30  pushd rubygems-1.1.1
31    ruby setup.rb
32    ln -s /usr/bin/gem1.8 /usr/bin/gem
33  popd
34  # Install Gem
35  gem install rails -l --no-ri --no-rdoc
36  
37# Clean 
38popd
39rm -rf $INSTALL_HOME

2) 安装 Apache

同样是 SHELL 脚本安装,请使用 sudo 前缀执行该脚本:

install_apache2.sh

1#!/bin/bash
2# Download files
3apt-get -d -y install apache2 libapache2-mod-fcgid
4
5# Install apache
6apt-get -y install apache2 libapache2-mod-fcgid
7ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

3) 配置 Apache

ROR 应用的 Apache 配置,在 /etc/apache2/apache2.conf 的末尾添加以下代码:
apache2.conf

1Include /etc/apache2/httpd.conf

修改 httpd.conf 为以下内容:
httpd.conf

 1DefaultInitEnv RAILS_ENV production
 2Alias "/redmine" "/var/www/redmine/public/"
 3<Directory "/var/www/redmine/public/">
 4  Options Indexes ExecCGI
 5  FollowSymLinks
 6  AllowOverride all
 7  Order Deny, Allow
 8  Allow from All
 9</Directory>
10<Location /redmine/>
11    AddHandler fcgid-script .fcgi
12    FCGIWrapper "/usr/bin/ruby /var/www/redmine/public/dispatch.fcgi" .fcgi
13    RewriteEngine on
14    RewriteBase /redmine
15    RewriteRule ^$ index.html [QSA]
16    RewriteRule ^([^.]+)$ $1.html [QSA]
17    RewriteCond %{REQUEST_FILENAME} !-f
18    RewriteRule ^(.*)$ dispatch.fcgi?$1 [QSA,L]
19</Location>

4) 配置 ROR 应用

配置 ROR 应用,这里为方便期间,直接使用 sqlite3,ROR 程序为 redmine
请使用 sudo 前缀执行该脚本:
install_app.sh

 1#!/bin/bash
 2INSTALL_HOME=~/install_redmine
 3if [ -d "$INSTALL_HOME" ]; then
 4  echo INSTALL_HOME directory "~/install_redmine" already exist.
 5  exit 1
 6fi
 7mkdir $INSTALL_HOME
 8
 9# Download files
10pushd $INSTALL_HOME
11  wget -t0 -T15 http://rubyforge.org/download.php/36468/redmine-0.7.1.tar.gz
12popd
13
14# Install redmine
15tar xfv $INSTALL_HOME/redmine-0.7.1.tar.gz -C /var/www/
16mv /var/www/redmine-0.7.1 /var/www/redmine
17cp /var/www/redmine/public/dispatch.fcgi.example /var/www/redmine/public/dispatch.fcgi
18
19#---- begin database.yml ----
20declare -x START_NO=$LINENO;
21if [ 0 -eq 1 ]; then
22production:
23  adapter: sqlite3
24  database: db/production.sqlite3
25  timeout: 5000
26
27development:
28  adapter: sqlite3
29  database: db/development.sqlite3
30  timeout: 5000
31
32test:
33  adapter: sqlite3
34  database: db/test.sqlite3
35  timeout: 5000
36fi;
37declare -x END_NO=$LINENO
38#---- end database.yml ----
39awk '{if(NR > ENVIRON["START_NO"] && NR < ENVIRON["END_NO"]) {print}}' $0 > /var/www/redmine/config/database.yml
40
41pushd /var/www/redmine
42  rake db:create RAILS_ENV="production"
43  rake db:migrate RAILS_ENV="production"
44  
45  #  Load redmine's default data
46  echo en > loc
47  rake redmine:load_default_data RAILS_ENV="production" < loc
48  rm -f log/production.log
49  rm -f public/.htaccess
50popd
51
52# Clean
53rm -rf $INSTALL_HOME

注:如果在 Linux 下部署,并使用 SQLite3 数据库时,第一次访问 redmine 无法创建匿名用户,访问时会发生 500 错误,这是属于 redmine 的问题,要解决此问题,执行以下命令:

交互式命令:

1sudo apt-get install sqlite3
2cd /var/www/redmine/db
3sudo sqlite3 production.sqlite3
4> INSERT INTO users ("created_on", "status", "last_login_on", "hashed_password", "language", "updated_on", "admin", "type", "firstname", "lastname", "mail", "auth_source_id", "mail_notification", "login") VALUES('2008-05-21 04:31:54', 0, NULL, '', '', '2008-05-21 04:31:54', 'f', 'AnonymousUser', '', 'Anonymous', '', NULL, 'f', '');
5> .exit
6sudo apache2ctl restart
7ruby script/server -e production

最后,重启 apache2:

1sudo apache2ctl restart

部署看似简单,写起脚本来可是非常费劲的,不过一劳永逸,以后想要部署可以直接参考使用了。

View Comments