使用Unicorn替代Mongrel作為Ruby on Rails的服務器

先前的開發的Ruby on Rails網站使用的服務程序是Mongrel + Nginx, 現在用了Rails 3, 發現Mongrel對它有兼容問題, 所以要換一個。 雖然現在Phusion Passenger大行其道, 但使用前要重新編譯Nginx。 為了避免重新安裝nginx,我找到mongrel的替代品Unicorn。 沒想到unicorn的設置非常方便。 我記錄在這裡供大家參考:

安裝unicorn:

sudo gem install unicorn

創建網站配置文件(myproject是項目名稱):

sudo mkdir /etc/unicorn
cd /etc/unicorn/
sudo nano /etc/unicorn/myproject.conf

內容如下:

RAILS_ROOT=/www/myproject
RAILS_ENV=production

在網站里再創建一個unicorn配置文件

nano /www/myproject/config/unicorn.rb

內容如下:

# Minimal sample configuration file for Unicorn (not Rack) when used
# with daemonization (unicorn -D) started in your working directory.
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.
# See also http://unicorn.bogomips.org/examples/unicorn.conf.rb for
# a more verbose configuration using more features.

app_path = "/www/myproject"

listen 8080 # by default Unicorn listens on port 8080
worker_processes 2 # this should be >= nr_cpus
pid "#{app_path}/tmp/pids/unicorn.pid"
stderr_path "#{app_path}/log/unicorn.log"
stdout_path "#{app_path}/log/unicorn.log"

設置unicorn啟動腳本:

sudo nano /etc/init.d/unicorn_init

腳本內容:

#!/bin/sh
#
# init.d script for single or multiple unicorn installations. Expects at least one .conf
# file in /etc/unicorn
#
# Modified by jay@gooby.org http://github.com/jaygooby
# based on http://gist.github.com/308216 by http://github.com/mguterl
#
## A sample /etc/unicorn/my_app.conf
##
## RAILS_ENV=production
## RAILS_ROOT=/var/apps/www/my_app/current
#
# This configures a unicorn master for your app at /var/apps/www/my_app/current running in
# production mode. It will read config/unicorn.rb for further set up.
#
# You should ensure different ports or sockets are set in each config/unicorn.rb if
# you are running more than one master concurrently.
#
# If you call this script without any config parameters, it will attempt to run the
# init command for all your unicorn configurations listed in /etc/unicorn/*.conf
#
# /etc/init.d/unicorn start # starts all unicorns
#
# If you specify a particular config, it will only operate on that one
#
# /etc/init.d/unicorn start /etc/unicorn/my_app.conf

set -e

sig () {
test -s "$PID" && kill -$1 `cat "$PID"`
}

oldsig () {
test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"`
}

cmd () {

case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting"
$CMD
;;
stop)
sig QUIT && echo "Stopping" && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && echo "Forcing a stop" && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
upgrade)
sig USR2 && echo Upgraded && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
$CMD
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 "
exit 1
;;
esac
}

setup () {

echo -n "$RAILS_ROOT: "
cd $RAILS_ROOT || exit 1
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid
export OLD_PID="$PID.oldbin"

CMD="/usr/bin/unicorn_rails -c config/unicorn.rb -E $RAILS_ENV -D"
}

start_stop () {

# either run the start/stop/reload/etc command for every config under /etc/unicorn
# or just do it for a specific one

# $1 contains the start/stop/etc command
# $2 if it exists, should be the specific config we want to act on
if [ $2 ]; then
. $2
setup
cmd $1
else
for CONFIG in /etc/unicorn/*.conf; do
# import the variables
. $CONFIG
setup

# run the start/stop/etc command
cmd $1
done
fi
}

ARGS="$1 $2"
start_stop $ARGS

注意將裡面的/usr/bin/unicorn_rails 換成你系統中unicorn_rails程序的實際路徑。

設置unicorn_init文件屬性:

sudo chmod 755 /etc/init.d/unicorn_init

啟動unicorn:

/etc/init.d/unicorn_init

修改nginx的配置文件,加入unicorn的代理設置:

upstream myproject_mongrel {
server 127.0.0.1:8080 fail_timeout=0;
}

這部分跟使用mongrel的類似的。

這樣unicorn的設置就完成了。 剛設置好,感覺unicorn跟mongrel一樣, 都是比較吃內存的, 一啟動就佔了50M. 不知道會不會也像mongrel一樣把內存吃爆, 會得話得設置監控軟件(如god)看住它。

此條目發表在 Ruby on Rails, 站長文檔 分類目錄,貼了 , , , , 標籤。將固定鏈接加入收藏夾。

使用Unicorn替代Mongrel作為Ruby on Rails的服務器》有 1 條評論

  1. Kingron 說:

    你好,很高興認識你,我想和你聊聊Ruby的方面的事情,如果收到信息,麻煩回復一下我的郵箱,謝謝。

Kingron 進行回復 取消回復