如何将Rails项目部署到服务器上
2019-11-25 本文已影响0人
苑畅
登录到服务器
ssh username@ip
更新并新建用户
sudo apt-get update
sudo apt-get upgrade -y
adduser deploy --ingroup sudo
安装必要的依赖包
sudo apt-get install -y vim git libmysqlclient-dev build-essential git-core bison openssl libreadline6-dev curl zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 autoconf libc6-dev libpcre3-dev curl libcurl4-nss-dev libxml2-dev libxslt-dev imagemagick nodejs libffi-dev
安装rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
git clone https://github.com/andorchen/rbenv-china-mirror.git ~/.rbenv/plugins/rbenv-china-mirror # 换rbenv镜像源,让下载速度更快
rbenv install 2.4.6
rbenv global 2.4.6
ruby -v
由于gem源默认是外国网站所以网速慢,换国内源
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
gem install bundler
bundle config mirror.https://rubygems.org https://gems.ruby-china.com
安装MySQL
# 服务器上
sudo apt-get install mysql-common mysql-client libmysqlclient-dev mysql-server
# 安装过程中,会让你输入密码。切勿忘记密码
# 新建一个数据库,其中deployment是你数据库的名字,可根据需求自行修改
mysql -u root -p
CREATE DATABASE deployment_production CHARACTER SET utf8mb4;
# 退出 mysql console
quit
设置SSH Key登录
# 切回本地电脑端
cat ~/.ssh/id_rsa.pub
# 服务器端执行,请确保当前用户为deploy
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
vim ~/.ssh/authorized_keys # 把👆cat ~/.ssh/id_rsa.pub的内容复制进来并保存
chmod 700 ~/.ssh
chmod 644 ~/.ssh/authorized_keys
禁用密码登录(可选,出于安全考虑建议禁用)
# 服务器上
sudo vi /etc/ssh/sshd_config
# 将`PasswordAuthentication yes` 修改成 `PasswordAuthentication no`
# :wq保存退出后
sudo service ssh restart
到此环境搭建成功
下面进行Capistrano自动化部署
修改项目中Gemfile 加入 capistrano,添加下面的gem
gem 'mysql2'
# 往下翻,找到group :development, :test do中添加下面的代码
group :development, :test do
gem 'capistrano-rails'
gem 'capistrano-passenger'
gem 'capistrano-rbenv'
end
安装capistrano
bundle install
新增capistrano配置档案
cap install
编辑项目根目录下的Capfile,替换成下面的内容
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
require "capistrano/rails"
# Include default deployment tasks
require "capistrano/passenger"
# 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/rails/collection'
# require "capistrano/chruby"
# require "capistrano/bundler"
# require "capistrano/rails/assets"
# require "capistrano/rails/migrations"
# require "capistrano/passenger"
set :rbenv_type, :user
set :rbenv_ruby, '2.4.6'
# Load 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 for current version and patch releases of Capistrano
`ssh-add`
lock "~> 3.11.0"
set :application, "myblog"
set :repo_url, "your_project_git_url" # 你项目的github地址,这种格式 git@example.com:me/my_repo.git
set :rbenv_path, '/home/deploy/.rbenv'
# 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, "/home/deploy/your_project_name" # 放在你服务器哪个目录下
# Default value for :format is :airbrussh.
# set :format, :airbrussh
# You can configure the Airbrussh format using :format_options.
# These are the defaults.
# set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
append :linked_files, "config/database.yml","config/secrets.yml"
# Default value for linked_dirs is []
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
set :passenger_restart_with_touch, true
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for local_user is ENV['USER']
# set :local_user, -> { `git config user.name`.chomp }
# Default value for keep_releases is 5
set :keep_releases, 5
# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure
修改config/deploy/production.rb
set :branch, "master"
# xxx为你的服务器ip
server "xxx.xxx.xxx.xxx", user: "deploy", roles: %w{app db web}, my_property: :my_value
# set :rbenv_ruby, "2.3.8"
set :ssh_options, {
keys: %w(~/.ssh/id_rsa),
forward_agent: true,
auth_methods: %w(publickey)
}
把你服务器的ssh-public-key添加到你项目github的白名单中(不会的请百度或谷歌)
在本机的项目根目录执行下面的命令,这会自动登入远端服务器建立一些 Capistrano 需要的架构目录
cap production deploy:check
你会看到一个 ERROR 说服务器上缺少一些档案,需要在远端服务器上新增下面的文件:
在服务器上新增 /home/deploy/your_project_name/shared/config/database.yml
sudo vim /home/deploy/your_project_name/shared/config/database.yml
#添加下面的内容
production:
adapter: mysql2
encoding: utf8mb4
database: your_database_name #请确保你的数据库名存在
host: localhost
username: root
password: your_mysql_password
在本机的项目根目录执行下面的命令,这会产生一段乱数的key,等会要用。
rake secret
在服务器上新增 /home/deploy/your_project_name/shared/config/secrets.yml 这个文件
sudo vim /home/deploy/your_project_name/shared/config/secrets.yml
#添加下面的内容
production:
secret_key_base: 把👆刚生成的乱数key贴上来
提交代码并推送
git add .
git commit -m 'auto deploy'
git push
开始自动化部署(第一次部署比较耗时,请耐心等待)
cap production deploy
image
image