ruby on railsサーバ構成
6342 ワード
最近、各種サーバーの変更は、いくつかのサーバーの配置経験を蓄積した.
まず、私たちはchefなどを採用して私たちのサーバーを配置していません.私たちのサーバーの国内アリクラウド、chefなどのサーバーのソースは国外ec 2にあるので、1台のサーバーに付き添う時間は絶対的な災難なので、手動で来るしかありません.
まず、サーバに必要な環境:1.ruby 2.1 2. データベース3.nginx 4. redis 5. モニタ
一歩一歩:システムを選択して、私達はずっとubuntuの忠実なユーザーで、ここでubuntu 12.04を選択してサーバーにログインして、基礎環境をインストールします
rvmのインストール
モニタリングの構成
インフラストラクチャ
プロジェクトの導入スクリプトを追加します.Capistrano 3です.
railsプロジェクトのGemfileに次の内容を追加し、
railsのプロジェクトディレクトリに
編集config/deploy.rb
config/deploy/productionを編集します.rb
コードをコードウェアハウスにコミットし、cap production deployを使用してシステムを導入した後、cap production puma:monit:config cap production sidekiq:monit:configを使用してWebサーバとバックグラウンドキューを監視します.ここに権限の問題があることに注意してください.エラーを報告するときはサーバにログインしてください/tmpディレクトリの対応するファイルを/etc/monitディレクトリにコピーします.最後に
サーバにログインしてnginxを追加
nginxを再起動します.ここまでシステムの基本的な配置が完了し、プログラムレベルの監視についてはnewrelicを採用しています.もし使用する必要があれば、google newrelicで無料のアカウントを登録して、ヒントに基づいてあなたのプロジェクトを追加することができます.
まず、私たちはchefなどを採用して私たちのサーバーを配置していません.私たちのサーバーの国内アリクラウド、chefなどのサーバーのソースは国外ec 2にあるので、1台のサーバーに付き添う時間は絶対的な災難なので、手動で来るしかありません.
まず、サーバに必要な環境:1.ruby 2.1 2. データベース3.nginx 4. redis 5. モニタ
一歩一歩:システムを選択して、私達はずっとubuntuの忠実なユーザーで、ここでubuntu 12.04を選択してサーバーにログインして、基礎環境をインストールします
sudo apt-get install nodejs redis-server libmysqlclient-dev nginx monit htop
rvmのインストール
curl -sSL https://get.rvm.io | bash -s stable
# rvm
sed -i 's!cache.ruby-lang.org/pub/ruby!ruby.taobao.org/mirrors/ruby!' $rvm_path/config/db
rvm install ruby
モニタリングの構成
インフラストラクチャ
sudo vim /etc/monit/monitrc
は、次の構成を開きます.set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
allow @monit # allow users of group 'monit' to connect (rw)
allow @users readonly # allow users of group 'users' to connect readonly
sudo vim /etc/monit/conf.d/nginx
nginxモニタの追加check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
sudo vim /etc/monit/conf.d/redis
redisモニタの追加check process redis-server
with pidfile "/var/run/redis/redis.pid"
start program = "/etc/init.d/redis-server start"
stop program = "/etc/init.d/redis-server stop"
if 2 restarts within 3 cycles then timeout
if totalmem > 100 Mb then alert
if children > 255 for 5 cycles then stop
if cpu usage > 95% for 3 cycles then restart
if failed host 127.0.0.1 port 6379 then restart
if 5 restarts within 5 cycles then timeout
プロジェクトの導入スクリプトを追加します.Capistrano 3です.
railsプロジェクトのGemfileに次の内容を追加し、
bundle install
を実行します.group :development do
gem 'capistrano-rails'
gem 'capistrano-rvm'
gem 'capistrano-sidekiq', '~> 0.3.3'
gem 'capistrano3-puma'
end
group :production do
gem 'newrelic_rpm'
gem 'puma'
end
railsのプロジェクトディレクトリに
cap install
を使用して配置スクリプトを追加し、Capfile
を編集すると次のように表示されます.# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/sidekiq'
require 'capistrano/sidekiq/monit'
require 'capistrano/puma'
require 'capistrano/puma/monit'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
編集config/deploy.rb
# config valid only for Capistrano 3.1
lock '3.2.1'
set :application, ' '
set :repo_url, ' git '
set :deploy_to, ' '
set :scm, :git
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :keep_releases, 5
after 'deploy:publishing', 'deploy:restart'
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:web) do
within release_path do
with rails_env: fetch(:rails_env) do
# execute :rake, "up_assets"
end
end
end
end
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
end
end
after :finishing, 'deploy:cleanup'
before :finishing, 'deploy:restart'
after 'deploy:rollback', 'deploy:restart'
end
config/deploy/productionを編集します.rb
# Simple Role Syntax
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.1.2'
role :app, %w{[email protected]}
role :web, %w{[email protected]}
role :db, %w{[email protected]}
set :branch, "master"
server 'youdoman.com', user: 'deployer', roles: %w{app web}, my_property: :my_value
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" #accept array for multi-bind
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, 120]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, false
set :puma_preload_app, true
コードをコードウェアハウスにコミットし、cap production deployを使用してシステムを導入した後、cap production puma:monit:config cap production sidekiq:monit:configを使用してWebサーバとバックグラウンドキューを監視します.ここに権限の問題があることに注意してください.エラーを報告するときはサーバにログインしてください/tmpディレクトリの対応するファイルを/etc/monitディレクトリにコピーします.最後に
sudo monit reload
を使用してモニタを再ロードし、monit status
を使用してシステムの状況を確認する.サーバにログインしてnginxを追加
sudo vim /etc/nginx/sites-enabled/you_project_name
upstream app {
server unix:/xxx/xxx/puma.sock; # puma sock , ,
}
server {
listen 80;
server_name youserver_name;
root /xxx/xxx/current/public;
try_files $uri/index.html $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
nginxを再起動します.ここまでシステムの基本的な配置が完了し、プログラムレベルの監視についてはnewrelicを採用しています.もし使用する必要があれば、google newrelicで無料のアカウントを登録して、ヒントに基づいてあなたのプロジェクトを追加することができます.