LoginSignup
2
1

More than 5 years have passed since last update.

rails5.0+nginx1.11.8+puma3.6.2+CentOS6.7でrails環境構築

Last updated at Posted at 2017-01-20

忘れちゃうんでメモ

構成

ruby 2.3.1
rails 5.0.1
nginx
capistrano
puma
conoha - centOS 6.7 64bit

目次

VPS

vpsは検討の末conohaのvpsを使用
ssh接続のための接続等は省略しておきます。
とりあえずrootでログイン

ログイン

ssh root@xxx.xxx.xx.xx (xxx.xxx.xx.xxはサーバのIPアドレス)

rbenvをinstall

rubyを管理するためにrbenvをインストール
rbenvをinstallをしてPATHを通す

$ rbenv install 2.3.1
$ rbenv rehash
$ rbenv global 2.3.1

環境設定の末行に追記する(server側)

#~/.bash_profile

export PATH="~/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

~/.bash_profileを再読み込み

$ source .bash_profile

nginxをinstall及び設定

yum でインストールするため、yumのリポジトリを登録

sudo rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

Mainlineをインストールしたいので、「/etc/yum.repos.d/nginx.repo 」を修正
stable の場合 nginx.repo は修正しなくて良いらしい。
参照:http://qiita.com/ymaru/items/4a56f1619c960981180f

# vim /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/6/$basearch/
gpgcheck=0
enabled=1

installする

sudo yum install nginx
#/etc/nginx/conf.d/hoge.conf

upstream #{app_name} {
  # uncomment the following line if multiple application servers are used.
  # this will force nginx to send requests from the same client to the same
  # application server.
  # ip_hash;
  server unix:/var/www/#{app_name}/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name colors-link.com;
  root /var/www/#{app_name}/current/public;
  access_log /var/www/#{app_name}/current/log/nginx.access.log;
  error_log /var/www/#{app_name}/current/log/production.log;

  location ^~ /assets/ {
    # gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @#{app_name};
  location @#{app_name} {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://#{app_name};
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

起動する

sudo service nginx start

ちょいハマったところ

  • 設定を変更したらrestartする
  • server unix:
  • server_name
# nginxをrestartする
$ sudo service nginx restart

Capistranoでデプロイ

Capistrano設定

#Gemfile

group :development do
  gem 'capistrano'
  gem 'capistrano-rails'
  gem 'capistrano3-puma'
  gem 'capistrano-rbenv'
  gem 'capistrano-rbenv-vars'
  gem 'capistrano-bundler'
end
bundle install
bundle exec cap install
#Capfile

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/rbenv'
require 'capistrano/bundler'
require 'capistrano/puma'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

#deploy.rb

# config valid only for current version of Capistrano
lock '3.7.1'

server "#{ip_address}", user: 'root', roles: %w{app db web}

set :application, '#{app_name}'
set :repo_url, 'git@bitbucket.org:shotay/#{app_name}.git'

set :pty, true
# rbenvの設定
set :rbenv_type, :user
set :rbenv_ruby, '2.3.1'
# set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all

set :deploy_to, '/var/www/#{app_name}'
set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix:#{shared_path}/tmp/sockets/puma.sock"
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [0, 16]
set :puma_workers, 0
set :puma_init_active_record, true
set :puma_preload_app, true


# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/my_app_name


# Default value for :scm is :git
# set :scm, :git

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
set :log_level, :warn

# Default value for :pty is false
# set :pty, true

# Default value for :linked_files is []
# set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')

# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
set :keep_releases, 3

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir /var/www/#{app_name}/shared/tmp/sockets -p"
      execute "mkdir /var/www/#{app_name}/shared/tmp/pids -p"
    end
  end
  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
end



$ bundle exec cap production deploy:initial

ちょいハマったところ

  • bundle installの部分でエラーが出る時下記二つ実行
yum install gcc-c++
yum install mysql-devel
  • 他の解説でよく使われていたset :scm, :gitを使用すると上手く動かなかったため、Capfileに下記を追加した
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

ログの確認

$ tail -f /var/www/#{app_name}/shared/log/production.log

まとめ

VPSを借りての構築が初めてだったので結構ハマった。

2
1
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
2
1