3
5

More than 3 years have passed since last update.

Rails6 on Heroku

Last updated at Posted at 2019-11-03

目的

業務でWeb系言語に触れることが少なく、危機感を感じる今日この頃なので、Web系言語の環境構築〜公開までやってみました。Salesforce案件で使用頻度の高いHerokuを公開先とし、多少かじったことのあるRubyをデプロイします。

前提

  • ホストOS
    • macOS Mojave 10.14.6
  • ゲストOS
    • CentOS Linux release 7.6.1810 (Core)
  • VirtualBox 6.0.10
  • Vagrant 2.2.5
  • PostgreSQL10 (ローカル)
  • Ruby 2.6.3
  • Rails 6.0.0

手順

1. vagrant up

$ mkdir centos7
$ cd centos7
$ vagrant init centos/7

Vagrantfileを以下のように編集します。

Vagrantfile
# ... 略

config.vm.network "private_network", ip: "192.168.33.14"

# ... 略
$ vagrant up
$ vagrant ssh
[vagrant@localhost ~]$

2. Ruby on Rails 環境構築

1. gitインストール

$ sudo yum -y install git
$ git --version
git version 1.8.3.1

2. nodejsとyarnインストール

webpackerのインストール時に使用します。

$ curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
$ sudo yum -y install nodejs
$ node -v
v8.16.2

$ sudo yum -y install wget
$ sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
$ sudo yum -y install yarn
$ yarn -v
1.19.1

3. その他必要なパッケージもろもろインストール

$ sudo yum -y install gcc make openssl-devel readline-devel

4. rbenvインストール

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

5. ruby-buildインストール

$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-$build

6. rubyインストール

$ rbenv install 2.6.3
$ rbenv global 2.6.3
$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

7. postgresql設定

1. postgresql10をインストール
$ sudo yum -y localinstall https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
$ sudo yum -y install postgresql10-server
$ /usr/pgsql-10/bin/postgres --version
postgres (PostgreSQL) 10.10
2. 初期化/自動起動設定
$ sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
$ sudo systemctl enable postgresql-10
$ sudo systemctl start postgresql-10
$ systemctl status postgresql-10
3. 接続ユーザ作成

postgresqlインストール時に作成されるOSのユーザ(postgres)で、postgresqlにログインします。

$ sudo -u postgres psql -U postgres

ユーザ名:mymemo、パスワード:mymemopwdの、superuser権限を持つユーザを作成します。

-- 接続ユーザ作成
postgres=# create role mymemo with superuser login password 'mymemopwd';
CREATE ROLE

-- 接続ユーザ確認
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member
 of 
-----------+------------------------------------------------------------+-------
----
 mymemo    | Superuser                                                  | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

-- postgresからログアウト
postgres=# \q
4. ユーザ認証変更
$ sudo vi /var/lib/pgsql/10/data/pg_hba.conf

以下のように、pg_hba.confのMETHODmd5に変更します。

pg_hba.conf
# ... 略

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     md5
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

変更後、再起動します。

$ sudo systemctl restart postgresql-10

8. Railsのインストールと疎通確認

1. Railsのインストール
$ gem install rails
$ rails --version
Rails 6.0.0
2. アプリ作成
$ sudo yum -y install postgresql-devel
$ gem install pg -v '1.1.4'
$ rails new mymemo -d postgresql
$ cd mymemo/
3. データベース設定
$ sudo vi config/database.yml

database.ymlに手順7-3で作成した接続ユーザのユーザ名とパスワードを設定します。

database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
# 手順7-3で作成した接続ユーザのユーザ名とパスワードを設定する
  username: mymemo
  password: mymemopwd

development:
  <<: *default
  database: mymemo_development

test:
  <<: *default
  database: mymemo_test

production:
  <<: *default
  database: mymemo_production
4. データベース作成
$ rails db:create
Created database 'mymemo_development'
Created database 'mymemo_test'
5. 疎通確認
$ bundle exec rails webpacker:install
$ bundle exec rails s -b 192.168.33.14

アドレスバーに192.168.33.14:3000を入力します。

スクリーンショット 2019-11-03 22.55.34.png

6. scaffold

Yay!You're on Rails! だとアレので、scaffoldします。。

$ bundle exec rails g scaffold Memo title:string body:text
$ rake db:migrate 
$ bundle exec rails s -b 192.168.33.14

アドレスバーに192.168.33.14:3000/memosを入力します。

スクリーンショット 2019-11-04 1.09.52.png

3. Heroku Push

1. Heroku CLIインストール

$ su -
$ curl https://cli-assets.heroku.com/install.sh | sh
$ exit
$ heroku -v
heroku/7.33.3 linux-x64 node-v11.14.0

2. データベース設定変更

$ sudo vi config/database.yml

Herokuのpostgresを参照するように編集します。

database.yml

# ... 省略

production:
  <<: *default
  # database: mymemo_production
  url: <%= ENV['DATABASE_URL'] %>

3. Gemfile編集とbundle install

$ vi Gemfile

rails_12factorを末尾に追記します。

Gemfile

# ... 省略

gem 'rails_12factor', group: :production

Gemfile.lockを更新します。

$ bundle install

4. Procfile作成

$ vi Procfile
Procfile
web: bundle exec rails server -p $PORT

5. Heroku Push

$ heroku login -i
Logged in as xxx@xxx.xxx

$ heroku create
$ git add .
$ git commit -m "initial commit"
$ git push heroku master
$ heroku run rake db:migrate

6. アプリにアクセス

$ heroku apps:info

=== [herokuが生成したアプリ名]
Addons:         heroku-postgresql:hobby-dev
Auto Cert Mgmt: false
Dynos:          web: 1
Git URL:        https://git.heroku.com/[herokuが生成したアプリ名].git
Owner:          xxx@xxx.xxx
Region:         us
Repo Size:      155 KB
Slug Size:      58 MB
Stack:          heroku-18
Web URL:        https://[herokuが生成したアプリ名].herokuapp.com/

https://[herokuが生成したアプリ名].herokuapp.com/memos

7. PG Commanderでデータ登録確認

何件かデータを登録した後、PG Commanderでpostgresに接続し、確認します。

スクリーンショット 2019-11-04 1.16.56.png

まとめ

環境構築が大変でした。おまじない箇所が結構あるので継続して勉強することにします。

参考サイト

https://weblabo.oscasierra.net/postgresql10-centos7-install/
https://qiita.com/isotai/items/67bafc37a16ea5de5e3b

3
5
1

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
3
5