VM上でRails構築してみた

Ruby on Rails をローカルに構築するのはちょっとかっこわるいのでVM上に構築してみた!

そこで備忘録だぁぁぁ!!

 

今回の開発環境

構築した環境

  • rbenv

    
    $ rbenv --version
    rbenv 1.1.1-2-g615f844
    
  • Ruby

    $ ruby -v ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux] 
    動作確認できたのは2.3.0 4.1.0も一応インストール
  • gem

    $ gem -v
    2.5.1
    yumでインストール
  • bundler

    $ bundle -v
    Bundler version 1.15.3
  • Rails

    $ rails -v
    Expected string default value for '--rc'; got false (boolean)
    Rails 4.2.2

 

Railsインストールの前準備

rbenv

$ sudo yum -y install gcc gcc-c++ make bzip2 libyaml-devel libffi-devel zlib-devel openssl-devel readline-devel gdbm-devel ncurses-devel
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

.bash_profileに追加

$ sudo vi ~/.bash_profile
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
$ source ~/.bash_profile

ruby

$ rbenv install 2.3.0
$ rbenv global 2.3.0

gem

$ sudo yum install gem

各ツール

$ sudo yum -y install libxml2 libxslt libxml2-devel libxslt-devel
$ sudo gem install nokogiri -- --use-system-libraries

 ∗ なんか2回目の構築の際に上記のinstallでnokogiriが上手くインストールされなかった

下記のinstallでnokogiriをinstall

 ∗ $ gem install nokogiri -v '1.6.6.4'
$ sudo yum -y install sqlite sqlite-devel
$ sudo gem install sqlite3

bundler

$ sudo gem install bundler

Rails

$ sudo gem install rails --version 4.2.2

 

インストールしたRuby on Railsを動かそう

$ cd
$ mkdir workspace
$ cd workspace
$ cd ~/workspace
$ rails new hello_app

rails new プロジェクト名で指定したプロジェクトファイルの中に大量のファイルとディレクトリが作成される。この大量にあるファイルの中から編集する所を選択して編集するだけでRailsアプリケーションが簡単にできる!これぞ、フレームワークの醍醐味!

 

vim ~/hello_app/Gemfile

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.2'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme
# for more supported runtimes
gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster.
# Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5.x'
# Build JSON APIs with ease.
# Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution
  # and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using
  # <%= console %> anywhere in the code.
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running
  # in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

Defaultでは gem 'therubyracer', platforms: :rubyコメントアウトになっているので コメントアウトを解除する

 

Gemをインストール

$ bundle install

Gemfileやディレクトリを変更するたびにbundle installしないといけない

 

Webサーバを起動

$ rails server -b VMのIP -p 3000

 

Webブラウザで3000番portにhttp接続

http://VM IP :3000

Ex)  $ rails server -b 255.255.255.255 -p 3000

     http://255.255.255.255:3000

だがしかし

 

サーバーに接続できません(^q^)

何故だ,何故だ,何故だ,もはやポルナレフ状態

あ、そうか...

FireWallの設定だ

 

Firewallの設定


$ firewall-cmd --zone=public --add-port=3000/tcp --permanent
 #3000番ポートの開放
$ firewall-cmd --reload
 #リロード

 

FireWalldをsystemctlで起動した状態で

 

確認


$ firewall-cmd --list-all

public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: **************
  ports: 3000/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules:
  

3000番portの開放確認

この状態だとlsofコマンドとpsコマンドでプロセスを確認できる

 

Webブラウザで確認

 

f:id:ayataka00:20170815094010p:plain

WebブラウザでRailsが確認できた!これで環境構築は一通り完了だ!

 

今回参考にしたページ

CentOS7にRuby on Railsを導入する方法