LoginSignup
14
10

More than 3 years have passed since last update.

Ruby on Rails + puma + nginx でアプリを立ち上げる

Last updated at Posted at 2019-04-20

目的

centos + nginx で railsアプリケーションを立ち上げたい
その作業をした際の備忘録

作業内容

前提

ruby on rails はインストール済み

nginx のインストール

[root@localhost ~]# yum install -y n nginx

nginx の設定変更

rails_app の部分は自分の作成しているアプリ名に書き換える

/etc/nginx/nginx.conf
user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    error_log  /var/log/nginx/error.log warn;

    upstream app {
        server unix:/var/work/<rails_app>/tmp/sockets/puma.sock;
    }

    server {
        listen 80;
        server_name localhost;

        location / {
        proxy_pass http://app;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        }

    }
}

nginx 起動

[root@localhost ~]# service nginx start

puma の設定

pumaとは

https://github.com/puma/puma/
webアプリケーションサーバーの一つ、並行処理が得意らしい

rails のpuma用設定ファイルに以下を追記する

config/puma.rb
# 以下を追記
bind "unix://#{Rails.root}/tmp/sockets/puma.sock"

rails 側で本番環境立ち上げ用の設定

本番環境用の設定で起動する場合は、秘密鍵を取得して環境変数に設定する必要がある

[root@localhost ~]# bundle exec rake secret
[root@localhost ~]# export SECRET_KEY_BASE= #上記コマンドで取得したキーを設定

本番用の環境設定ファイルを書き換える
ここ不要かもしれない

config/environments/production.rb
~
  #config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
  config.public_file_server.enabled = true
~

サーバー起動

[root@localhost ~]# rails s -e production
* Version 3.12.1 (ruby 2.6.2-p47), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: production
* Listening on unix:///var/work/<rails_app>/tmp/sockets/puma.sock
Use Ctrl-C to stop

アプリにアクセス

http://localhost:80 にアクセスし、表示ができることを確認する

We're sorry, but something went wrong.エラーが出た

エラーが出てページが表示できなかったので、画面の指示通りにログファイルを確認する

log/production.log
ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.):
      5:     <%= csrf_meta_tags %>
      6:     <%= csp_meta_tag %>
      7: 
      8:     <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
      9:     <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
     10:     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
     11:   </head>
 FATAL -- :  app/views/layouts/application.html.erb:8:in `_app_views_layouts_application_html_erb'

application.html.erb の8行目でエラーと出たが、テストサーバーで起動しても問題なく表示されるし心当たりがない

いろいろ調べて以下のページをもとに解決
https://teratail.com/questions/56995
パーミッションが適切でなかったようなので、キャッシュクリアなどを試す

[root@localhost ~]# bin/rails tmp:cache:clear 
[root@localhost ~]# bin/rails assets:precompile 

→ もう一回本番サーバー起動したら表示できた

14
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
10