红联Linux门户
Linux帮助

Linux下Unicorn服务器配置

发布时间:2015-04-21 10:06:44来源:linux网站作者:linux人

Unicorn 是什么?
1. 为 Rack 应用程序设计的 HTTP server
2. 是一个利用Unix的高级特性开发的
3. 为具备低延迟,高带宽的连接的客户服务


特性:
1. 为 Rack, Unix, 快速的客户端和易调试而设计。
2. 完全兼容 Ruby 1.8 和 1.9。
3. 进程管理:Unicorn 会获取和重启因应用程序出错导致死亡的任务,不需要自己管理多个进程和端口。Unicorn 可以产生和管理任何数量的任务进程。
4. 负载均衡完全由操作系统(Unix)核心完成。在繁忙的任务进程时,请求也不会堆积。
5. 不需要关心应用程序是否是线程安全的,workers 运行在特们自己独立的地址空间,且一次只为一个客户端服务。
6. 支持所有的 Rack 应用程序。
7. 使用 USR1 信号来固定重复打开应用程序的所有日志文件。Unicorn 也可以逐步的确定一个请求的多行日志放在同一个文件中。
8. nginx 式的二进制升级,不丢失连接。你可以升级 Unicorn、你的整个应用程序、库、甚至 Ruby 编辑器而不丢失客户端连接。
9. 在 fork 进程时如果由特殊需求可以使用 before_fork 和 after_fork 。如果“preload_app“ 为 false 时,则不能使用。
10. 可以使用 copy-on-wirte-friendly 内存管理来节约内容(通过设置 “preload_app" 为 true )。
11. 可以监听多接口,包括:UNIX sockets,每个 worker process 也可以在简单调试时通过 after_fork 钩子绑定到私有的端口。
12. 配置使用简单易用的 Ruby DSL。


Linux下Unicorn服务器安装配置:

gem install unicorn

给工程创建一个unicorn配置文件

new_sxcoalts2.0/config/unicorn.rb
内容如下:
app_path = "/work/new_sxcoalts2.0" #程序路径
listen 3000 # 端口号
worker_processes 2 # cpu核数
pid "#{app_path}/tmp/pids/unicorn.pid"
stderr_path "#{app_path}/log/unicorn.log"
stdout_path "#{app_path}/log/unicorn.log"
rails_env = 'production'


启动:
进入到工程根目录 cd /work/new_sxcoalts2.0/
unicorn_rails -c /work/new_sxcoalts2.0/config/unicorn.rb
参数-c 意思为执行后面配置文件里的内容

停止服务:
后台服务:  Kill 进程
命令行服务:  ctrl + c


建立启动,关闭服务:
创建工程配置文件夹:
/etc/unicorn
在此目录下添加所有需要的工程配置(可放置多个)
例如:project1.conf
内容为
RAILS_ROOT=/work/project1
RAILS_ENV=production


编写unicorn 启动脚本
在/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/new_sxcoalts_2.0.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/new_sxcoalts_2.0.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="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


执行命令:
/etc/init.d/unicorn_init start/stop/restart