使用nginx puma和capistrano 3部署Rails应用程序(例子)

・10 分钟阅读

使用Nginx unicorn postgres和Capistrano将Rails应用程序部署到Digital Ocean

首先,在终端创建一个Droplet (Ubuntu 14.04的x64 )和ssh到root:

ssh root@server # replace 'server' with your vps ip

更改你的密码:

passwd

创建新用户并设置权限


adduser username
visudo

root中查找用户权限部分,然后复制行,使它更改为你的新用户:


# User privilege specification
root ALL=(ALL:ALL) ALL
username ALL=(ALL:ALL) ALL

按(CTRL O ),然后按(CTRL X)键保存,并且退出,现在是配置ssh的时候了:

nano /etc/ssh/sshd_config

为了提高安全性,建议你禁用root,并且更改ssh端口(anything between 1025..65536):


Port 22 # change this to whatever port you wish to use
Protocol 2
PermitRootLogin no

sshd_config的末尾,输入:


UseDNS no
AllowUsers username

按(CTRL O),然后按(CTRL X)保存,并且退出。重新加载ssh:

reload ssh

仍不关闭root!用新的用户名打开一个新的shell和ssh (记住端口,或者你被锁定了),

ssh username@server -p 7171

如果一切正常,你可以关闭root ,我们现在需要安装包; 在你的新用户ssh会话中,输入:


sudo apt-get update
sudo apt-get install curl git-core nginx -y

安装rvmrubyrubygems


curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm requirements
rvm install 2.1.0
rvm use 2.1.0 --default
rvm rubygems current

(wince安装可能会失败,它可能要求你下载它的iframe密钥,所以,请重试),

安装railsbundler


gem install rails --no-ri --no-rdoc -V
gem install bundler --no-ri --no-rdoc -V

用Github/Bitbucket握手,并且生成一个公共/私有密钥对:


ssh -T git@github.com
ssh -T git@bitbucket.org
ssh-keygen -t rsa 

将它添加为你的库的部署密钥(说明: github

将你自己的ssh密钥添加到rtc" 在你的本地终端会话中,输入:

cat ~/.ssh/id_rsa.pub | ssh -p 7171 username@server 'cat >> ~/.ssh/authorized_keys'

现在,在你的项目Gemfile中,添加这些和bundle


group :development do
 gem 'capistrano', require: false
 gem 'capistrano-rvm', require: false
 gem 'capistrano-rails', require: false
 gem 'capistrano-bundler', require: false
 gem 'capistrano3-puma', require: false
end

运行:

cap install

为你的root和config文件夹中的deploy.rb文件创建一个,用以下内容替换Capfile的内容:


# Load DSL and Setup Up Stages
require 'capistrano/setup'
require 'capistrano/deploy'

require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'

# 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的内容:


# Change these
server 'server', port: 7171, roles: [:web, :app, :db], primary: true

set :repo_url, 'git@example.com:user/app.git'
set :application, 'appname'
set :user, 'username'
set :puma_threads, [4, 16]
set :puma_workers, 0

# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, false # Change to true if using ActiveRecord

## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5

## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

namespace :puma do
 desc 'Create Directories for Puma Pids and Socket'
 task :make_dirs do
 on roles(:app) do
 execute "mkdir #{shared_path}/tmp/sockets -p"
 execute "mkdir #{shared_path}/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
 after :finishing, :restart
end

# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma

在项目目录中创建config/nginx.conf,并将它添加到(再次替换你的参数):


upstream puma {
 server unix:///home/username/apps/appname/shared/tmp/sockets/appname-puma.sock;
}

server {
 listen 80 default_server deferred;
 # server_name example.com;

 root /home/username/apps/appname/current/public;
 access_log /home/username/apps/appname/current/log/nginx.access.log;
 error_log /home/username/apps/appname/current/log/nginx.error.log info;

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

 try_files $uri/index.html $uri @puma;
 location @puma {
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header Host $http_host;
 proxy_redirect off;

 proxy_pass http://puma;
 }

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

提交更改,推送并进行初始部署:


git add -A
git commit -m "Set up Puma, Nginx & Capistrano"
git push origin master
cap production deploy:initial

如果一切顺利,回到你的ssh会话,连接你的nginx.conf,并且启动,你只需要在(但是每次更新conf文件时,你可能必须重新启动Nginx)上做一次。


sudo rm /etc/nginx/sites-enabled/default
sudo ln -nfs /home/username/apps/appname/current/config/nginx.conf /etc/nginx/sites-enabled/appname
sudo service nginx start

现在,只要你想推出一个新的部署:


git add -A
git commit -m "Deploy Message"
git push
cap production deploy
凉爽拍拍 profile image