2020年最新CentOS7部署Rails5

一、創建一個deploy用戶

$ useradd -m -s /bin/bash deploy
$ sudo echo "deploy ALL=(ALL:ALL) ALL" >> /etc/sudoers
# 切換到deploy用戶
$ sudo su deploy

# 進入`家`目錄
$ cd ~

二、使用祕鑰登錄服務器

# 把本地的ssh公鑰文件安裝到遠程主機對應的賬戶下:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub deploy@你的ip

現在你再用SSH連服務器,直接ssh deploy@你的ip,不再需要輸入密碼了。

三、禁用密碼登錄(可選)

# 服務器上
$ sudo vi /etc/ssh/sshd_config

# 將`PasswordAuthentication yes` 修改成 `PasswordAuthentication no` 後:wq退出

四、服務器基礎準備工作

# 更新
$ sudo yum update
# 安裝Rails所必須的各種常見依賴
$ sudo yum install -y epel-release git openssl-devel readline-devel zlib-devel libxml2-devel libxslt-devel libvirt-devel tar gcc-c++ pcre-devel unzip bzip2 nodejs;

五、使用rbenv安裝Ruby

# 服務器上
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc

rbenv需要ruby-build,才能安裝ruby。所以現在來安裝它。

$ 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

# 看一下,現在可安裝的Ruby版本
$ rbenv install -l

# 解決rbenv安裝慢問題,這裏可以使用一個"安道"寫的rbenv插件,讓rbenv直接使用中國的鏡像站點下載
$ git clone https://github.com/andorchen/rbenv-china-mirror.git "$(rbenv root)"/plugins/rbenv-china-mirror

# 我寫文章這個時間,最新的是2.6.2
$ rbenv install 2.6.2

# 設置成全局默認使用
$ rbenv global 2.6.2

# 看一下是否裝成功
$ ruby -v

# 使用Ruby China的RubyGems(境外服務器請略過)
$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
$ gem sources -l
# 確保只有 gems.ruby-china.com

# 接着安裝 bundler gem
$ gem install bundler

# 同樣使用gems.ruby-china.com
$ bundle config mirror.https://rubygems.org https://gems.ruby-china.com

六、安裝PostgreSQL

# 服務器上

# 通過運行以下命令將PostgreSQL Yum存儲庫添加到CentOS 7系統中:
$ sudo yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# 安裝PostgreSQL Server和客戶端軟件包
$ sudo yum -y install postgresql11-server postgresql11 postgresql-devel

# 初始化數據庫並設置成自啓
$ sudo /usr/pgsql-11/bin/postgresql-11-setup initdb  # 初始化數據庫
$ sudo systemctl start postgresql-11    # 啓動數據庫
$ sudo systemctl enable postgresql-11   # 設置爲自啓

# PostgreSQL 11配置文件是/var/lib/pgsql/11/data/postgresql.conf這個位置
# 接下來是對數據庫的操作,首先使用默認超級用戶登錄進入命令行
$ sudo -u postgres psql

# 新建deploy用戶:
postgres=# create user deploy CREATEDB password '用戶密碼';

# 刪除用戶(可選):
postgres=# drop user 用戶名;

# 創建數據庫(可選)
postgres=# create database 數據庫名; 

# 刪除數據庫(可選)
postgres=# drop database 數據庫名; 

# 退出命令行
postgres=# \q

七、安裝 Nginx

# 通過運行以下命令安裝nginx,最好安裝1.16以上的版本
$ sudo yum install nginx

# 啓動nginx服務
$ systemctl start nginx.service

# 設置開機自啓動
$ systemctl enable nginx.service

八、進入Rails項目,修改gemfile,安裝Capistrano以及插件

Gemfilegroup :development中,添加如下代碼,這些都是capistrano的插件

group :development do
  gem 'capistrano-rails'
  gem 'capistrano3-puma'
  gem 'capistrano-rbenv'
end
# 改完後,命令行進入自己項目中,執行
$ bundle install

九、配置Capistrano

1. 生成capistrano的相關配置文件
$ cap install

2. 編輯 Capfile(項目的根目錄下),見示例圖1
# 加上這三行
require "capistrano/rails"
require 'capistrano/puma'
install_plugin Capistrano::Puma

# 去掉這個前面的`#`號 
require "capistrano/rbenv"

3. 編輯 config/deploy.rb,見示例圖2
# 最頂上加這行,注意是「`」號而不是單引號「'」
# 如果你對ssh-add有興趣,你可以去讀這一篇。https://ihower.tw/blog/archives/7837
`ssh-add`

# 項目名稱
set :application, "deployment"

# git倉庫地址
set :repo_url, "[email protected]:aaronryuu/deployment.git"

# 需要部署到服務器的位置
set :deploy_to, "/var/www/deployment"

# 去掉註釋,並加上 "config/master.key"
append :linked_files, "config/database.yml", "config/master.key"

# 去掉註釋
append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system'

4. 編輯config/deploy/production.rb,見示例圖3
# 改成你自己的ip
server "114.67.72.94", user: "deploy", roles: %w{app db web}, my_property: :my_value

set :ssh_options, {
  keys: %w(~/.ssh/id_rsa),
  forward_agent: true,
  auth_methods: %w(publickey)
}

5. 分配發布文件夾權限
$ sudo mkdir /var/www
$ sudo chown -R deploy:root /var/www

6. 嘗試報錯
# 本地執行
$ cap production deploy:check
出現錯誤

ERROR linked file /home/deploy/deployment/shared/config/database.yml does not exist on 114.67.72.94
解決方法,是需要手動到服務器上,建config/database.yml,config/master.key這兩個文件,並做好配置。

服務器上,會出現releases和shared兩個目錄。releases是每次部署的文件,shared目錄則是一些公用的配置文件。那麼我們現在就去shared目錄中,添加這兩個公用的配置文件。

7. 新建database.yml文件
# 服務器上
$ cd /var/www/deployment/shared/config
$ vim database.yml
# 裏面填寫以下內容
  default: &default
    adapter: postgresql
    encoding: unicode
  	pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  
  production:
    <<: *default
    database: witcan_production
    username: deploy
    password: <%= ENV['WITCAN_DATABASE_PASSWORD'] %>

8. 新建master.key文件
$ vim master.key
# 然後將自己本地項目config/master.key中的內容,複製進去。

9. 再試一次
# Mac 本地執行
cap production deploy:check
這次就沒有報錯了。

示例圖1
示例圖2
示例圖3

十、正式部署

# 本地執行
$ cap production deploy

十一、配置nginx

1. 新增一個項目配置
$ sudo vim /etc/nginx/conf.d/deployment.conf

# 填寫以下內容
upstream server_url {
  server localhost:3000;
}

server {
  listen 80;
  server_name 114.67.72.94;
  server_tokens off;     # don't show the version number, a security best practice
  root /var/www/deployment/current/public;

  # individual nginx logs for this gitlab vhost
  access_log  /var/log/nginx/nginx_access.log;
  error_log   /var/log/nginx/nginx_error.log;

  client_max_body_size 3M;

  location / {
    # serve static files from defined root folder;.
    # @www_tangxiaoyao_com is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html @server_url;
  }
  # if a file, which is not found in the root folder is requested,
  # then the proxy pass the request to the upsteam (putiankj unicorn)
  location @server_url {
    proxy_read_timeout 300;
    proxy_connect_timeout 300;
    proxy_redirect     off;

    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;

    proxy_pass http://server_url;
  }
}

# 重啓nginx
$ sudo service nginx restart

# 然後修改puma文件
$ vim /var/www/deployment/shared/puma.rb

# 注掉原來的,然後新增一個監聽 3000端口
# bind 'unix:///var/www/witcan/shared/tmp/sockets/puma.sock'
bind 'tcp://0.0.0.0:3000'

# 本地再次執行部署命令
$ cap production deploy

十二、成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章