使用Unicorn替代Mongrel作为Ruby on Rails的服务器

本文介绍了一种针对Ruby on Rails 3应用的部署方案,使用Unicorn作为服务器替代Mongrel,详细记录了Unicorn的安装配置过程,并提供了启动脚本及Nginx代理设置。
先前的开发的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)看住它。
http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/
下载代码方式:https://pan.quark.cn/s/e6c2e312b658 在苹果公司的Mac操作系统环境中,当用户尝试安装非原厂驱动程序时,可能会遭遇系统无法正常启动的困境。这种情况常常源于名为.kext的内核扩展驱动程序存在兼容性问题或安装过程中出现失误。这份指南介绍了一种无需重新安装操作系统且能够保护所有用户数据的修复方法,这一方案对于先前许多面临类似挑战的用户而言,曾是极为棘手的情况。文档中提及的“用户模式启动”实际是指单用户模式,这种启动方式仅加载核心系统功能,而忽略图形用户界面及常规应用程序的加载。在单用户模式下,用户能够访问命令行界面,进而执行一系列修复指令。解决此问题的首要环节是验证存储设备是否存在故障,因为这是导致系统无法启动的常见诱因。借助终端指令`/sbin/fsck -f`,可以诊断并纠正文件系统层面的错误。倘若系统在启动过程中检测到文件系统异常,通常会自动执行`fsck`命令,然而,如果系统卡在进度条100%无法继续,手动运行该命令则显得尤为必要。指令`mount -uw /`的功能是将根目录切换为可读写状态,由于系统默认是以只读模式启动的。这一操作的目的是为了在不重新进入正常模式的前提下,对系统进行必要的调整。随后,文档提供了一个关键操作:对存在问题的驱动程序文件进行修改或更名。在Mac系统中,第三方驱动程序一般安装在`/Library/Extensions/`目录下。每个驱动程序都包含一个以.kext为后缀名的文件夹,例如在此案例中的AX88772.kext。通过命令行将故障的.kext文件更名(例如改为.kext.bak),可以临时禁用该驱动程序。这一操作需在命令行环境中完成,首先使用`cd /Library/Exte...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值