さくらVPS:Nginx+unicorn+Rails+MySQL(その2)

つづき。

unicornのインストールと設定

インストールは gem コマンド一発。

[root@www2465uo Lathercraft]# gem install unicorn

設定ファイルを ${RAILS_ROOT}/config の中に作る。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
worker_processes 2
working_directory "/var/www/Lathercraft"
listen "/var/run/unicorn/unicorn_lathercraft.sock"
pid "/var/run/unicorn/unicorn_lathercraft.pid"
preload_app true
worker_processes 2 working_directory "/var/www/Lathercraft" listen "/var/run/unicorn/unicorn_lathercraft.sock" pid "/var/run/unicorn/unicorn_lathercraft.pid" preload_app true
worker_processes 2
working_directory "/var/www/Lathercraft"

listen "/var/run/unicorn/unicorn_lathercraft.sock"
pid "/var/run/unicorn/unicorn_lathercraft.pid"

preload_app true

他にもいろいろ書けるみたいだけど、これで最低限のよう。sock や pid のためのディレクトリを作る。

[root@www2465uo Lathercraft]# mkdir /var/run/unicorn
[root@www2465uo Lathercraft]# chmod 777 /var/run/unicorn

ファイル自体は勝手に作ってくれるらしい。

unicornの起動

[root@www2465uo Lathercraft]# unicorn_rails -c /var/www/Lathercraft/config/unicorn.rb -E production -D

Nginxの設定

/etc/nginx/conf.d/www.lathercraft.new.conf を次のようにした。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
upstream unicorn-lathercraft {
server unix:/var/run/unicorn/unicorn_lathercraft.sock;
}
server {
# port
listen 80;
# server name
server_name www.lathercraft.net;
# document root
root /var/www/Lathercraft;
# index
#index index.php index.html index.htm;
# log files
access_log /var/log/nginx/www.lathercraft.net/access.log main;
error_log /var/log/nginx/www.lathercraft.net/error.log warn;
location / {
root /var/www/Lathercraft;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://unicorn-lathercraft;
}
}
upstream unicorn-lathercraft { server unix:/var/run/unicorn/unicorn_lathercraft.sock; } server { # port listen 80; # server name server_name www.lathercraft.net; # document root root /var/www/Lathercraft; # index #index index.php index.html index.htm; # log files access_log /var/log/nginx/www.lathercraft.net/access.log main; error_log /var/log/nginx/www.lathercraft.net/error.log warn; location / { root /var/www/Lathercraft; proxy_set_header X-Real-IP remoteaddr;proxysetheaderXForwardedForproxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://unicorn-lathercraft; } }
upstream unicorn-lathercraft {
    server unix:/var/run/unicorn/unicorn_lathercraft.sock;
}

server {
    # port
    listen 80;

    # server name
    server_name www.lathercraft.net;

    # document root
    root /var/www/Lathercraft;

    # index
    #index index.php index.html index.htm;

    # log files
    access_log /var/log/nginx/www.lathercraft.net/access.log main;
    error_log /var/log/nginx/www.lathercraft.net/error.log warn;

    location / {
        root /var/www/Lathercraft;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_pass http://unicorn-lathercraft;
    }

}

これで Nginx を再起動。

[root@www2465uo www]# service nginx restart
nginx を停止中:                                            [  OK  ]
nginx を起動中:                                            [  OK  ]

ブラウザからアクセスすると、アプリのホームページが表示された!

lathercraft-home-20140814

unicornの自動起動

さいごに、OS を起動した時に unicorn も自動起動するようにしておく。/etc/init.d/unicorn_lathercraft を次のようにした。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
#
# unicorn_lathercraft Startup script for unicorn.
#
# chkconfig: - 85 15
# description: lathercraft on unicorn start/stop script.
#
set -u
set -e
export PATH=/usr/local/rvm/rubies/ruby-2.0.0-p481/bin:/usr/local/rvm/gems/ruby-2.0.0-p481/bin:$PATH
APP_NAME=Lathercraft
APP_ROOT="/var/www/$APP_NAME"
CNF="$APP_ROOT/config/unicorn.rb"
PID="/var/run/unicorn/unicorn_lathercraft.pid"
ENV=production
UNICORN_OPTS="-c CNFEENV -D"
old_pid="$PID.oldbin"
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $old_pid && kill -$1 `cat $old_pid`
}
case ${1-help} in
start)
sig 0 && echo >&2 "Already running" && exit 0
cd $APP_ROOT ; unicorn_rails $UNICORN_OPTS
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting instead"
unicorn_rails $UNICORN_OPTS
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting instead"
unicorn_rails $UNICORN_OPTS
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
#!/bin/bash # # unicorn_lathercraft Startup script for unicorn. # # chkconfig: - 85 15 # description: lathercraft on unicorn start/stop script. # set -u set -e export PATH=/usr/local/rvm/rubies/ruby-2.0.0-p481/bin:/usr/local/rvm/gems/ruby-2.0.0-p481/bin:PATHAPPNAME=LathercraftAPPROOT="/var/www/APP_NAME" CNF="APPROOT/config/unicorn.rb"PID="/var/run/unicorn/unicornlathercraft.pid"ENV=productionUNICORNOPTS="cCNF -E ENVD"oldpid="PID.oldbin" cd Misplaced &{1-help} in start) sig 0 && echo >&2 "Already running" && exit 0 cd APPROOT;unicornrailsUNICORN_OPTS ;; stop) sig QUIT && exit 0 echo >&2 "Not running" ;; force-stop) sig TERM && exit 0 echo >&2 "Not running" ;; restart|reload) sig HUP && echo reloaded OK && exit 0 echo >&2 "Couldn't reload, starting instead" unicorn_rails Misplaced &UNICORN_OPTS ;; rotate) sig USR1 && echo rotated logs OK && exit 0 echo >&2 "Couldn't rotate logs" && exit 1 ;; *) echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>" exit 1 ;; esac
#!/bin/bash
#
# unicorn_lathercraft Startup script for unicorn.
#
# chkconfig: - 85 15
# description: lathercraft on unicorn start/stop script.
#

set -u
set -e

export PATH=/usr/local/rvm/rubies/ruby-2.0.0-p481/bin:/usr/local/rvm/gems/ruby-2.0.0-p481/bin:$PATH
APP_NAME=Lathercraft
APP_ROOT="/var/www/$APP_NAME"
CNF="$APP_ROOT/config/unicorn.rb"
PID="/var/run/unicorn/unicorn_lathercraft.pid"
ENV=production
UNICORN_OPTS="-c $CNF -E $ENV -D"
old_pid="$PID.oldbin"

cd $APP_ROOT || exit 1

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

case ${1-help} in
start)
sig 0 && echo >&2 "Already running" && exit 0
cd $APP_ROOT ; unicorn_rails $UNICORN_OPTS
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting instead"
unicorn_rails $UNICORN_OPTS
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting instead"
unicorn_rails $UNICORN_OPTS
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac

</start|stop|restart|upgrade|rotate|force-stop>

[root@www2465uo Lathercraft]# vim /etc/init.d/unicorn_lathercraft
[root@www2465uo Lathercraft]# chmod +x /etc/init.d/unicorn_lathercraft
[root@www2465uo Lathercraft]# chkconfig unicorn_lathercraft on
[root@www2465uo Lathercraft]# chkconfig --list unicorn_lathercraft
unicorn_lathercraft	0:off	1:off	2:on	3:on	4:on	5:on	6:off

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください