Apache2.4+passenger5でrails4.2を動かす
o_wani
owani.net#stay.hungry
前に作ってたrailsの開発環境が古くなったので、OSなどのバージョンを新しくして、Vagrant+CentOS7でRuby on Rails5で環境を構築する。
前提としては、「Vagrant」と「VirtualBox」はインストールしておく。
今回のプロジェクト用にディレクトリを作成する
$ mkdir ruby-centos7-project
$ cd ruby-centos7-project/
vagrantの起動設定をする
公式BOXのCentOS7を使用する
Vagrant Boxes(参考)
$ 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.
ホストマシンの3000番ポートをゲストOSの3000番ポートへフォワード
config.vm.network "forwarded_port", guest: 3000, host: 3000
少し時間がかかる
$ vagrant up
仮想環境へログイン
$ vagrant ssh
仮想環境にログインしてからの設定
既存のミドルウェアを更新する
$ sudo yum -y update
rubyをインストールするまでに必要な手順
コンパイラなどをインストールしておく
$ sudo yum -y install git-all openssl-devel readline-devel sqlite gcc gcc-c++
複数のrubyのバージョンを管理できるツールをインストールする
$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ echo 'gem: --no-ri --no-rdoc' > ~/.gemrc
$ exec $SHELL
rbenvがインストールされたことを確認する
$ rbenv --version
rbenv 1.1.1-28-gb943955
時間がかかる
$ rbenv install 2.5.0
Downloading ruby-2.5.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2
Installing ruby-2.5.0...
Installed ruby-2.5.0 to /home/vagrant/.rbenv/versions/2.5.0
インストールしたバージョンの確認
$ rbenv versions
* 2.5.0 (set by /home/vagrant/.rbenv/version)
環境全体のrubyのバージョンを2.5.0に設定
$ rbenv global 2.5.0
・インストールしたバージョンの確認
rubyのバージョン確認
$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
rubyのライブラリ管理ツールをインストール
$ gem install bundler
Fetching: bundler-1.16.1.gem (100%)
Successfully installed bundler-1.16.1
1 gem installed
railsとそれに必要なパッケージをインストール
$ gem install rails
Fetching: concurrent-ruby-1.0.5.gem (100%)
Successfully installed concurrent-ruby-1.0.5
Fetching: i18n-0.9.1.gem (100%)
Successfully installed i18n-0.9.1
Fetching: thread_safe-0.3.6.gem (100%)
Successfully installed thread_safe-0.3.6
Fetching: tzinfo-1.2.4.gem (100%)
Successfully installed tzinfo-1.2.4
Fetching: activesupport-5.1.4.gem (100%)
Successfully installed activesupport-5.1.4
Fetching: rack-2.0.3.gem (100%)
Successfully installed rack-2.0.3
Fetching: rack-test-0.8.2.gem (100%)
Successfully installed rack-test-0.8.2
Fetching: mini_portile2-2.3.0.gem (100%)
Successfully installed mini_portile2-2.3.0
Fetching: nokogiri-1.8.1.gem (100%)
Building native extensions. This could take a while...
Successfully installed nokogiri-1.8.1
Fetching: crass-1.0.3.gem (100%)
Successfully installed crass-1.0.3
Fetching: loofah-2.1.1.gem (100%)
Successfully installed loofah-2.1.1
Fetching: rails-html-sanitizer-1.0.3.gem (100%)
Successfully installed rails-html-sanitizer-1.0.3
Fetching: rails-dom-testing-2.0.3.gem (100%)
Successfully installed rails-dom-testing-2.0.3
Successfully installed rails-dom-testing-2.0.3
Fetching: builder-3.2.3.gem (100%)
Successfully installed builder-3.2.3
Fetching: erubi-1.7.0.gem (100%)
Successfully installed erubi-1.7.0
Fetching: actionview-5.1.4.gem (100%)
Successfully installed actionview-5.1.4
Fetching: actionpack-5.1.4.gem (100%)
Successfully installed actionpack-5.1.4
Fetching: activemodel-5.1.4.gem (100%)
Successfully installed activemodel-5.1.4
Fetching: arel-8.0.0.gem (100%)
Successfully installed arel-8.0.0
Fetching: activerecord-5.1.4.gem (100%)
Successfully installed activerecord-5.1.4
Fetching: globalid-0.4.1.gem (100%)
Successfully installed globalid-0.4.1
Fetching: activejob-5.1.4.gem (100%)
Successfully installed activejob-5.1.4
Fetching: mini_mime-1.0.0.gem (100%)
Successfully installed mini_mime-1.0.0
Fetching: mail-2.7.0.gem (100%)
Successfully installed mail-2.7.0
Fetching: actionmailer-5.1.4.gem (100%)
Successfully installed actionmailer-5.1.4
Fetching: nio4r-2.2.0.gem (100%)
Building native extensions. This could take a while...
Successfully installed nio4r-2.2.0
Fetching: websocket-extensions-0.1.3.gem (100%)
Successfully installed websocket-extensions-0.1.3
Fetching: websocket-driver-0.6.5.gem (100%)
Building native extensions. This could take a while...
Successfully installed websocket-driver-0.6.5
Fetching: actioncable-5.1.4.gem (100%)
Successfully installed actioncable-5.1.4
Fetching: thor-0.20.0.gem (100%)
Successfully installed thor-0.20.0
Fetching: method_source-0.9.0.gem (100%)
Successfully installed method_source-0.9.0
Fetching: railties-5.1.4.gem (100%)
Successfully installed railties-5.1.4
Fetching: sprockets-3.7.1.gem (100%)
Successfully installed sprockets-3.7.1
Fetching: sprockets-rails-3.2.1.gem (100%)
Successfully installed sprockets-rails-3.2.1
Fetching: rails-5.1.4.gem (100%)
Successfully installed rails-5.1.4
35 gems installed
rails5.1.4
がインストールされた
$ rails -v
Rails 5.1.4
MySQL(mariadb)をインストール
$ sudo yum install mariadb-server mariadb-devel
MySQL(mariadb)起動
$ sudo systemctl start mariadb
初期インストール
$ sudo mysql_secure_installation
railsプロジェクトhoge作成する。データベースをmysqlに指定、springアプリケーションをインストールしない。
$ rails new hoge --database=mysql --skip-spring
Gemfileの修正 (therubyracerを有効にする)
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
必要パッケージのインストール
$ bundle install
Webサーバ起動
$ rails s -b 0.0.0.0
http://localhost:3000
ブラウザで確認しよう。
データベースのことはまた書きます。
Vagrant + VirtualBoxでつくるRails 5の環境構築