如何將Rails項目部署到服務(wù)器上

登錄到服務(wù)器

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源默認(rèn)是外國網(wǎng)站所以網(wǎng)速慢,換國內(nèi)源

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

# 服務(wù)器上
sudo apt-get install mysql-common mysql-client libmysqlclient-dev mysql-server
# 安裝過程中,會讓你輸入密碼。切勿忘記密碼

# 新建一個數(shù)據(jù)庫,其中deployment是你數(shù)據(jù)庫的名字,可根據(jù)需求自行修改
mysql -u root -p
CREATE DATABASE deployment_production CHARACTER SET utf8mb4;

# 退出 mysql console
quit

設(shè)置SSH Key登錄

# 切回本地電腦端
cat ~/.ssh/id_rsa.pub

# 服務(wù)器端執(zhí)行,請確保當(dāng)前用戶為deploy
mkdir ~/.ssh

touch ~/.ssh/authorized_keys

vim ~/.ssh/authorized_keys # 把??cat ~/.ssh/id_rsa.pub的內(nèi)容復(fù)制進(jìn)來并保存

chmod 700 ~/.ssh

chmod 644 ~/.ssh/authorized_keys


禁用密碼登錄(可選,出于安全考慮建議禁用)

# 服務(wù)器上
sudo vi /etc/ssh/sshd_config

# 將`PasswordAuthentication yes` 修改成 `PasswordAuthentication no`
# :wq保存退出后

sudo service ssh restart


到此環(huán)境搭建成功


下面進(jìn)行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,替換成下面的內(nèi)容

# 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,替換成下面的內(nèi)容,??有些配置需要根據(jù)你項目的實際信息更改

# 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" # 放在你服務(wù)器哪個目錄下

# 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為你的服務(wù)器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)
}


把你服務(wù)器的ssh-public-key添加到你項目github的白名單中(不會的請百度或谷歌)


在本機的項目根目錄執(zhí)行下面的命令,這會自動登入遠(yuǎn)端服務(wù)器建立一些 Capistrano 需要的架構(gòu)目錄

cap production deploy:check

你會看到一個 ERROR 說服務(wù)器上缺少一些檔案,需要在遠(yuǎn)端服務(wù)器上新增下面的文件:


在服務(wù)器上新增 /home/deploy/your_project_name/shared/config/database.yml

sudo vim /home/deploy/your_project_name/shared/config/database.yml

#添加下面的內(nèi)容

production:
  adapter: mysql2
  encoding: utf8mb4
  database: your_database_name #請確保你的數(shù)據(jù)庫名存在
  host: localhost
  username: root
  password: your_mysql_password
  

在本機的項目根目錄執(zhí)行下面的命令,這會產(chǎn)生一段亂數(shù)的key,等會要用。

rake secret

在服務(wù)器上新增 /home/deploy/your_project_name/shared/config/secrets.yml 這個文件

sudo vim /home/deploy/your_project_name/shared/config/secrets.yml

#添加下面的內(nèi)容

production:
  secret_key_base: 把??剛生成的亂數(shù)key貼上來


提交代碼并推送

git add .
git commit -m 'auto deploy'
git push

開始自動化部署(第一次部署比較耗時,請耐心等待)

cap production deploy

image

image

如果有錯請根據(jù)錯誤提示解決.也可以在??截圖留言


安裝 Nginx + Passenger 網(wǎng)站服務(wù)器


請到這里查看安裝 Nginx + Passenger 網(wǎng)站服務(wù)器


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容