- 公開日:2015年05月27日
- 最終更新日:2015年05月28日
以前の記事でredmineのインストール手順を記載しました。
今回はunicornとnginxを使ってredmineを動かす設定をします。
環境
- centos6.6
- redmine2.2.3(ruby1.9.3, rails3.2.12)
- nginx1.8.0
注意
railsのアプリでunicornとnginxの連携をやったことがないと、理解できない可能性があります。
無理だと思ったら諦めて、インストーラーを使って動作させるのも選択の一つです。redmineは所詮ツールだということを忘れないようにしましょう。
unicornをインストール
// redmineのフォルダに移動 cd /var/www/rails/redmine // Gemfileを開く vi Gemfile
Gemfile
Gemfileにunicornを記載します。
gem "unicorn"
unicornをインストール
bundle install
unicornの導入に成功したかどうかを、仮起動で確かめます。
bundle exec unicorn_rails -l 8081 -E production
localhost:8081でredmineの画面が表示されることが確認できれば、導入は成功です。
設定ファイル
導入に成功したら、細かな設定をするためのconfigファイルを用意します。
// プロジェクトルートに移動 cd /var/www/rails/redmine // unicorn.rbを作成 touch config/unicorn.rb
unicorn.rb
unicorn.rbには以下のように設定を記載します
@app_path = '/var/www/rails/redmine'
worker_processes 2
working_directory "#{@app_path}/"
preload_app true
timeout 30
listen "/tmp/unicorn.sock", :backlog => 64
listen 8081, :tcp_nopush => true
pid "/tmp/unicorn.pid"
stderr_path "#{@app_path}/log/unicorn.stderr.log"
stdout_path "#{@app_path}/log/unicorn.stdout.log"
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
sleep 1
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
動作確認
作成したconfigファイルを指定して動かします。nginxとの連携を考慮し、バックグラウンドで動かします。
// プロジェクトルートに移動 cd /var/www/rails/redmine // unicorn起動 bundle exec unicorn_rails -c config/unicorn.rb -E production -D
動作停止
バックグラウンドで動かしているので、コマンドで停止します。
cat /tmp/unicorn.pid | xargs kill
画面が非表示になればunicornでの起動と停止は成功です。
nginxとunicornの連携
さて次はnginxとunicornを連携する設定をおこないます。
/etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# 以下を追記
upstream unicorn-redmine {
server unix:/tmp/unicorn.sock;
}
include /etc/nginx/conf.d/*.conf;
}
upstream unicorn-redmineの箇所を追加しています。unicornのソケット通信との連携を設定しています。
/etc/nginx/conf.d/default.conf
server {
listen 80;
client_max_body_size 1G;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /var/www/rails/redmine/public;
try_files $uri/index.html $uri.html $uri @app;
}
location @app {
proxy_redirect off;
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_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 600;
# If you don't find the filename in the static files
# Then request it from the unicorn server
if (!-f $request_filename) {
proxy_pass http://unicorn-redmine;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
nginxの設定は以上になります。
nginxの再起動とunicornの起動
// unicorn起動 bundle exec unicorn_rails -c config/unicorn.rb -E production -D // nginx再起動
localhostでredmineのトップページが確認できれば成功です。ポートの8081を指定すると、unicornだけで動作しているのも確認できると思います。
以上
0 件のコメント:
コメントを投稿