Closed14

EC2-Rails環境にnginxを導入

ito_110ito_110
  • EC2環境にログインしてnginxをインストール
sudo amazon-linux-extras install nginx1
ito_110ito_110
  • nginxのインストールを確認
nginx -v
[ec2-user@ip-10-0-8-225 ~]$ nginx -v
nginx version: nginx/1.20.0

バージョンの表示を確認。インストールOK

ito_110ito_110
  • nginxのステータスを確認
sudo service nginx status
[ec2-user@ip-10-0-8-225 ~]$ sudo service nginx status
Redirecting to /bin/systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

nginxが起動していないことを確認

ito_110ito_110
  • nginxを起動
    nginxを起動させるコマンド
sudo service nginx start

nginxの起動を確認

ps aux | grep nginx
terminal
[ec2-user@ip-10-0-8-225 ~]$ sudo service nginx start
Redirecting to /bin/systemctl start nginx.service
[ec2-user@ip-10-0-8-225 ~]$ ps aux | grep nginx
root     22695  0.0  0.0  39864   972 ?        Ss   12:29   0:00 nginx: master process /usr/sbin/nginx
nginx    22696  0.0  0.2  40332  2952 ?        S    12:29   0:00 nginx: worker process
ec2-user 22698  0.0  0.0 119448   968 pts/0    S+   12:29   0:00 grep --color=auto nginx

masterとworkerの起動を確認

ito_110ito_110
  • ブラウザでnginxを確認
    URLEC2パブリックIPv4アドレス:80を確認
    http://13.230.184.55/
ito_110ito_110
  • Railsサーバーを起動して、ELBを起動させる
    rails s -b 0.0.0.0

    unhealthyなので、しばらく待つ
ito_110ito_110
  • pumaの設定
config/puma.rb
"config/puma.rb" 44L, 1844B                                                                                                                                                               19,49        全て
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count

# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

# 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/puma.sock" # 追記

# Specifies the `environment` that Puma will run in.
#
ito_110ito_110
  • nginxの設定
    /etc/nginx/conf.d下に設定ファイルの作成
    sudo vi {アプリ名}.conf
    ※管理者権限でないとファイルの保存ができない
ito_110ito_110
/etc/nginx/conf.d/{アプリ名}.conf
# log directory
error_log  /var/www/rails/{アプリ名}/log/nginx.error.log; #自分のアプリケーション名に変更
access_log /var/www/rails/{アプリ名}/log/nginx.access.log; #自分のアプリケーション名に変更
# max body size
client_max_body_size 2G;
upstream app_server {
  # for UNIX domain socket setups
  server unix:/var/www/rails/{アプリ名}/tmp/sockets/puma.sock fail_timeout=0; #自分のアプリケーション名に変更
}
server {
  listen 80;
  server_name ~~~.~~~.~~~.~~~; #自分のElasticIP
  # nginx so increasing this is generally safe...
  keepalive_timeout 5;
  # path for static files
  root /var/www/rails/{アプリ名}/public; #自分のアプリケーション名に変更
  # page cache loading
  try_files $uri/index.html $uri.html $uri @app;
  location @app {
    # HTTP headers
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }
  # Rails error pages
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /var/www/rails/{アプリ名}/public; #自分のアプリケーション名に変更
  }
}

Railsアプリの保存場所に注意

ito_110ito_110
  • nginxサーバー・Railsサーバーを再起動して、ページ表示
    表示を確認
このスクラップは2022/07/27にクローズされました