Ubuntu 20 + Apache 2 + asdf + Rails 6 + Passenger + Capistrano

Bootstrap, jQuery

參考 How to install Bootstrap and jQuery on Rails 6, 2020-04-27

mac 上執行

brew install yarn
Yarn add bootstrap jquery popper.js

編輯 config/webpack/environment.js

const { environment } = require('@rails/webpacker')  
const webpack = require('webpack')
environment.plugins.append('Provide',
  new webpack.ProvidePlugin({
    s: 'jquery',
    jQuery: 'jquery',
    Popper: ['popper.js', 'default']
  })
)
module.exports = environment

app/javascript/packs/application.js

import 'bootstrap'

app/assets/stylesheets/application.css 副檔名改為 scss

並加上:

@import 'bootstrap/scss/bootstrap';

Capistrano

編輯 Gemfile

group :development do
  gem "capistrano", "~> 3.10", require: false
  gem "capistrano-rails", "~> 1.6", require: false
  gem 'capistrano-asdf'
  gem 'capistrano-passenger'
end

執行

bundle install
bundle exec cap install

編輯 Capfile

require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/passenger"
require "capistrano/asdf"

編輯 config/deploy.rb, 如果 server 上還沒有 master.key 就上傳。

set :application, "YourApplicationName"
set :repo_url, "URL of Your Git Repository"

append :linked_files, "config/master.key"

set :passenger_restart_with_touch, true

namespace :deploy do
  namespace :check do
    before :linked_files, :set_master_key do
      on roles(:app), in: :sequence, wait: 10 do
        unless test("[ -f #{shared_path}/config/master.key ]")
          upload! 'config/master.key', "#{shared_path}/config/master.key"
        end
      end
    end
  end
end

namespace :deploy do
  task :restart do
    on roles(:web), in: :sequence do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end
end

編輯 config/deploy/production.rb

server 'Your.Server.URL', user: 'UserNameUsedToDeployToServer', roles: %w{app db web}

Source code 上傳 Git.

Server: Yarn

使用 asdf 安裝 yarn

asdf plugin-add yarn
asdf install yarn latest

查看版本

yarn -v

結果

No version set for command yarn
Consider adding one of the following versions in your config file at /var/www/vm_docx/releases/20210128042346/.tool-versions
yarn 1.22.10

設為預設

asdf global yarn 1.22.10

Server: Apache

決定 Passenger 要使用的 Ruby

執行以下命令:

passenger-config about ruby-command

結果

To use in Apache: PassengerRuby /home/ray/.asdf/installs/ruby/3.0.0/bin/ruby

網址使用 subdirectory

例如 http://xxx.dila.edu.tw/ProjectName

假設 rails project root: /var/www/ProjectName/current

編輯 /etc/apache2/sites-available/ProjectName.conf

Alias /ProjectName /var/www/ProjectName/current/public
<Location /ProjectName>
  PassengerBaseURI /ProjectName
  PassengerAppRoot /var/www/ProjectName/current
  PassengerRuby /home/ray/.asdf/installs/ruby/3.0.0/bin/ruby
</Location>
<Directory /var/www/ProjectName/current/public>
  Allow from all
  Options -MultiViews
  Require all granted
</Directory>

編輯 /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
  ...
  Include sites-available/ProjectName.conf
</VirtualHost>

重新啟動 apache2

sudo service apache2 restart


Deploy

cap production deploy

如果出現如下錯誤:

Your bundle only supports platforms ["x86_64-darwin-20"] 
but your local platform is x86_64-linux. 
Add the current platform to the lockfile with 
`bundle lock --add-platform x86_64-linux` and try again.

就依指示執行

bundle lock --add-platform x86_64-linux