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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
INSTALL_HOME=~/ror_installer
if [ -d "$INSTALL_HOME" ]; then
  echo INSTALL_HOME directory "~/ror_installer" already exist.
  exit 1
fi
mkdir $INSTALL_HOME
pushd $INSTALL_HOME
  # Download files
  apt-get -d -y install ruby libruby-extras libyaml-ruby \
    libzlib-ruby libiconv-ruby libxml-ruby libdbi-ruby \
    libmysql-ruby libdbd-mysql-ruby libdbd-sqlite3-ruby libfcgi-ruby1.8

  wget -t0 -T15 http://rubyforge.org/download.php/35283/rubygems-1.1.1.tgz
  wget -t0 -T15 http://rubyforge.org/download.php/29353/actionmailer-2.0.2.gem
  wget -t0 -T15 http://rubyforge.org/download.php/29350/actionpack-2.0.2.gem
  wget -t0 -T15 http://rubyforge.org/download.php/29551/activerecord-2.0.2.gem
  wget -t0 -T15 http://rubyforge.org/download.php/29552/activeresource-2.0.2.gem
  wget -t0 -T15 http://rubyforge.org/download.php/29553/activesupport-2.0.2.gem
  wget -t0 -T15 http://rubyforge.org/download.php/29554/rails-2.0.2.gem
  wget -t0 -T15 http://rubyforge.org/download.php/29751/rake-0.8.1.gem

  # Install ruby
  apt-get -y install ruby libruby-extras libyaml-ruby \
    libzlib-ruby libiconv-ruby libxml-ruby libdbi-ruby \
    libmysql-ruby libdbd-mysql-ruby libdbd-sqlite3-ruby libfcgi-ruby1.8

  # Install rubygems
  tar xfv rubygems-1.1.1.tgz
  pushd rubygems-1.1.1
    ruby setup.rb
    ln -s /usr/bin/gem1.8 /usr/bin/gem
  popd
  # Install Gem
  gem install rails -l --no-ri --no-rdoc
  
# Clean 
popd
rm -rf $INSTALL_HOME

2) 安装 Apache

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

install_apache2.sh

1
2
3
4
5
6
7
#!/bin/bash
# Download files
apt-get -d -y install apache2 libapache2-mod-fcgid

# Install apache
apt-get -y install apache2 libapache2-mod-fcgid
ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

3) 配置 Apache

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

1
Include /etc/apache2/httpd.conf

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

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

4) 配置 ROR 应用

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
INSTALL_HOME=~/install_redmine
if [ -d "$INSTALL_HOME" ]; then
  echo INSTALL_HOME directory "~/install_redmine" already exist.
  exit 1
fi
mkdir $INSTALL_HOME

# Download files
pushd $INSTALL_HOME
  wget -t0 -T15 http://rubyforge.org/download.php/36468/redmine-0.7.1.tar.gz
popd

# Install redmine
tar xfv $INSTALL_HOME/redmine-0.7.1.tar.gz -C /var/www/
mv /var/www/redmine-0.7.1 /var/www/redmine
cp /var/www/redmine/public/dispatch.fcgi.example /var/www/redmine/public/dispatch.fcgi

#---- begin database.yml ----
declare -x START_NO=$LINENO;
if [ 0 -eq 1 ]; then
production:
  adapter: sqlite3
  database: db/production.sqlite3
  timeout: 5000

development:
  adapter: sqlite3
  database: db/development.sqlite3
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  timeout: 5000
fi;
declare -x END_NO=$LINENO
#---- end database.yml ----
awk '{if(NR > ENVIRON["START_NO"] && NR < ENVIRON["END_NO"]) {print}}' $0 > /var/www/redmine/config/database.yml

pushd /var/www/redmine
  rake db:create RAILS_ENV="production"
  rake db:migrate RAILS_ENV="production"
  
  #  Load redmine's default data
  echo en > loc
  rake redmine:load_default_data RAILS_ENV="production" < loc
  rm -f log/production.log
  rm -f public/.htaccess
popd

# Clean
rm -rf $INSTALL_HOME

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

交互式命令:

1
2
3
4
5
6
7
sudo apt-get install sqlite3
cd /var/www/redmine/db
sudo sqlite3 production.sqlite3
> 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', '');
> .exit
sudo apache2ctl restart
ruby script/server -e production

最后,重启 apache2:

sudo apache2ctl restart

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

Zealic @ 2008-05-21

View Comments |
Categories: tech.posix
Tags:

Related posts