本番用のデータベースを作成済みでマイグレーションは終了しているとする。Nginxは既にSSL対応で稼働済みとする。
app/assets以下のファイル(Javascript、CSS等)をコンパイルする、
RAILS_ENV=production rake assets:precompile assets:clean
動的にコンパイルを設定する。コンパイル済みのファイルが見つからない場合にその場でコンパイルを実行する。
vi Rails.root/config/environments/production.rb
config.assets.compile = true
動作確認を行う。
rails server -e production
=> Booting Puma
=> Rails 5.2.4.2 application starting in production
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.4 (ruby 2.7.1-p83), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: production
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
ソケット経由でのAP動作を設定する。
vi rails-root/config/puma.rb
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
#port ENV.fetch("PORT") { 3000 }
bind "unix://#{Rails.root}/tmp/sockets/foo.sock"
Nginxの設定
vi /etc/nginx/sites-available/railspuma
upstream foo {
# puma.rbと同じものを記述(/は3つ)
server unix://Rails Appへのパス/tmp/sockets/foo.sock fail_timeout=0;
}
server {
# httpをhttpsにリダイレクト
listen 3000;
server_name サーバー名;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name サーバー名;
ssl on;
## Use TLS instead of SSL - Compatibility issues with some Java clients
## and older versions of of IE, however, more secure.
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
## Use more secure and less CPU tasking ciphers compared to nginx defaults
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
keepalive_timeout 10;
root Rails Appへのパス;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
try_files $uri/index.html $uri @foo;
location @foo {
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# upstreamの名前を記述
proxy_pass http://foo;
}
error_page 500 502 503 504 /500.html;
}
ln -s /etc/nginx/sites-available/railspuma /etc/nginx/sites-enabled/
開発環境からRailsアプリを実行サーバーへアップロードする。FTPでもGit経由でも・・・
再度Railsの設定
vi Rails Appへのパス/config/puma.rb
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
#port ENV.fetch("PORT") { 3000 }
bind "unix://#{Rails.root}/tmp/sockets/foo.sock"
daemonize # デーモン化
stdout_redirect "#{Rails.root}/log/stdout", "#{Rails.root}/log/stderr" # 標準主力とエラー出力先を指定
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "production" }
cd Rails Appへのパス
rails server -e production
もしくは
bundle exec rails s
pumaのデーモン化は仕様から外れた様です。
systemdを使用してデーモン化することとなる様です。
pumaのsystemd設定ファイルのサンプルは、ここにあります
コメント