LoginSignup
8
10

More than 5 years have passed since last update.

Vagrant + VirtualBox + CentOS7 で rails s するまで

Last updated at Posted at 2017-07-09

http://kawakawa.hatenablog.com/entry/2017/07/07/%E4%BB%AE%E6%83%B3%E7%92%B0%E5%A2%83%E3%82%92%E7%AB%8B%E3%81%A6%E3%81%A6rails_s%E3%81%BE%E3%81%A7%E3%81%AE%E6%B5%81%E3%82%8C
上記のリンクの記事の移植です。

環境

  • MacOSX 10.12.5
  • VirtualBox 5.1.22
  • Vagrant 1.9.6
  • CentOS 7.3
  • Git 1.8.3.1
  • Ruby 2.4.1
  • Rails 5.1.2
  • Mysql 5.7.18

VagrantでVirtualBox+CentOSの環境構築

VirtualBoxインストール

VirtualBoxは以下のリンクからダウンロードできます

ダウンロードしてきたら表示に従ってインストールを完了してください。

Vagrantインストール

Vagrantも以下のリンクからダウンロードできます。

こちらも指示に従ってインストールしてください。

仮想環境にCentOSをインストール

こちらからCentOSを探してください。[https://app.vagrantup.com/boxes/search:embed:cite]

CentOSの記述部分に従って行う。

$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v1706.02) for provider: virtualbox
default: Downloading: https://app.vagrantup.com/centos/boxes/7/versions/1706.02/providers/virtualbox.box
==> default: Successfully added box 'centos/7' (v1706.02) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...

vagrant upは途中までしか載せていませんが
こんな感じでboxのインストールや起動をやってくれます
この処理が終わったら「Vagrantfile」が作成されてます

この作業が終わったら仮想環境が動いてるかどうか確認します

$ vagrant status
default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

動いてそうなので接続してみます

$ vagrant ssh
[vagrant@localhost ~]$ 

接続できました。

仮想環境にrbenvをインストールし、Rubyをインストール

CentOSのシステムのバージョンをあげる

[vagrant@localhost ~]$ sudo yum update

gitをインストールする

[vagrant@localhost ~]$ sudo yum install git

rbenvに必要なパッケージをインストールする

[vagrant@localhost ~]$ sudo yum install gcc gcc-c++ openssl-devel readline-devel

rbenvをcloneする

[vagrant@localhost ~]$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
Cloning into '/home/vagrant/.rbenv'...
remote: Counting objects: 2620, done.
remote: Total 2620 (delta 0), reused 0 (delta 0), pack-reused 2620
Receiving objects: 100% (2620/2620), 483.39 KiB | 334.00 KiB/s, done.
Resolving deltas: 100% (1641/1641), done.

rbenvのPATHを通す

[vagrant@localhost ~]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
[vagrant@localhost ~]$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
[vagrant@localhost ~]$ source ~/.bash_profile

rbenvが使えることを確認するため、バージョンを確認

[vagrant@localhost ~]$ rbenv --version
rbenv 1.1.1-2-g615f844

ruby-buildプラグインをclone

->rbenvを使ってRubyをインストールするため

[vagrant@localhost ~]$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
Cloning into '/home/vagrant/.rbenv/plugins/ruby-build'...
remote: Counting objects: 7851, done.
remote: Total 7851 (delta 0), reused 0 (delta 0), pack-reused 7850
Receiving objects: 100% (7851/7851), 1.62 MiB | 548.00 KiB/s, done.
Resolving deltas: 100% (4830/4830), done.

インストール可能なRubyのバージョンを確認

[vagrant@localhost ~]$ rbenv install -l
Available versions:
  1.8.5-p52
  1.8.5-p113
  1.8.5-p114
  1.8.5-p115
  1.8.5-p231
  1.8.6
  1.8.6-p36
  1.8.6-p110
       ・
       ・
       ・
   2.4.0-dev
   2.4.0-preview1
   2.4.0-preview2
   2.4.0-preview3
   2.4.0-rc1
   2.4.0
   2.4.1
       ・
       ・
       ・

こんな感じでインストールが可能なRubyのバージョンを表示してくれます

今回は2.4.1をインストールします。

[vagrant@localhost ~]$ rbenv install 2.4.1
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
Installing ruby-2.4.1...
       ・
       ・
       ・

こんな感じで進んでいきます

これが終わったら
Rubyをインストールしたバージョンに切り替えます

[vagrant@localhost ~]$ rbenv global 2.4.1
[vagrant@localhost ~]$ rbenv rehash

Rubyがインストールされているか確認します。

[vagrant@localhost ~]$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

大丈夫そうですね

Rails+bundlerのインストール

[vagrant@localhost ~]$ gem install --no-ri --no-rdoc rails
[vagrant@localhost ~]$ gem install bundler

Railsがインストールできたかどうかの確認

[vagrant@localhost ~]$ rails -v
Rails 5.1.2

いい感じですね

mysqlのインストール

centOS7ではmariaDBがデフォルトになっているため、mysqlを入れると競合してエラーが出る

そのため削除します

[vagrant@localhost ~]$ sudo yum remove mariadb-libs

[https://dev.mysql.com/downloads/repo/yum/:title]

上記のリンクのページから新しいバージョンのリンクをクリック

写真下部"No thanks, just start my download." のリンクをコピー
aa.png

コピーしたリンクを使いこんな感じでインストール

[vagrant@localhost ~]$ sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

こうすることで.rpmを直接インストールできる

mysqlの諸々をインストール

[vagrant@localhost ~]$ sudo yum install mysql-community-server

インストールされたかどうかを確認する

[vagrant@localhost ~]$ mysqld --version
mysqld  Ver 5.7.18 for Linux on x86_64 (MySQL Community Server (GPL))

こんな感じ

mysqlの起動、自動起動設定

[vagrant@localhost ~]$ sudo systemctl start mysqld.service
[vagrant@localhost ~]$ sudo systemctl enable mysqld.service

rootじゃないといけない的な感じで怒られたのでsudoつけてます

mysqlパスワードの変更

mysqlの起動をすると初期パスワードが生成される

[vagrant@localhost ~]$ cat /var/log/mysqld.log | grep password
2017-07-07T13:01:51.419332Z 1 [Note] A temporary password is generated for root@localhost: 8lNt_uD+;BL#

ここでは「8lNt_uD+;BL#」がパスワード

これを利用してパスワードを変更する。

[vagrant@localhost ~]$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: #生成されたパスワードを入力

The existing password for the user account root has expired. Please set a new password.

New password: #新しく設定するパスワードを入力(8文字以上かつ英大文字・小文字・数字・記号)

Re-enter new password: #再び入力
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

ここでは何をしているのかというと

  • rootパスワード変更
  • 匿名ユーザーの削除
  • リモートホストからrootログイン禁止
  • テスト用データベース削除
  • ユーザー権限が保存されているテーブルをリロード

しています

mysqlにログイン

先程設定したパスワードで入れるか確認します
[vagrant@localhost ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[vagrant@localhost ~]$

入れましたね

mysqlの文字コードを変更

文字コードを「utf-8」に変更します

[vagrant@localhost ~]$ sudo vi /etc/my.cnf
character_set_server=utf8
skip-character-set-client-handshake

下の2行をファイルの最後の方に追加してください

設定を適用するため再起動

[vagrant@localhost ~]$ sudo systemctl restart mysqld.service

RailsでMySQLを使うために必要なgemを使うためのパッケージのインストール

[vagrant@localhost ~]$ sudo yum install mysql-devel

Railsアプリケーションをcloneしてrails sまで

今回は新しくアプリケーションは作らず適当なアプリケーションをgithubからcloneしたいと思います。
ここからは一気に行きます。

[vagrant@localhost project]$ git clone #各々のプロジェクトのgithubのリンク
[vagrant@localhost project]$ cd #各々のプロジェクトパス
[vagrant@localhost upload_slides]$ bundle install
[vagrant@localhost upload_slides]$ rake db:create
[vagrant@localhost config]$ vi config/database.yml
#mysqlのパスワードを記述する
[vagrant@localhost upload_slides]$ rake db:migrate
[vagrant@localhost upload_slides]$ rails s -b 0.0.0.0

として

に接続できたら終了!!

接続できないときの対処法

このサイトを見れば一発解決です。

自分も参考にさせてもらいました。

参考サイト

いろいろ参考にさせてもらいました。

ありがとうございます!!

8
10
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
8
10