ruby on rails 服务器配置

近期各种服务器变更, 积累了一些服务器的配置经验.

首先, 我们没有采用chef等来部署我们的服务器, 因为我们的服务器的国内阿里云, chef等服务器的源在国外ec2, 陪一台服务器的时间绝对的灾难, 所以只能手动来了.

首先是我们的服务器需要的环境:
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 server和后台队列进行监控, 注意这里可以有权限问题, 报错的时候可以登录到服务器吧/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 注册一个免费的帐号, 根据提示添加你的项目中.

你可能感兴趣的:(ruby-on-rails,web部署)