Rails アプリを unicorn で動かす。
unicornのインストールと設定
Gemfile に unicorn を追加。
gem 'unicorn'
インストール。
[root@tk2-254-36564 lathercraft]# bundle install --path vendor/bundle
unicorn の設定ファイルはつぎのようにした。
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 timeout 1200
/var/run/unicorn ディレクトリを作成。
[root@tk2-254-36564 lathercraft]# mkdir /var/run/unicorn
Nginxの設定
upstream unicorn-lathercraft {
server unix:/var/run/unicorn/unicorn_lathercraft.sock;
}
server {
# port
listen 80;
# server name
server_name www2.lathercraft.net;
# document root
root /var/www/lathercraft/public;
# 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;
keepalive_timeout 6000;
proxy_connect_timeout 6000;
proxy_read_timeout 6000;
proxy_send_timeout 6000;
client_max_body_size 8192M;
location /kel-mirrors {
root /var/www/lathercraft/recent/;
rewrite '^/kel-mirrors(.*)$' $1;
break;
}
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;
}
}
ログファイル用のディレクトリを作成。
[root@tk2-254-36564 lathercraft]# mkdir /var/log/nginx/www.lathercraft.net
Nginxの再起動とunicornの起動
Nginx を再起動。
[root@tk2-254-36564 lathercraft]# service nginx restart Stopping nginx: [ OK ] Starting nginx: [ OK ]
unicorn を起動。
[root@tk2-254-36564 lathercraft]# export SECRET_KEY_BASE=bundle exec rails secret [root@tk2-254-36564 lathercraft]# bundle exec unicorn_rails -c unicorn.rb -E production I, [2017-05-05T11:58:43.619697 #13435] INFO -- : Refreshing Gem list DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in at /var/www/lathercraft/config/environments/production.rb:23) I, [2017-05-05T11:58:46.475292 #13435] INFO -- : listening on addr=/var/run/unicorn/unicorn_lathercraft.sock fd=10 I, [2017-05-05T11:58:46.475781 #13435] INFO -- : worker=0 spawning... I, [2017-05-05T11:58:46.479485 #13435] INFO -- : worker=1 spawning... I, [2017-05-05T11:58:46.481100 #13438] INFO -- : worker=0 spawned pid=13438 I, [2017-05-05T11:58:46.481436 #13435] INFO -- : master process ready I, [2017-05-05T11:58:46.481630 #13438] INFO -- : worker=0 ready I, [2017-05-05T11:58:46.482935 #13441] INFO -- : worker=1 spawned pid=13441 I, [2017-05-05T11:58:46.483183 #13441] INFO -- : worker=1 ready
さて、これでいいはず。
名前解決
ちゃんと動いているはずだけど、確認するには www2.lathercraft.net の名前解決化できないといけない。これはさくらインターネットのゾーン編集をする。
……結果、無事動いていることを確認。
unicornの自動起動
最後に、OS起動時に unicorn が自動起動するようにしておく。/etc/init.d/unicorn_lathercraft をつぎのようにした。
#!/bin/bash
#
# unicorn_lathercraft Startup script for unicorn.
#
# chkconfig: - 85 15
# description: lathercraft on unicorn start/stop script.
#
set -e
set -u
export PATH=/usr/local/rvm/rubies/ruby-2.3.3/bin:/usr/local/rvm/gems/ruby-2.3.3/bin:$PATH
export RUBYLIB=/usr/local/rvm/gems/ruby-2.3.3:/usr/local/rvm/gems/ruby-2.3.3@global
export GEM_HOME=/usr/local/rvm/gems/ruby-2.3.3:/usr/local/rvm/gems/ruby-2.3.3@global
export GEM_PATH=/usr/local/rvm/gems/ruby-2.3.3:/usr/local/rvm/gems/ruby-2.3.3@global
#echo `ruby -v`
#echo `which ruby`
APP_NAME=lathercraft
APP_ROOT="/var/www/$APP_NAME"
CNF="$APP_ROOT/unicorn.rb"
PID="/var/run/unicorn/unicorn_lathercraft.pid"
ENV=production
UNICORN_OPTS="-c $CNF -E $ENV -D"
old_pid="$PID.oldbin"
#export SECRET_KEY_BASE=`cd /var/www/lathercraft; bundle exec rake secret`
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
export SECRET_KEY_BASE=`bundle exec rake secret`
bundle exec 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
これでいいはず。スタートしてみよう。
[root@tk2-254-36564 init.d]# chmod 755 unicorn_lathercraft [root@tk2-254-36564 init.d]# service unicorn_lathercraft start
OK。
自動起動するように設定。
[root@tk2-254-36564 init.d]# chkconfig unicorn_lathercraft on [root@tk2-254-36564 init.d]# chkconfig --list unicorn_lathercraft unicorn_lathercraft 0:off 1:off 2:on 3:on 4:on 5:on 6:off
これで完了。