1.部署服务器
首先是服务器的基础配置,各种装
先调整服务器时间
sudo dpkg-reconfigure tzdata
然后更新服务器
sudo apt-get update
sudo apt-get upgrade
- rvm
- ruby
- rails
上边三个可以在ruby-china的wiki中学习
- mysql
sudo apt-get install mysql-server-x.x(版本号自己选)
sudo apt-get install libmysqlclient-dev
- nodejs
sudo apt-get install nodejs
- nginx
sudo apt-get install nginx
- 各种依赖
sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline5 libyaml-dev
这个自己看着办吧
2.在rails程序里边添加gem包
在gemfile
里边添加
gem 'unicorn'
gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails'
gem 'capistrano-rvm'
gem 'capistrano-unicorn-nginx'
3.执行cap初始化命令
然后在shell中执行
cap install
会生成很多文件
4.修改配置文件
- capfile文件
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/unicorn_nginx'
# require 'capistrano/passenger'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
- config/deploy文件
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'application_name(项目名称)'
set :repo_url, 'git地址'
# Default branch is :master
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/var/www/文件夹名称'
# 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, :debug
# Default value for :pty is false
set :pty, true
set :use_sudo, 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, 5
# set :rvm_ruby_version, '2.2.3p173' #ruby版本
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
- config/deploy/production.rb(想用staging,换个地方写而已)
server 'xxx.xxx.xxx.xxx(或者写域名,前提,域名已解析)', user: 'username(服务器用户名)', roles: %w{app db web}
5.最后需要执行的命令
生成uicorn和nginx的文件
rails g capistrano:unicorn_nginx:config
执行setup操作
cap production setup
可能上边这一步会有问题,关于没有权限创建文件夹的问题,执行下边那句命令
sudo chown username:username /var/www/
最后的部署命令,之后每次更新只要执行这一句就行了。
cap production deploy
可能出现的问题
/usr/bin/env git no such file or directory capistrano
解决方法:
sduo apt-get install git
可能会碰到的问题
- Gem files will remain installed in......
sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev autoconf libc6-dev ncurses-dev automake libtool
sudo apt-get install libgmp-dev
或者
sudo apt-get install libgmp3-dev