LoginSignup
52

More than 5 years have passed since last update.

Rails5をproduction(本番環境)で起動する時に嵌ったこと

Last updated at Posted at 2017-06-13

本番環境でrails serverする

$ rails server --environment production
参考:http://somethingpg.hatenablog.com/entry/2013/12/22/101528

なにもせずにやるとエラーが出ます🤔

An unhandled lowlevel error occurred. The application logs may have details.
とブラウザに出ておこられる。

rails_root/config/secrets.yml
development:
  secret_key_base: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

test:
  secret_key_base: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

これを解決するには以下のターミナルコマンドが必要です。

(A).
$ export SECRET_KEY_BASE=`bundle exec rake secret`

試しに$ bundle exec rake secretを打ってみると、長いキーが帰ってきます。
毎度打ちたくないので、(A)を.hashrc(だっけ、.bash_profileだっけ)に記入しておくといいだろう。
参考:http://blog.naichilab.com/entry/2015/12/05/232651

css (assets全般) が読み込まれない🤔

webページのリソースを見るとエラーが出ていてFailed to load resource: the server responded with a status of 404 (Not Found)とおこられてる。
ターミナルで
$ RAILS_ENV=production bundle exec rake assets:precompile
を実行して、次のファイルの箇所をtrueに直す。

rails_root/config/environments/production.rb
config.assets.compile = true

Rails再起動が多分必要。
参考:http://stackoverflow.com/questions/9888724/failed-to-load-resource-the-server-responded-with-a-status-of-404-not-found

ローカル開発環境からサーバへ移った時にsecret_key_baseが無いとおこられ続ける🤔

SECRET_KEY_BASEの環境変数が正しく収容されているか、確認してみようってことで

$ export -p | grep SECRET_KEY_BASE
declare -x SECRET_KEY_BASE="Could not locate Gemfile or .bundle/ directory"

いろいろいじってrake secretがソラで実行できるようになればよし。主にインストール先の問題だろう。
その後もrails sを実行しようとするとpermission errorで書き込めないファイルがあるとかで、いろいろ調べて自分のローカルの実行できている環境とls -laで照らし合わせていくと、xの権限がtmpとlogフォルダに足りないのが原因だった模様。
$ chmod 755 -R tmpなどとして、ようやく解決。
最後の方はやや荒技っぽくなったけど、ひとまずサーバでローカルに立ち上げることには成功した。

Unicorn + Nginxでまだだめ🤔

sudoでunicornを起動してしまうと、ログファイルがルート権限になってしまい、想定外の不具合につながる。
起動がルート以外でできないのは、ポート80番がルート権限を必要とするため。なので、Nginxの設定ファイルをいじり、listenするポートを8080番など1024以降の番号にし、自宅ルータをWAN80番からLAN8080番に転送するように設定する。
これで、sudoを使わなくともサーバを起動できるようになったはずだ。

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
52