<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>船长日志 &#187; unicorn</title>
	<atom:link href="http://www.cslog.cn/tag/unicorn/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cslog.cn</link>
	<description>最让我激动的是不知道下一个星球上能发现什么...</description>
	<lastBuildDate>Wed, 30 Jul 2025 16:06:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>使用Unicorn替代Mongrel作为Ruby on Rails的服务器</title>
		<link>http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/</link>
		<comments>http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 16:54:41 +0000</pubDate>
		<dc:creator>船长</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[站长文档]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[unicorn]]></category>
		<category><![CDATA[服务器]]></category>
		<category><![CDATA[网站开发]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=727</guid>
		<description><![CDATA[先前的开发的Ruby on Rails网站使用的服务程序是Mongrel + N &#8230; <a href="http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>先前的开发的Ruby on Rails网站使用的服务程序是<a title="在ubuntu6.06上安装apache2.2X + mysql + PHP + Ruby on Rails + Mongrel Cluster | 船长日志" href="http://www.cslog.cn/Content/ubuntu606_mysql_apache2_ruby_rails_mongrel_cluster/">Mongrel + Nginx</a>， 现在用了Rails 3， 发现Mongrel对它有兼容问题， 所以要换一个。 虽然现在<a title="Overview — Phusion Passenger™ (a.k.a. mod_rails / mod_rack)" href="http://www.modrails.com/">Phusion Passenger</a>大行其道， 但使用前要重新编译Nginx。 为了避免重新安装nginx，我找到mongrel的替代品<a title="File: README [Unicorn: Rack HTTP server for fast clients and Unix]" href="http://unicorn.bogomips.org/">Unicorn</a>。 没想到unicorn的设置非常方便。 我记录在这里供大家参考：</p>
<p>安装unicorn:<br />
<code><br />
sudo gem install unicorn<br />
</code></p>
<p>创建网站配置文件(myproject是项目名称):<br />
<code><span id="more-727"></span><br />
sudo mkdir /etc/unicorn<br />
cd /etc/unicorn/<br />
sudo nano /etc/unicorn/myproject.conf<br />
</code></p>
<p>内容如下：<br />
<code><br />
RAILS_ROOT=/www/myproject<br />
RAILS_ENV=production<br />
</code></p>
<p>在网站里再创建一个unicorn配置文件<br />
<code><br />
nano /www/myproject/config/unicorn.rb<br />
</code></p>
<p>内容如下：<br />
<code><br />
# Minimal sample configuration file for Unicorn (not Rack) when used<br />
# with daemonization (unicorn -D) started in your working directory.<br />
#<br />
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete<br />
# documentation.<br />
# See also http://unicorn.bogomips.org/examples/unicorn.conf.rb for<br />
# a more verbose configuration using more features.</code></p>
<p><code> </code></p>
<p><code>app_path = "/www/myproject"</code></p>
<p><code> </code></p>
<p><code>listen 8080 # by default Unicorn listens on port 8080<br />
worker_processes 2 # this should be &gt;= nr_cpus<br />
pid "#{app_path}/tmp/pids/unicorn.pid"<br />
stderr_path "#{app_path}/log/unicorn.log"<br />
stdout_path "#{app_path}/log/unicorn.log"<br />
</code></p>
<p>设置unicorn启动脚本：<br />
<code><br />
sudo nano /etc/init.d/unicorn_init<br />
</code><br />
脚本内容：<br />
<code><br />
#!/bin/sh<br />
#<br />
# init.d script for single or multiple unicorn installations. Expects at least one .conf<br />
# file in /etc/unicorn<br />
#<br />
# Modified by jay@gooby.org http://github.com/jaygooby<br />
# based on http://gist.github.com/308216 by http://github.com/mguterl<br />
#<br />
## A sample /etc/unicorn/my_app.conf<br />
##<br />
## RAILS_ENV=production<br />
## RAILS_ROOT=/var/apps/www/my_app/current<br />
#<br />
# This configures a unicorn master for your app at /var/apps/www/my_app/current running in<br />
# production mode. It will read config/unicorn.rb for further set up.<br />
#<br />
# You should ensure different ports or sockets are set in each config/unicorn.rb if<br />
# you are running more than one master concurrently.<br />
#<br />
# If you call this script without any config parameters, it will attempt to run the<br />
# init command for all your unicorn configurations listed in /etc/unicorn/*.conf<br />
#<br />
# /etc/init.d/unicorn start # starts all unicorns<br />
#<br />
# If you specify a particular config, it will only operate on that one<br />
#<br />
# /etc/init.d/unicorn start /etc/unicorn/my_app.conf</code></p>
<p><code> </code></p>
<p><code>set -e</code></p>
<p><code>sig () {<br />
test -s "$PID" &amp;&amp; kill -$1 `cat "$PID"`<br />
}</p>
<p>oldsig () {<br />
test -s "$OLD_PID" &amp;&amp; kill -$1 `cat "$OLD_PID"`<br />
}</p>
<p>cmd () {</p>
<p>case $1 in<br />
start)<br />
sig 0 &amp;&amp; echo &gt;&amp;2 "Already running" &amp;&amp; exit 0<br />
echo "Starting"<br />
$CMD<br />
;;<br />
stop)<br />
sig QUIT &amp;&amp; echo "Stopping" &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Not running"<br />
;;<br />
force-stop)<br />
sig TERM &amp;&amp; echo "Forcing a stop" &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Not running"<br />
;;<br />
restart|reload)<br />
sig USR2 &amp;&amp; sleep 5 &amp;&amp; oldsig QUIT &amp;&amp; echo "Killing old master" `cat $OLD_PID` &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Couldn't reload, starting '$CMD' instead"<br />
$CMD<br />
;;<br />
upgrade)<br />
sig USR2 &amp;&amp; echo Upgraded &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Couldn't upgrade, starting '$CMD' instead"<br />
$CMD<br />
;;<br />
rotate)<br />
sig USR1 &amp;&amp; echo rotated logs OK &amp;&amp; exit 0<br />
echo &gt;&amp;2 "Couldn't rotate logs" &amp;&amp; exit 1<br />
;;<br />
*)<br />
echo &gt;&amp;2 "Usage: $0 "<br />
exit 1<br />
;;<br />
esac<br />
}</p>
<p>setup () {</p>
<p>echo -n "$RAILS_ROOT: "<br />
cd $RAILS_ROOT || exit 1<br />
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid<br />
export OLD_PID="$PID.oldbin"</p>
<p>CMD="/usr/bin/unicorn_rails -c config/unicorn.rb -E $RAILS_ENV -D"<br />
}</p>
<p>start_stop () {</p>
<p># either run the start/stop/reload/etc command for every config under /etc/unicorn<br />
# or just do it for a specific one</p>
<p># $1 contains the start/stop/etc command<br />
# $2 if it exists, should be the specific config we want to act on<br />
if [ $2 ]; then<br />
. $2<br />
setup<br />
cmd $1<br />
else<br />
for CONFIG in /etc/unicorn/*.conf; do<br />
# import the variables<br />
. $CONFIG<br />
setup</p>
<p># run the start/stop/etc command<br />
cmd $1<br />
done<br />
fi<br />
}</p>
<p></code></p>
<p><code>ARGS="$1 $2"<br />
start_stop $ARGS<br />
</code><br />
注意将里面的/usr/bin/unicorn_rails 换成你系统中unicorn_rails程序的实际路径。</p>
<p>设置unicorn_init文件属性:<br />
<code><br />
sudo chmod 755 /etc/init.d/unicorn_init<br />
</code></p>
<p>启动unicorn:<br />
<code><br />
/etc/init.d/unicorn_init<br />
</code></p>
<p>修改nginx的配置文件，加入unicorn的代理设置：<br />
<code><br />
upstream myproject_mongrel {<br />
server 127.0.0.1:8080 fail_timeout=0;<br />
}<br />
</code><br />
这部分跟使用mongrel的类似的。</p>
<p>这样unicorn的设置就完成了。 刚设置好，感觉unicorn跟mongrel一样， 都是比较吃内存的， 一启动就占了50M. 不知道会不会也像mongrel一样把内存吃爆， 会得话得设置监控软件（如god）看住它。
<div style="margin-top: 15px; font-style: italic">
<p>转载请注明: 转自<a href="http://www.cslog.cn/">船长日志</a>, 本文链接地址: <a href="http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/">http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
