7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOS7.0 + Ruby + Unicon環境構築

Last updated at Posted at 2015-02-27

ちょっとづつまとめてみようと思う。
無駄がある気がするので修正依頼求む。

OS状況確認

CentOS 7 (x86_64) with Updates HVM
https://aws.amazon.com/marketplace/pp/B00O7WM7QW
※user:'centos'

Rubyインストール

rbenvインストール

sudo yum install git gcc make openssl-devel libffi-devel patch gcc-c++
cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

rbenvインストール確認

rbenv --version

※rbenv 0.4.0-140-gf87d876等が表示されればインストール完了

ruby-buildインストール

git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
sudo git clone https://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build

##インストール出来るRubyバージョンを確認

sudo rbenv install -l

以下のように表示されればOK
Available versions:
1.8.6-p383
1.8.6-p420
1.8.7-p249

rbenv install 2.2.0
rbenv global 2.2.0
ruby -v

Ruby環境設定

gem update --system
gem install bundler
gem install unicorn
rbenv rehash

※unicornは後でインストールする為不要かも。

Rails インストール

※公開ディレクトリが/opt/domainsの場合

gem install rails
sudo yum install sqlite-devel
mkdir /opt/domains
cd /opt/domains
rails new <<Virtual Domain>>

インストール

※公開ディレクトリが/opt/domainsの場合

cd /opt/domains/
sed -i "s/# gem 'unicorn'/gem 'unicorn'/g" <<Virtual Domain>>/Gemfile
sed -i "s/# gem 'therubyracer', platforms: :ruby/gem 'therubyracer', platforms: :ruby/g" <<Virtual Domain>>/Gemfile
echo "gem 'rb-readline'" >>  <<Virtual Domain>>/Gemfile
cat <<_EOF_ > config/unicorn.rb
rails_root = File.expand_path('../../', __FILE__)

# ワーカーの数
worker_processes 2

# RAILS_ROOT を指定
working_directory rails_root

# ソケット
listen "#{rails_root}/tmp/sockets/unicorn.sock"

# PID
pid    "#{rails_root}/tmp/pids/unicorn.pid"

# ログ
stderr_path "#{rails_root}/log/unicorn_error.log"
stdout_path "#{rails_root}/log/unicorn_stdout.log"

# ダウンタイムなくす
preload_app true

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!

  old_pid = "#{ server.config[:pid] }.oldbin"
  unless old_pid == server.pid
    begin
      Process.kill :QUIT, File.read(old_pid).to_i
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
_EOF_
bundle exec unicorn_rails -c config/unicorn.rb -p 3000 -E development -D

Unicon 起動確認

ps -ef | grep unicorn | grep -v grep

Nginx インストール

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install nginx

Webサーバ(Nginx)起動・公開

sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Webサーバ停止

kill -QUIT `cat tmp/pids/unicorn.pid`

Webサーバ(Nginx)起動確認

ブラウザにてサーバIPにアクセスする
http://<<サーバIP>>

NginxとUnicon連携

cat <<_EOF_ > /etc/nginx/conf.d/vhost-<<サーバドメイン>>.conf
upstream unicorn {
  # nginxとunicornの連携
  server unix:/opt/domains/<<サーバドメイン>>/tmp/sockets/unicorn.sock;
}

server {
    listen 80;
    server_name <<サーバドメイン>>;

    root /opt/domains/<<サーバドメイン>>/public;

    access_log /var/log/nginx/vhost-<<サーバドメイン>>_access.log;
    error_log /var/log/nginx/vhost-<<サーバドメイン>>_error.log;

    client_max_body_size 100m;
    error_page 500 502 503 504 /500.html;

    try_files $uri/index.html $uri @unicorn;

    location @unicorn {
      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://<<サーバIP>>;
  }
}

_EOF_

NginxとUnicon連携確認

sudo systemctl restart nginx.service

残課題

unicorn の自動起動
unicorn のログローテーション
Webサーバ経由で簡単にrailsの起動確認を行う

その他関連URL

[Rails] APサーバの比較検証(Puma, Unicorn, Passenger)
http://blog.s21g.com/articles/2346
apache,nginx × passenger,unicornのベンチをとってみた
http://spring-mt.tumblr.com/post/19282358465/apache-nginx-x-passenger-unicorn

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?