1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

初めてRailsアプリをproductionモードで起動する(ローカル)

Last updated at Posted at 2021-01-11

##環境
Ruby : 2.6.6
Rails : 6.0.3.4

##SECRET_KEY_BASE を設定する
まずターミナルで以下コマンドを打ち、keyをコピーします。

ターミナル
% bundle exec rails secret
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

エディタを開いて、SECRET_KEY_BASE: xxxxxxxxxxxxxxxxxxxxxxxxxxxと言う様な感じで、先ほどのkeyをペーストします。

ターミナル
% EDITOR="vi" bundle exec rails credentials:edit
vi
SECRET_KEY_BASE: xxxxxxxxxxxxxxxxxxxxxxxxxxx

##アセットプリコンパイル

ターミナル
% RAILS_ENV=production bundle exec rails assets:precompile

##DBのセットアップ

production用のユーザーとデータベースを作成します。

ターミナル
% bundle exec rails db

mysql> create user 'your_user'@'localhost' IDENTIFIED BY 'your_password';
mysql> select User,Host from mysql.user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| your_user        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.01 sec)


mysql> create database `your_database`;
mysql> use your_database;
mysql> grant all on * to 'your_database'@'localhost';
mysql> grant all privileges on your_database.* to 'your_user'@'localhost';
mysql> show grants for 'your_user'@'localhost';

+----------------------------------------------------------------------+
| Grants for your_user@localhost                                       |
+----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `your_user`@`localhost`                        |
| GRANT ALL PRIVILEGES ON `your_database`.* TO `your_user`@`localhost` |
+----------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> exit
database.yml
production:
  # <<: *default
  adapter: mysql2
  encoding: utf8
  database: your_database
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  username: your_user
  password: your_password
  socket: /tmp/mysql.sock
# abase: db/production.sqlite3

作成したデータベース名とユーザー名・パスワードをセットします。

##DBマイグレーションとRailsの起動

migrateとseedを読み込みます。

ターミナル
% RAILS_ENV=production bundle exec rails db:migrate
% RAILS_ENV=production bundle exec rails db:seed

productionを指定してサーバーを起動します。

ターミナル
bundle exec rails s -e production

##あれ?コンパイルができていない...
config/environments/production.rbを書き変えます。

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

これでちゃんとpuroductionで起動できました!

##参考
参考にさせていただきました。ありがとうございます!
https://qiita.com/mokuo/items/b766ca36521530fd3084
https://qiita.com/at-946/items/b7d467bf25c40fcfca44

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?