LoginSignup
1
0

More than 1 year has passed since last update.

Railsの環境構築

Posted at

はじめに

Railsのインストール方法を下記のぺージをお手本にして行いました。
わからないことばかりなので、間違っていた際や改善点がありましたらアドバイスをいただけると幸いです。

要件定義

・EC2 (t2.micro) ←無料で使用できます。
・Amazon Linux2
・Ruby Ver 2.6.3
・MySQL Ver 5.7.32
・Node.js Ver 14.15.2
・Nginx Ver 1.18.0
・Rails Ver 5.2.3 5.2.8.1

更新処理を実行

$ sudo yum update

gitのインストール

$ sudo yum install git
$ git --version
git version 2.37.1

rbenv インストール

ディレクトリを作成して、そのパスを環境変数 RBENV_ROOT に設定します。

$ sudo mkdir /opt/rbenv
$ export RBENV_ROOT=/opt/rbenv

gitを用いて、rbenvを/opt/rbenvにcloneします。

$ sudo git clone https://github.com/sstephenson/rbenv.git /opt/rbenv
$ sudo mkdir /opt/rbenv/versions
$ sudo mkdir /opt/rbenv/shims

すべてのユーザが rbenv を利用できるように、PATHを追加します。

$ sudo vi /etc/profile.d/rbenv.sh
rbenv.sh
export RBENV_ROOT="/opt/rbenv"
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"

バージョンの確認

$ source /etc/profile
$ rbenv -v
rbenv 1.2.0

Rubyをrbenv経由でインストールする時に必要なプラグイン ruby-buildもcloneします。

$ sudo git clone https://github.com/sstephenson/ruby-build.git /opt/rbenv/plugins/ruby-build

インストールを実行します。

$ PREFIX=/opt/rbenv sudo /opt/rbenv/plugins/ruby-build/install.sh

Rubyのバージョン一覧が表示されれば、ruby-buildのインストールは完了です。

$ rbenv install -l

2.6.10
2.7.6
3.0.4
3.1.2
jruby-9.3.6.0
mruby-3.1.0
picoruby-3.0.0
rbx-5.0
truffleruby-22.2.0
truffleruby+graalvm-22.2.0

Rubyインストール

必要なパッケージを事前にインストールしていきます。

$ sudo yum install -y gcc-c++ bzip2 openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel

root ユーザーに変更

$ sudo su -

rbenvで使用するRubyのバージョンを指定し、バージョン確認します。

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

bundlerをインストール

$ gem install bundler
Fetching bundler-2.3.20.gem
Successfully installed bundler-2.3.20
Parsing documentation for bundler-2.3.20
Installing ri documentation for bundler-2.3.20
Done installing documentation for bundler after 0 seconds
1 gem installed

rootユーザーから、一般ユーザに戻ります。

$ exit

MySQL5.7 インストール

パッケージがインストールされているか確認

$ yum list installed | grep mariadb
$ yum list installed | grep mysql

初期からインストールされているMariaDB用パッケージを削除します。

$ sudo yum remove mariadb-libs

MySQLのリポジトリをyumに追加します。

$ sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

MySQL8.0の無効化をして、MySQL5.7を有効化します。

$ sudo yum-config-manager --disable mysql80-community
$ sudo yum-config-manager --enable mysql57-community
$ yum info mysql-community-server mysql-community-devel

エラーが出てきました↓

$ sudo yum install mysql-community-server mysql-community-devel

読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
mysql-connectors-community                               | 2.6 kB     00:00     
mysql-tools-community                                    | 2.6 kB     00:00     
mysql57-community                                        | 2.6 kB     00:00     
〜省略〜
mysql-community-client-5.7.39-1.el7.x86_64.rpm の公開鍵がインストールされていません

Failing package is: mysql-community-client-5.7.39-1.el7.x86_64
 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

上記のエラーが出た際に下の方法で解決できました。

$ sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

同じコマンドを再度入力してみました。

sudo yum install mysql-community-server mysql-community-devel

読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
53 packages excluded due to repository priority protections
依存性の解決をしています
〜省略〜
依存性関連をインストールしました:
  mysql-community-client.x86_64 0:5.7.39-1.el7                                  
  mysql-community-common.x86_64 0:5.7.39-1.el7                                  
  mysql-community-libs.x86_64 0:5.7.39-1.el7                                    
  ncurses-compat-libs.x86_64 0:6.0-8.20170212.amzn2.1.3                         

完了しました!

バージョン確認

$ mysqld --version
mysqld  Ver 5.7.39 for Linux on x86_64 (MySQL Community Server (GPL))
$ sudo touch /var/log/mysqld.log

起動して、statusがactiveになっているか確認します。

$ sudo systemctl start mysqld
$ sudo systemctl status mysqld.service
 mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since  2022-08-12 02:34:54 UTC; 43s ago
     Docs: man:mysqld(8)
〜省略〜
$ sudo systemctl enable mysqld

初期パスワードを確認します。

sudo less /var/log/mysqld.log | grep root@localhost
2022-08-12T02:34:51.840292Z 1 [Note] A temporary password is generated for root@localhost: g:rdTc.d-9Yk

password入力する際に今回は g:rdTc.d-9Yk を入力し、ログインします。

mysql -u root -p
Enter password: 

パスワードの再設定します。

mysql> ALTER USER 'root'@'localhost' identified BY '<-新パスワード->';
パスワード設定の例
mysql> ALTER USER 'root'@'localhost' identified BY 'rails';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
パスワードが簡単すぎると設定できないんですね。。。

mysql> ALTER USER 'root'@'localhost' identified BY 'Nekosuki22.';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

Node.js(nvm) インストール

ディレクトリを作成します。

$ sudo mkdir /opt/nvm
$ export NVM_ROOT=/opt/nvm

gitを用いて、nvmを/opt/nvmにcloneします。

$ sudo git clone https://github.com/nvm-sh/nvm.git /opt/nvm
$ sudo mkdir /opt/nvm/versions

PATHを追加します。

$ sudo vi /etc/profile.d/nvm.sh
nvm.sh
export NVM_ROOT="/opt/nvm"
export PATH="$NVM_ROOT/bin:$PATH"
[ -s "$NVM_ROOT/nvm.sh" ] && \. "$NVM_ROOT/nvm.sh"  # This loads nvm
[ -s "$NVM_ROOT/bash_completion" ] && \. "$NVM_ROOT/bash_completion"  # This loads nvm bash_completion

リロードして、バージョンが確認できればインストール完了です。

$ source /etc/profile
$ nvm -v
0.39.1

rootユーザーに変更します。

$ sudo su -

Node.jsのバージョン一覧を確認し、インストールします。

$ nvm ls-remote
v0.1.14
v0.1.15
v0.1.16
.
.
.
v18.5.0
v18.6.0
v18.7.0

$ nvm install 14.15.2
Downloading and installing node v14.15.2...
Downloading https://nodejs.org/dist/v14.15.2/node-v14.15.2-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.15.2 (npm v6.14.9)
Creating default alias: default -> 14.15.2 (-> v14.15.2)

使用するバージョンを指定します。バージョン確認ができたら完了です。

$ nvm use v14.15.2
Now using node v14.15.2 (npm v6.14.9)

$ node -v
v14.15.2

$ npm -v
6.14.9

yarn をインストール

$ npm install -g yarn

> yarn@1.22.19 preinstall /opt/nvm/versions/node/v14.15.2/lib/node_modules/yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)

/opt/nvm/versions/node/v14.15.2/bin/yarn -> /opt/nvm/versions/node/v14.15.2/lib/node_modules/yarn/bin/yarn.js
/opt/nvm/versions/node/v14.15.2/bin/yarnpkg -> /opt/nvm/versions/node/v14.15.2/lib/node_modules/yarn/bin/yarn.js
+ yarn@1.22.19
added 1 package in 0.759s

バージョンを確認

$ which yarn
/opt/nvm/versions/node/v14.15.2/bin/yarn
$ yarn -v
1.22.19

一般ユーザに戻ります。

$ exit

Nginx インストール

インストール可能なパッケージ一覧を確認します。

$ amazon-linux-extras
0  ansible2                 available    \
        [ =2.4.2  =2.4.6  =2.8  =stable ]
2  httpd_modules            available    [ =1.0  =stable ]
3  memcached1.5             available    \
        [ =1.5.1  =1.5.16  =1.5.17 ]
5  postgresql9.6            available    \
        [ =9.6.6  =9.6.8  =stable ]
6  postgresql10             available    [ =10  =stable ]
.
.
.

Nginxをインストールします。

$ sudo amazon-linux-extras install nginx1

バックアップ

$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back

Nginxを起動します。

$ sudo systemctl start nginx
$ sudo systemctl enable nginx
$ systemctl status nginx
$ sudo systemctl reload nginx
$ sudo systemctl stop nginx

Railsのサンプルアプリ作成

ディレクトリ作成します。

$ mkdir sample
$ cd sample
$ bundle init
$ vim Gemfile

Rails v5.2.3 をインストールするように Gemfile を変更します。

# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "rails", "~> 5.2.3"

Gemをインストールします。

$ bundle install --path vendor/bundle --jobs=4
[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set --local path 'vendor/bundle'`, and stop using this flag
Fetching gem metadata from https://rubygems.org/...........
.
.
.
Fetching rails 5.2.8.1
Installing rails 5.2.8.1
Bundle complete! 1 Gemfile dependency, 40 gems now installed.
Bundled gems are installed into `./vendor/bundle`

現在のディレクトリに、Railsファイルを生成します。

$ bundle exec rails new . -d mysql -BJT
exist  
create  README.md
create  Rakefile
create  .ruby-version
.
.
.
create  tmp/storage/.keep
remove  app/assets/javascripts
remove  config/initializers/cors.rb
remove  config/initializers/new_framework_defaults_5_2.rb

updateします。

$ bundle update
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Bundler could not find compatible versions for gem "actionpack":
  In snapshot (Gemfile.lock):
    actionpack (>= 5.2.8.1)

  In Gemfile:
    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      actionpack (= 5.2.8.1)

    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      sprockets-rails (>= 2.0.0) was resolved to 3.4.2, which depends on
        actionpack (>= 5.2)

Deleting your Gemfile.lock file and running `bundle install` will rebuild your
snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
Bundler could not find compatible versions for gem "actionview":
  In snapshot (Gemfile.lock):
    actionview (>= 5.2.8.1)

  In Gemfile:
    jbuilder (~> 2.5) was resolved to 2.11.5, which depends on
      actionview (>= 5.0.0)

    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      actionview (= 5.2.8.1)

Deleting your Gemfile.lock file and running `bundle install` will rebuild your
snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
Bundler could not find compatible versions for gem "activesupport":
  In snapshot (Gemfile.lock):
    activesupport (>= 5.2.8.1)

  In Gemfile:
    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      activejob (= 5.2.8.1) was resolved to 5.2.8.1, which depends on
        globalid (>= 0.3.6) was resolved to 1.0.0, which depends on
          activesupport (>= 5.0)

    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      activesupport (= 5.2.8.1)

    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      actionpack (= 5.2.8.1) was resolved to 5.2.8.1, which depends on
        rails-dom-testing (~> 2.0) was resolved to 2.0.3, which depends on
          activesupport (>= 4.2.0)

    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      sprockets-rails (>= 2.0.0) was resolved to 3.4.2, which depends on
        activesupport (>= 5.2)

Deleting your Gemfile.lock file and running `bundle install` will rebuild your
snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
Bundler could not find compatible versions for gem "railties":
  In snapshot (Gemfile.lock):
    railties (>= 5.2.8.1)

  In Gemfile:
    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      railties (= 5.2.8.1)

    sass-rails (~> 5.0) was resolved to 5.1.0, which depends on
      railties (>= 5.2.0)

Deleting your Gemfile.lock file and running `bundle install` will rebuild your
snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
Bundler could not find compatible versions for gem "sprockets":
  In snapshot (Gemfile.lock):
    sprockets (>= 4.1.1)

  In Gemfile:
    sass-rails (~> 5.0) was resolved to 5.1.0, which depends on
      sprockets (>= 2.8, < 4.0)

    rails (~> 5.2.8, >= 5.2.8.1) was resolved to 5.2.8.1, which depends on
      sprockets-rails (>= 2.0.0) was resolved to 3.4.2, which depends on
        sprockets (>= 3.0.0)

Deleting your Gemfile.lock file and running `bundle install` will rebuild your
snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

エラーが出てしましました↑

Gemfileの中を書き換えてみました。

Gemfile書き換えてみました
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.8.1' ←書き換えました(元々はgem "rails", "~> 5.2.3"でした。) 
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 6' ←書き換えました
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'

再度updateをしてみました。

$ bundle update
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies....
Using rake 13.0.6
Using concurrent-ruby 1.1.10
Using minitest 5.16.2
.
.
.
Using sassc-rails 2.1.2
Using web-console 3.7.0
Using sass-rails 6.0.0
Bundle updated!

パスワードを設定します。

vi config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: Nekosuki22. ←設定したものを入力
  host: localhost

Rails用のデータベースを作成します。

$ bundle exec rake db:create

Nginxで表示できるように、今回のsampleアプリ用の Nginxの設定ファイルを追加します。

$ sudo vim /etc/nginx/conf.d/sample.conf
sample.conf
server {
  # リクエストを受けるポートを設定する
  listen   80;
  server_name localhost;
  location / {
    # 80 ポートで受けたリクエストを 3000 ポートに中継する
    proxy_pass http://127.0.0.1:3000;
  }
}

Nginxを再起動

$ sudo systemctl reload nginx

Railsサーバーを起動します。

$ bundle exec rails s

http://IPアドレス にアクセスしてRailsの画面が表示されたら完了になります。

スクリーンショット 2022-08-12 14.06.16.png

まとめ

せりぴーさんの環境構築の説明がとてもわかりやすく自分一人で進めることができました。今後も色々なものに挑戦していきたいと思います。

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