35
39

More than 3 years have passed since last update.

ローカルで Rails アプリを production モードで起動する

Last updated at Posted at 2018-03-20

はじめに

Rails アプリケーションをローカル PC で production モードで起動する際の手順を整理しようと思います。

基本的に、サーバーにデプロイする前に、手元で productionモードで動かして、エラーは潰しておくのが良いと思います。

※ API サーバーだけであれば、↓↓ に最新のバージョンで書き直したので、そちらを御覧ください。
ローカルで Rails アプリを production モードで起動する(API サーバー編) - Qiita

バージョン

Ruby : 2.4.2
Rails : 5.1.4
React : 16.2.0
Redux : 3.7.2
Webpacker : 3.3.1
Heroku CLI : heroku-cli/6.15.35-cf39a29 (darwin-x64) node-v9.8.0

手順

SECRET_KEY_BASE を設定する

bin/rails secret で出力された値を、環境変数 SECRET_KEY_BASE に設定する。
私は dotenv-rails gem を使用しているので、 .env に設定する。
dotenv-rails を使用していない場合 export とかで設定すれば良いはず。

.env
SECRET_KEY_BASE= xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

参考

アセットプリコンパイル

$ RAILS_ENV=production bundle exec rails assets:precompile

ここで、実は複数回エラーになりました。詳細は以下をご覧ください。

DBのセットアップ

production 用の DB とユーザーを作成する

$ bin/rails db
mysql> create user 'your_user'@'localhost' IDENTIFIED BY 'your_password';
mysql> select User,Host from mysql.user;
+----------------+-----------+
| User           | Host      |
+----------------+-----------+
| mysql.sys      | localhost |
| your_user      | localhost |
| root           | localhost |
+----------------+-----------+
3 rows in set (0.00 sec)

### 権限設定
mysql> create database `your_database`;
mysql> use your_database;
mysql> grant all on * to 'your_database'@'localhost';
mysql> show grants for 'your_database'@'localhost';
+----------------------------------------------------------------------------------------------------------------+
| Grants for elm-asp-manager@localhost                                                                           |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'pomodoro-rails'@'localhost'                                                            |
| GRANT ALL PRIVILEGES ON `pomodoro-rails_production`.* TO 'pomodoro-rails'@'localhost'                        |
+----------------------------------------------------------------------------------------------------------------+
mysql> exit

database.yml の設定

先ほど作成したproduction用のDBユーザー・パスワードを database.yml に設定する。
database.yml.sample などを作成しておき、 database.yml.gitignore しておくと良い。

DBマイグレーション

$ RAILS_ENV=production bundle exec rails db:migrate
$ RAILS_ENV=production bundle exec rails db:seed # seedデータがない場合は不要

サーバーを production モードで起動する

$ bundle exec rails s -e production

エラーなくアクセスできればOK!!

35
39
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
35
39