2024 Deploy Ubuntu ROR Rvm+Puma+Capistrano+Nginx
版本: linux ubuntu 20.04, rvm for ruby 3.3.0, Rails 7~
ubuntu可更新到 22.04以上
前情提要:
以前都用passenger加nginx,後來發現passenge後面有版本..等需要升級付費問題,決定改用puma+nginx,就寫了一個完整ROR rvm+puma + nginx部署版本,從底層邏輯建構。
以下是步驟是我習慣的部署步驟:
# step0 初始設定使用者
adduser {username}
vim authorized_keys
# user name: deploy => /etc/sudoers
deploy ALL=(ALL:ALL) ALL
#讓deploy使用者不需要密碼 sudo su
deploy ALL=(ALL) NOPASSWD:ALL
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
service sshd restartm
Step2 Method: Using Ruby Version Manager (這裡使用RVM)
sudo apt upgrade
sudo apt update
# node & npm & yarn
sudo apt install -y nodejs
sudo apt install npm
sudo apt install curl
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn
# install ruby on rails 相關工具
sudo apt install curl g++ gcc autoconf automake bison libaio1 libncurses5 libc6-dev libffi-dev libgdbm-dev libncurses5-dev libsqlite3-dev libtool libyaml-dev make pkg-config sqlite3 zlib1g-dev libgmp-dev libreadline-dev libssl-dev
Step3 Ruby for RVM install
# for ubuntu rvm
sudo apt update
gpg --keyserver hkp://pgp.mit.edu --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable or curl -sSL https://get.rvm.io | bash
curl -sSL https://get.rvm.io | bash
source ~/.rvm/scripts/rvm
rvm list known
rvm install ruby-x.x.x
# 預設ruby版本
rvm use x.x.x --default
# rails -v 7.0.5
gem install rails
Step4 Install Nginx
sudo apt update
sudo apt install nginx
Step5 New Rails Projec 設定Capistrano:
# ex. ruby 3.3.0, rails 7.0.3", ">= 7.0.3.1
gem 'capistrano', '~> 3.1'
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano-rvm'
gem 'capistrano-yarn'
#bundle install
Step6 設定puma (production env)
/etc/systemd/system/
# 把 puma.service 使用"軟連結指令"建立 路徑連到 /project/../config/puma.service
# 使用這兩個指令啟動 puma
systemctl daemon-reload
systemctl restart puma.service
# 也可以放在 cap裡面,部署時自動restart(權限要處理一下)
# 可用這個查看啟動狀態
systemctl status puma.service
寫一支puma.rb (正式環境)
# 允許在後台運行
daemonize true
# 設定 Puma 監聽的 Unix socket 路徑
#bind "unix:///tmp/puma.sock"
# 設定工作的路徑
directory "/home/deploy/project_name/current/"
# 設定工作的執行緒數量
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count
# 設定環境為 production
environment "production"
# 啟動時是否單一進程運行
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
# 設定進程啟動方式
preload_app!
# 在每個進程啟動之後執行的代碼
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
# 設定 pid 文件的路徑
pidfile "/home/deploy/project_name/shared/tmp/pids/puma.pid"
# 設定 stdout 和 stderr 的路徑
stdout_redirect "/home/deploy/project_name/shared/log/puma_access.log", "/home/deploy/project_name/shared/log/puma_error.log", true
# 定義關閉工作的超時時間
worker_shutdown_timeout 60
bind "unix:/home/deploy/project_name/shared/tmp/sockets/puma.sock"
正式環境puma.service (執行rvm ruby)
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Environment="WORK_DIR=/home/deploy/prohect_name"
Type=simple
User=deploy
Group=deploy
WorkingDirectory=/home/deploy/prohect_name/current
ExecStart=/home/deploy/.rvm/bin/rvm all do bundle exec puma -b unix://${WORK_DIR}/shared/tmp/sockets/puma.sock -e production
Restart=always
[Install]
WantedBy=multi-user.target
設定Nginx.conf
upstream puma {
server unix:///home/deploy/project_name/shared/tmp/sockets/puma.sock;
}
server {
listen 80;
server_name {dns};
root /home/deploy/project_name/current/public;
try_files $uri/index.html $uri @puma_renew_code;
location @puma {
proxy_pass http://puma;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
# 這個資料夾可自定義放哪裡都可
error_log /home/deploy/logs/puma_error.log;
}
以上就是整個從無到有,
用rvm+puma+nginx部署流程,
順利的話就可以看到你的網站。