Ruby on Rails学習のため、開発環境をCentOS7(Vagrant)上に構築したことを書きます。
実行したコマンドを貼っていきますが、ターミナルは可読性向上のためカレントディレクトリを常事表示してます。
Ruby on Railsの公式サイトのGuides > Getting Started with Rails に丁寧に書いてますね。基本的にはここに書いてある通りに進めれば良さそう。
【目標】
ローカル開発環境のCentOS7(Vagrant)上でRailsアプリを起動し、ローカルブラウザから参照できるようになるまでをゴールとする。
【構築】
railsアプリを作るには当然Rubyが必要なのでRubyをインストールして、その後Railsをインストールしアプリを生成しアプリサーバを起動してみます。
Ruby
とりあえずRubyの公式サイトを見て、現在の安定板はRuby2.5.1
だそうなので、こちらをインストールする。
Linux/UNIXにインストールする場合はyumやrbenv、RVMを使うことを推奨しているよう。なのでとりあえずyum install ruby
しました。けれど残念なことにRuby2.0.0
が入ってしまったから即削除しました。rbenvなら最新versionのRubyを扱っているようなのでrbenv経由でRubyをインストールします。なので先ずrbenvをインストールします、インストール方法はこちらのREADMEに書いてあるので、説明の通りに進めてみます。
// とりあえずREADMEの通りに実行していく
localhost:[~]
% git clone https://github.com/rbenv/rbenv.git ./.rbenv
Cloning into './.rbenv'...
remote: Enumerating objects: 2744, done.
remote: Total 2744 (delta 0), reused 0 (delta 0), pack-reused 2744
Receiving objects: 100% (2744/2744), 515.63 KiB | 630.00 KiB/s, done.
Resolving deltas: 100% (1720/1720), done.
localhost:[~]
% cd .rbenv
localhost:[~/.rbenv] (master)
% ./src/configure
localhost:[~/.rbenv] (master)
% make -C src
make: ディレクトリ `/home/vagrant/.rbenv/src' に入ります
gcc -fPIC -c -o realpath.o realpath.c
gcc -shared -Wl,-soname,../libexec/rbenv-realpath.dylib -o ../libexec/rbenv-realpath.dylib realpath.o
make: ディレクトリ `/home/vagrant/.rbenv/src' から出ます
// 初期化はzshrcに書けと言われたのでzshrcに追記する
localhost:[~/.rbenv] (master)
% bin/rbenv init
# Load rbenv automatically by appending
# the following to ~/.zshrc:
eval "$(rbenv init -)"
// init後にshimsディレクトリができるので、そちらにもPATHを通しておく
localhost:[~/.rbenv] (master)
% vim ~/.zshrc
localhost:[~/.rbenv] (master)
% cat ~/.zshrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/shims:$PATH"
localhost:[~/.rbenv] (master)
% source ~/.zshrc
localhost:[~/.rbenv] (master)
% ll shims
合計 0
この後Rubyをインストールするにはrbenv install <version>
コマンドを実行するのですが、このコマンドを使うにはこちらのリポジトリのインストールシェルを実行する必要があります。
// homeでruby-buildをインストールする
localhost:[~/.rbenv] (master)
% cd ..
localhost:[~]
% git clone https://github.com/rbenv/ruby-build.git
Cloning into 'ruby-build'...
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 9231 (delta 0), reused 0 (delta 0), pack-reused 9229
Receiving objects: 100% (9231/9231), 1.92 MiB | 780.00 KiB/s, done.
Resolving deltas: 100% (6008/6008), done.
localhost:[~]
% sudo ./ruby-build/install.sh
// installコマンドが使えるようになった
// インストールできるversionに2.5.1があるのでインストールします
localhost:[~]
% rbenv install -l | grep 2.5.1
2.5.1
rbx-2.5.1
localhost:[~]
% rbenv install 2.5.1
Downloading ruby-2.5.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.bz2
Installing ruby-2.5.1...
BUILD FAILED (CentOS Linux 7 using ruby-build 20180822)
Inspect or clean up the working tree at /tmp/ruby-build.20180929170800.3912
Results logged to /tmp/ruby-build.20180929170800.3912.log
Last 10 log lines:
The Ruby openssl extension was not compiled.
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y openssl-devel readline-devel zlib-devel` to fetch missing dependencies.
Configure options used:
--prefix=/home/vagrant/.rbenv/versions/2.5.1
LDFLAGS=-L/home/vagrant/.rbenv/versions/2.5.1/lib
CPPFLAGS=-I/home/vagrant/.rbenv/versions/2.5.1/include
// 足りないPKGがあるそうなので、ログに書いてあるとおりにyumでインストールする
localhost:[~]
% sudo yum install -y openssl-devel readline-devel zlib-devel
.
.
.
インストール:
openssl-devel.x86_64 1:1.0.2k-12.el7 readline-devel.x86_64 0:6.2-10.el7 zlib-devel.x86_64 0:1.2.7-17.el7
依存性関連をインストールしました:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-19.el7 libcom_err-devel.x86_64 0:1.42.9-12.el7_5 libkadm5.x86_64 0:1.15.1-19.el7
libselinux-devel.x86_64 0:2.5-12.el7 libsepol-devel.x86_64 0:2.5-8.1.el7 libverto-devel.x86_64 0:0.2.5-4.el7 pcre-devel.x86_64 0:8.32-17.el7
完了しました!
// 再度インストールに挑戦
localhost:[~]
% rbenv install 2.5.1
Downloading ruby-2.5.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.bz2
Installing ruby-2.5.1...
Installed ruby-2.5.1 to /home/vagrant/.rbenv/versions/2.5.1
// インストールした2.5.1に切り替えます
localhost:[~]
% rbenv global 2.5.1
localhost:[~]
% ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
// ちなみに他のversionに切り替える場合は
// 切り替えたversionをインストールして同じように切り替える
localhost:[~]
% rbenv install 2.4.4
Downloading ruby-2.4.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.4.tar.bz2
Installing ruby-2.4.4...
Installed ruby-2.4.4 to /home/vagrant/.rbenv/versions/2.4.4
localhost:[~]
% rbenv global 2.4.4
localhost:[~]
% ruby -v
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-linux]
これでRubyのインストールは完了です。
SQLLite
続いてSQLiteが必要だそうなので、インストールします。ただ今回のCentOS7の場合、デフォルトでインストールされていました。存在しない場合はyumなどでインストールしてください。
localhost:[~]
% sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668
Rails
ここでやっとRailsをインストールします。といってもgemというパッケージ管理ツールがあるので、gemを使ってインストールするだけです。
localhost:[~]
% gem install rails
.
.
.
Done installing documentation for concurrent-ruby, i18n, thread_safe, tzinfo, activesupport, rack, rack-test, mini_portile2, nokogiri, crass, loofah, rails-html-sanitizer, rails-dom-testing, builder, erubi, actionview, actionpack, activemodel, arel, activerecord, globalid, activejob, mini_mime, mail, actionmailer, nio4r, websocket-extensions, websocket-driver, actioncable, mimemagic, marcel, activestorage, thor, method_source, railties, bundler, sprockets, sprockets-rails, rails after 48 seconds
39 gems installed
localhost:[~]
% rails -v
Rails 5.2.1
はい、これでRailsのインストール完了です。
【アプリ】
Railsアプリを作成して起動してみます。
生成
Railsはコマンド一つでアプリの雛形を作ってくれるようです、便利ですね。
// myappという名前のアプリを生成
localhost:[~]
% rails new myapp
.
.
.
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/sqlite3-1.3.13/ext/sqlite3
/home/vagrant/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20180929-28423-1411pav.rb extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (thelocation where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
.
.
.
Could not find gem 'sqlite3' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
sqlite-devel
を入れてから出直してこいと。。。sqliteだけではダメみたいですね。
まあ、書いてある通りにインストールして、もう一度ビルドします。
localhost:[~]
% sudo yum install sqlite-devel
.
.
.
インストール:
sqlite-devel.x86_64 0:3.7.17-8.el7
完了しました!
// アプリのビルドの途中でコケたのでディレクトリはもう存在しています。
// なのでmyappに移動して依存PKGのインストールの続きから行います。
localhost:[~]
% cd myapp
localhost:[~/myapp] (master)
% bundle install
.
.
.
Bundle complete! 18 Gemfile dependencies, 78 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
ビルドが終わり、アプリの生成が完了しました。
起動
生成したアプリを起動します。
localhost:[~/myapp] (master)
% ./bin/rails server
.
.
.
error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
.
.
.
execjs
を使用するためにはJavaScriptのランタイムPKGが必要だそうです。公式サイトにも書いてありますね。
こちらのREADMEにも書いてありますが、下記のランタイムPKGのいずれかがあれば良さそうです。
今回はGemfileにデフォルトで記述してあるmini_racer
を使用します。
// mini_racerのコメントを外します。
localhost:[~/myapp] (master)
% cat Gemfile | grep "gem 'mini_racer', platforms: :ruby"
# gem 'mini_racer', platforms: :ruby
localhost:[~/myapp] (master)
% sed -i -e "s/# gem 'mini_racer', platforms: :ruby/gem 'mini_racer', platforms: :ruby/g" Gemfile
localhost:[~/myapp] (master)
% cat Gemfile | grep "gem 'mini_racer', platforms: :ruby"
gem 'mini_racer', platforms: :ruby
// そしてインストール...
localhost:[~/myapp] (master)
% bundle install
.
.
.
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mini_racer-0.2.3/ext/mini_racer_extension
/home/vagrant/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20180929-2395-2mokwi.rb extconf.rb
checking for -lpthread... yes
sh: 行 3: g++: コマンドが見つかりません
.
.
.
// gccがないとビルドできないそうなのでインストール...
localhost:[~/myapp] (master)
% sudo yum -y install gcc-c++
.
.
.
インストール:
gcc-c++.x86_64 0:4.8.5-28.el7_5.1
依存性関連をインストールしました:
libstdc++-devel.x86_64 0:4.8.5-28.el7_5.1
完了しました!
// 再度インストール実行
localhost:[~/myapp] (master)
% bundle install
.
.
.
Bundle complete! 19 Gemfile dependencies, 80 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
// 今度こそ上手くいくはず...
localhost:[~/myapp] (master)
% ./bin/rails server
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
// errorも出ていないようなのでアプリの起動に成功したと思います。
とりあえず同じサーバ上でhttp接続できるか確認してみます。
localhost:[~]
% curl http://localhost:3000/
<!DOCTYPE html>
<html>
.
.
.
</html>
HTMLが取得できるので、大丈夫そうです。
あとはPort80で接続させるようにします。これにはいろいろやり方があるようで、今回はApacheをReverseProxyとしてRailsアプリに流すように設定します。
// すでにhttpdが入っているので設定ファイルを修正します。
localhost:[~]
% sudo vim /etc/httpd/conf/httpd.conf
.
.
.
<IfModule mod_proxy.c>
ProxyRequests On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</IfModule>
.
.
.
localhost:[~]
% sudo systemctl start httpd.service
ブラウザで確認します。
デフォルトページが表示されました。
一応目的は達成しましたが、コントローラの作成もしてみます。
こちらもコマンド一つでコントローラやviewテンプレートの作成、ルーティングの設定まで自動でやってくれるみたいです、便利すぎる。。。
localhost:[~/myapp] (master)
% ./bin/rails generate controller Welcome index
create app/controllers/welcome_controller.rb
route get 'welcome/index'
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/welcome.coffee
invoke scss
create app/assets/stylesheets/welcome.scss
localhost:[~/myapp] (master)
// 下記が表示されるHTML
% cat app/views/welcome/index.html.erb
<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>
作成したコントローラのURLを叩いてみます。
index.html.erb
の内容が確認できますね。
【まとめ】
まとめるほどのことはないですが、CentOS7上でRailsアプリを起動してみました。次回以降はDB連携部分などやっていきたいと思います。