夏休みに入り、30min.の運営スタッフの作業が止まるので、
その間に新しいサーバーへのデータ移行を進めている。
フレームワークとしてRuby on Railsを使用していること以前書いたと思うが、
具体的にはmongrel_clusterを使用して開発環境、本番環境の2つのRailsを動作させ、
Apache2のバーチャルホストとProxy Balancerで、
開発環境用のドメインと本番環境用のドメインで別々のRailsに振り分けており、
データベースは、位置情報データを効率よく扱うため、
PostgreSQLとその拡張ソフトのPostGISを使用している。
個別の設定については色々なサイトで紹介されているが、
まとめて紹介されているページが見当たらなかったので紹介しておこう。
(OSはCentOS 5.2を使用)
まずは
PostgreSQLのインストール。
# su -
# adduser postgres
# mkdir /usr/local/pgsql
# chown postgres.postgres /usr/local/pgsql
# su - postgres
$ cd /WORKDIR
$ tar xfvz postgresql-***.tar.gz
$ cd postgresql-***
$ ./configure
$ make
$ make install
PATHを通すために /etc/bashrc に次の設定を加える。
# postgres
PG=/usr/local/pgsql
PATH="$PATH":$PG/bin
export MANPATH="$MANPATH":$PG/man
export PGLIB=$PB/lib
export PGDATA=$PG/data
続いてデータベースの初期化
# su - postgres
$ initdb --encoding=UTF-8 --no-locale
起動スクリプトの設定。
# cp /WORKDIR/postgresql-***/contrib/start-scripts/linux /etc/rc.d/init.d/postgres
# chmod 755 /etc/rc.d/init.d/postgres
# chkconfig --add postgres
次に
proj.4ライブラリのインストール。
# cd /WORKDIR
# tar xfvz proj-***.tar.gz
# cd proj-***
# ./configure
# make
# make install
/etc/ld.so.conf.d/以下にproj4.confを作成して次の一行を記載。
/usr/local/lib
ldconfigの実行。
# ldconfig
GEOSのインストール。
# cd /WORKDIR
# tar xfvj geos-***.tar.bz2
# cd geos-***
# ./configure
# make
# make install
/etc/ld.so.conf.d/以下にgeos.confを作成して次の一行を記載。
/usr/local/bin
ldconfigの実行。
# ldconfig
PostGISのインストール。
# su - postgres
$ cd /WORKDIR
$ tar xfvz postgis-***.tar.gz
$ cd postgis-***
$ ./configure
$ make
$ make install
PostgreSQLを起動して設定を追加する。
# /etc/rc.d/init.d/postgres start
# su - postgres
$ createlang plpgsql template1
$ psql -d template1 -f /usr/local/pgsql/share/lwpostgis.sql
$ psql -d template1 -f /usr/local/pgsql/share/spatial_ref_sys.sql
ここまででデータベース関連の設定は完了だ。
続いてWebサーバー関連の設定。
Apache2のインストール。
# cd /WORKDIR
# tar xfvz httpd-***.tar.gz
# cd httpd-***/srclib/apr
# ./configure
# make
# make install
# cd ../apr-util
# ./configure --with-apr=/usr/local/apr
# make
# make install
# cd ../../
# ./configure --enable-so --enable-ssl --enable-rewrite --enable-proxy --enable-proxy-balancer
# make
# make install
起動スクリプトの設定。
# cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/apache
/etc/rc.d/init.d/apacheの最初にchkconfig用の設定を追加。
# chkconfig: 2345 99 01
# description: Apache2
chkconfigの実行。
# chkconfig --add apache
続いて
rubyのインストール。
# cd /WORKDIR
# tar xfvz ruby-***.tar.gz
# cd ruby-***
# ./configure
# make
# make install
念の為/usr/binでもPATHが通るようにしておく。
# ln -s /usr/local/bin/ruby /usr/bin/ruby
RubyGemsのインストール。
# cd /WORKDIR
# tar xfvz rubygems-***.tar.gz
# cd rubygems-***
# ruby setup.rb
Rails、gettext(日本語のメール送信用に)、mongrel、mongrel_clusterのインストール。
# gem install rails --remote --include-dependencies
# gem install gettext --remote --include-dependencies
# gem install mongrel --remote --include-dependencies
# gem install mongrel_cluster --remote --include-dependencies
Postgres用のアダプタをインストール。
# gem install postgres-pr --remote --include-dependencies
位置情報を扱うためのGeoRubyのインストール。
# gem install GeoRuby --remote --include-dependencies
SpatialAdapterをインストールするのに
Subversionが必要なので、
まずはSubversionをインストール。
# cd /WORKDIR
# tar xfvz subversion-***.tar.gz
# cd subversion-***
# ./configure --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr
# make
# make install
/usr/local/apache2/conf/httpd.confにSubversionの設定が追加されるので、
必要ない場合はコメントアウト。
#LoadModule dav_svn_module modules/mod_dav_svn.so
#LoadModule authz_svn_module modules/mod_authz_svn.so
Railsの設定。開発環境を/APPROOT/devとし、本番環境を/APPROOT/productとし、
Apache2、mongrelのユーザーをwwwとした場合。
まずはmongrel_clusterの自動起動の設定。
# cp /usr/local/lib/ruby/gems/***/gems/mongrel_cluster-***/resources/mongrel_cluster \
/etc/rc.d/init.d/mongrel_cluster
# chmod 755 /etc/rc.d/init.d/mongrel_cluster
/etc/rc.d/init.d/mongrel_clusterを修正。
まずはchkconfigの設定を追加。
#chkconfig: 2345 85 15
ユーザー名を設定。
USER=www
/usr/local/binだとPATHが通らない可能性があるので、
PATHのチェックを外す。
# which mongrel_cluster_ctl >/dev/null || exit 0
同様の理由でmongrel_cluster_ctlの箇所を変更。
/usr/local/bin/mongrel_cluster_ctl
chkconfigを実行。
# chkconfig --add mongrel_cluster
設定ファイル用のディレクトリを作成。
# mkdir /etc/mongrel_cluster
# mkdir /var/run/mongrel_cluster
# chown www.www /var/run/mongrel_cluster
Railsの環境を構築。
# su - www
$ mkdir /APPROOT/dev
$ cd /APPROOT/dev
$ rails rails
$ mongrel_rails cluster::configure -p 3000 -a 127.0.0.1 -e development \
-l /APPROOT/dev/rails/log/mongrel.log \
-c /APPROOT/dev/rails -r /APPROOT/dev/rails \
-P /var/run/mongrel_cluster/mongrel.pid -N 1 --user www --group www
$ mkdir /APPROOT/product
$ cd /APPROOT/product
$ rails rails
$ mongrel_rails cluster::configure -p 3001 -a 127.0.0.1 -e production \
-l /APPROOT/product/rails/log/mongrel.log \
-c /APPROOT/product/rails -r /APPROOT/product/rails \
-P /var/run/mongrel_cluster/mongrel.pid -N 1 --user www --group www
/APPROOT/dev/rails/config/mongrel_cluster_yml
/APPROOT/product/rails/config/mongrel_cluster_ymlにdocrootの行がある場合は削除しておく。
Apache2のDocumentRoot用に空のディレクトリを作っておく。
$ mkdir /APPROOT/dev/htdocs
$ mkdir /APPROOT/product/htdocs
mongrel_clusterの設定ディレクトリにmongrel_cluster_ymlへのシンボリックリンクを設定。
$ su -
# ln -s /APPROOT/dev/rails/config/mongrel_cluster_yml /etc/mongrel_cluster/dev.yml
# ln -s /APPROOT/product/rails/config/mongrel_cluster_yml /etc/mongrel_cluster/product.yml
Apache2の設定ファイルの編集。
/usr/local/apache2/conf/httpd.confの変更。
まずは実行ユーザーの変更。
User www
Group www
httpd-vhosts.confを読み込むようにする。
Include conf/extra/httpd-vhosts.conf
その他、ServerNameなど必要に応じて変更しておく。
/usr/local/apache2/conf/extra/httpd-vhosts.confに設定を追加。
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@DOMAINNAME.jp
DocumentRoot /APPROOT/product/htdocs
ServerName www.DOMAINNAME.jp
ErrorLog "|/usr/local/apache2/bin/rotatelogs \
/usr/local/apache2/logs/error_log.%Y%m%d 86400 540"
CustomLog "|/usr/local/apache2/bin/rotatelogs \
/usr/local/apache2/logs/access_log.%Y%m%d 86400 540" combined
<IfModule mod_proxy.c>
<Proxy *>
Order deny,allow
deny from all
# 開発環境を閲覧できるIPアドレス
allow from 192.168.0.0/24
</Proxy>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:3001/
ProxyPassReverse / http://127.0.0.1:3001/
</IfModule>
SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
</VirtualHost>
<VirtualHost *>
ServerAdmin webmaster@DOMAINNAME.jp
DocumentRoot /APPROOT/dev/htdocs
ServerName development.DOMAINNAME.jp
ErrorLog "|/usr/local/apache2/bin/rotatelogs \
/usr/local/apache2/logs/development-error_log.%Y%m%d 86400 540"
CustomLog "|/usr/local/apache2/bin/rotatelogs \
/usr/local/apache2/logs/development-access_log.%Y%m%d 86400 540" combined
<IfModule mod_proxy.c>
<Proxy *>
Order allow,deny
allow from all
</Proxy>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</IfModule>
SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
</VirtualHost>
これでWebサーバー関連の設定も完了だ。