新人さんがRuby on Railsの開発環境構築に苦戦していたので、Vagrantでその手助けをしてあげました。
僕はその新人さん以上にRubyが全く分からない初心者ですが、先人の知恵をお借りすることで環境構築に成功しました。
ベースとなるCentOSのBOXを追加
Vagrant Cloudよりbentoを利用させてもらいます。
$ vagrant box add bento/centos-7.3 https://app.vagrantup.com/bento/boxes/centos-7.3
==> box: Loading metadata for box 'https://app.vagrantup.com/bento/boxes/centos-7.3'
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) parallels
2) virtualbox
3) vmware_desktop
Enter your choice: 2
# 2) virtualboxを選択しますVagrantの起動
適当にディレクトリを作成して移動します。
$ cd /Users/<NAME>/Vagrant/centos7-railsVagrantfileの作成と編集をします。
$ vagrant init bento/centos-7.3生成されたVagrantfileをエディタ等で編集しておきます。
# config.vm.network "forwarded_port", guest: 80, host: 8080
↓
config.vm.network "forwarded_port", guest: 3000, host: 3000
# config.vm.network "private_network", ip: "192.168.33.10"
↓
config.vm.network "private_network", ip: "192.168.33.10"コマンドを入力してVagrantを起動します。
$ vagrant up起動したらSSHで接続してみます。
$ vagrant ssh
[vagrant@localhost ~]$Vagrantにrbenvを導入してRubyをインストール
まずはCentOSの諸々をアップデートします。
[vagrant@localhost ~]$ sudo yum update続いてGitをインストールします。
[vagrant@localhost ~]$ sudo yum install gitrbenvに必要なパッケージをインストールします。
[vagrant@localhost ~]$ sudo yum install gcc gcc-c++ openssl-devel readline-develrbenvをcloneします。
[vagrant@localhost ~]$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenvrbenvのPATHを通します。
[vagrant@localhost ~]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
[vagrant@localhost ~]$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
[vagrant@localhost ~]$ source ~/.bash_profilerbenvがインストールされて使用できるか、バージョンを確認します。
[vagrant@localhost ~]$ rbenv --versionrbenvでRubyをインストールするため、ruby-buildプラグインをcloneします。
[vagrant@localhost ~]$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-buildインストール可能なRubyのバージョンを確認します。
[vagrant@localhost ~]$ rbenv install -l
Available versions:
1.8.5-p52
1.8.5-p113
1.8.5-p114
.
.
.
2.5.0-rc1
2.5.0
2.6.0-dev
.
.
.2018年2月時点での最新である2.5.0をインストールします。
少し時間がかかります。
[vagrant@localhost ~]$ rbenv install 2.5.0Rubyをインストールしたバージョンに切り替えます。
[vagrant@localhost ~]$ rbenv global 2.5.0
[vagrant@localhost ~]$ rbenv rehashRubyがインストールされて使用できるか、バージョンを確認します。
[vagrant@localhost ~]$ ruby -vRails+bundlerのインストール
Railsとbundlerをインストールします。
[vagrant@localhost ~]$ gem install --no-ri --no-rdoc rails
[vagrant@localhost ~]$ gem install bundlerRailsがインストールされて使用できるか、バージョンを確認します。
[vagrant@localhost ~]$ rails -vMySQLのインストール
CentOS7のmariaDBを削除します。
[vagrant@localhost ~]$ sudo yum remove mariadb-libsMySQL :: Download MySQL Yum Repository
上記より新しいバージョンのリンクを取得してMySQLをインストールします。
2018年2月時点では以下のコマンドです。
[vagrant@localhost ~]$ sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpmMySQLに関する諸々をインストールします。
[vagrant@localhost ~]$ sudo yum install mysql-community-serverMySQLがインストールされて使用できるか、バージョンを確認します。
[vagrant@localhost ~]$ mysqld --versionMySQLの起動、自動起動設定
MySQLの起動と自動起動の設定をします。
[vagrant@localhost ~]$ sudo systemctl start mysqld.service
[vagrant@localhost ~]$ sudo systemctl enable mysqld.serviceMySQLのパスワード変更
MySQLの起動をすると初期パスワードが生成されます。
[vagrant@localhost ~]$ cat /var/log/mysqld.log | grep password
2018-02-19T15:20:23.947088Z 1 [Note] A temporary password is generated for root@localhost: ibgw6k!m+VDiこの場合では「ibgw6k!m+VDi」がパスワードです。
このパスワードを利用して、rootパスワードを変更します。
[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:
# 以降基本的に y をタイプして進めます
All done!MySQLにログイン
設定したパスワードでMySQLにログインできるか確認します。
[vagrant@localhost ~]$ mysql -u root -p
Enter password:
.
.
.
mysql> quit
Bye
[vagrant@localhost ~]$MySQLの文字コードを変更
viで下の2行をファイルの最後に追記して、MySQLの文字コードを「utf-8」に変更します。
[vagrant@localhost ~]$ sudo vi /etc/my.cnf
character_set_server=utf8
skip-character-set-client-handshake設定を適用するため、MySQLを再起動します。
[vagrant@localhost ~]$ sudo systemctl restart mysqld.serviceRails with MySQLに必要なパッケージのインストール
RailsでMySQLを使うために必要なgemを使うためのパッケージのインストールをします。
[vagrant@localhost ~]$ sudo yum install mysql-develVagrantにnodebrewを導入してNode.jsをインストール
nodebrewを導入してPATHを通します。
[vagrant@localhost ~]$ curl -L git.io/nodebrew | perl - setup
[vagrant@localhost ~]$ echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> ~/.bash_profile
[vagrant@localhost ~]$ source ~/.bash_profileNode.jsの最新安定版をインストールします。
[vagrant@localhost ~]$ nodebrew install-binary stable
[vagrant@localhost ~]$ nodebrew use stableNode.jsがインストールされて使用できるか、バージョンを確認します。
[vagrant@localhost ~]$ node -v
[vagrant@localhost ~]$ npm -vRailsサーバー起動
ここまでで開発環境構築は完了です。
Railsサーバー起動コマンドは以下になるかと思います。
[vagrant@localhost ~]$ bundle exec rails serverhttp://192.168.33.10:3000
上記URLで接続できたら成功です。




コメント