LoginSignup
1
4

More than 5 years have passed since last update.

Vagrant + VirtualBox で Rails開発環境構築

Last updated at Posted at 2017-10-09

事前にインストール

※環境
windows10 64bit
(前記事 msys2 + MinGW64 でWindows10にターミナル環境構築 にもとづいてターミナル環境構築済み)
Vagrant 1.9.7
VM VirtualBox 5.1.26

Vagrant

Box作成

$ vagrant box add (name) (url)

※ ex) CentOS6.8を導入する場合

$ vagrant box add ◯◯(任意ですが私はapp名にすることが多い) https://atlas.hashicorp.com/kaorimatz/boxes/centos-6.8-x86_64/versions/20160703.0.0/providers/virtualbox.box

Box確認

$ vagrant box list

設定

$ cd Documents
$ mkdir (Boxの名前)
$ cd (Boxの名前)
$ vagrant init (name)

Vagrantfile

config.vm.provider :virtualbox do |vb|
# # Don’t boot with headless mode
# vb.gui = true
#
# # Customize the amount of memory on the VM:
vb.memory = "2048"
end

# config.vm.hostname = "ledger.com” #← IP以外で設定したい場合
config.vm.network "private_network", ip: "192.168.33.10" #← 他の番号も可

# config.vm.synced_folder "../data", "/vagrant_data"
# ↓ コメントアウトを外して下記に変更
config.vm.synced_folder".", "/◯◯", create: "true", mount_options: ['dmode=755','fmode=644']

仮想マシン起動

$ vagrant up

仮想マシンにアクセス

$ vagrant ssh

※文字化けしている時
localPC ↓
/etc/ssh/ssh_config の SendEnv LANG LC_*行をコメントアウト
ssh を exit で落として vagrant reload

インストール

curl・git・rubyなど

$ sudo yum --exclude=systemtap* groupinstall 'Development Tools' && sudo yum install curl git m4 ruby texinfo bzip2-devel curl-devel expat-devel ncurses-devel zlib-devel openssl-devel irb

brew

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
$ echo 'export PATH="$HOME/.linuxbrew/bin:$PATH"' >> ~/.bash_profile
$ echo 'export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH"' >> ~/.bash_profile
$ echo 'export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"' >> ~/.bash_profile
$ echo 'export LD_LIBRARY_PATH="$HOME/.linuxbrew/lib:$LD_LIBRARY_PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
$ gcc -dumpversion
$ ln -s `which gcc` `brew --prefix`/bin/gcc-4.4
$ ln -s `which cpp` `brew --prefix`/bin/cpp-4.4
$ ln -s `which g++` `brew --prefix`/bin/g++-4.4
$ brew -v

tree(ディレクトリのファイル・ディレクトリ構造を表示できる)

$ sudo yum install tree

xclip(コンソール画面からクリップボードにコピーできる)

$ sudo yum install xclip

gem(ruby言語用パッケージ管理用ツール)

$ sudo yum install rubygems

git(言わずと知れたファイルのバージョン管理用ツール)

$ sudo yum install git

vim(プラグイン入れて最強のエディタに)

$ sudo yum install vim
$ mkdir -p ~/.vim/bundle
$ curl https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh > installer.sh
$ sh installer.sh ~/.vim/bundle
$ cd ~/.vim
$ git clone https://github.com/Shougo/dein.vim.git ~/.vim/dein/repos/github.com/Shougo/dein.vim

(colorscheme)
$ mkdir colors
$ git clone https://github.com/tomasr/molokai
$ mv molokai/colors/molokai.vim ~/.vim/colors/

(gitでdotfiles管理している場合)
$ cd ~
$ git clone https://●●@bitbucket.org/●●/dotfiles.git
$ rm -f .vimrc
$ cp ~/dotfiles/_vimrc ~/.vimrc

$ vim (確認)

rbenv (rubyのバージョン管理用ツール)

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ rbenv --version

Ruby 2.x (今回は2.2.3)

$ sudo yum install gcc make openssl-devel readline-devel
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ rbenv install -l
$ rbenv install 2.2.3
$ rbenv global 2.2.3
$ ruby -v

Rails

$ gem install rails

httpd(webサーバ)

$ sudo yum -y install httpd
$ apachectl -v

サーバーが起動したらwebサーバーを起動するように

$ sudo chkconfig httpd on
$ sudo vim /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf
# ポート番号の確認
ServerTokens Prod #44行目

Listen 80 #136行目

# ServerName を有効にする #285行目
UseCanonicalName On

<Directory /> #305行目
    Options FollowSymLinks
    AllowOverride None
    EnableMMAP Off  #追加
    EnableSendfile Off #追加
</Directory>

# サーバーの名前の情報(ホスト名)を設定する
ServerName www.example.com:80

#
ServerSignature Off #538行目

起動確認

$ sudo service httpd restart

Passenger(Railsアプリを実行するためのApacheモジュール)

$ gem install passenger
$ rbenv rehash
$ passenger-install-apache2-module

※エラーが出た場合は足りないものをインストール

$ sudo yum install curl-devel httpd-devel apr-devel apr-util-devel

再度以下を叩く

$ passenger-install-apache2-module
$ passenger-install-apache2-module --snippet

上のコマンドで表示されたスニペットを貼り付け

$ sudo vim /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf
LoadModule passenger_module /Users/●●/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.1.2/buildout/apache2/mod_passenger.so
  <IfModule mod_passenger.c>
  PassengerRoot /Users/●●/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.1.2
  PassengerDefaultRuby /Users/●●/.rbenv/versions/2.2.3/bin/ruby
  </IfModule>

<VirtualHost *>
  ServerName 192.168.33.11
  DocumentRoot /◯◯/◯◯_app/public
  RailsEnv development
  #RailsEnv production
  PassengerEnabled on
</VirtualHost>

iptableを止める

$ sudo service iptables stop
$ sudo chkconfig iptables off

SELinuxも止める

$ sudo vim /etc/sysconfig/selinux
SELINUX=disabled

MySQL

$ sudo yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
$ sudo yum install mysql-community-client mysql-community-common mysql-community-devel mysql-community-libs mysql-community-libs-compat mysql-community-release mysql-connector-python mysql-utilities mysql-community-server

確認

$ sudo yum list installed | grep mysql
$ mysql --version
$ mysqld --version

サーバーを起動したら自動的に MySQL Server が起動するように設定

$ sudo chkconfig mysqld on

起動確認

$ sudo service mysqld start
$ sudo service mysqld stop
$ sudo service mysqld restart

root でログイン

$ mysql -uroot

ユーザー作成

mysql> create user 'ledger'@'localhost' identified by 'runner';

ユーザーの権限を確認

mysql> show grants for '◯◯'@'localhost';

ユーザーに権限付与

mysql> grant all privileges on *.* to '◯◯'@'localhost';

権限の反映

mysql> flush privileges;

データベースを選択

mysql> use mysql;

登録されているユーザーとホストを確認

mysql> select user,host from mysql.user;

Ctrl + C で抜ける。

指定のユーザーでログイン

$ mysql -u ledger -p

Enterを押す

password: 

パスワードを入れる。
※ちなみに、

$ mysql -uledger -p****(パスワード) 

で最初からパスワード入れてすっ飛ばすことも可能

データベース作成

mysql> create database ◯◯_production;
mysql> create database ◯◯_development;

文字コードのデフォルトを utf8 に設定

mysql> ALTER DATABASE `◯◯_production` default character set utf8;
mysql> ALTER DATABASE `◯◯_development` default character set utf8

mysqlの設定変更

$ sudo vim /etc/my.cnf
/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character-set-server=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8

Railsによるアプリ作成

$ cd /◯◯
$ rails new ◯◯_app
#gem 'sqlite3'
#上記コメントアウトし下記を追記
gem 'mysql2'
gem 'therubyracer', platforms: :ruby
config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  timeout: 5000
  username: ◯◯
  password: ****
  host: localhost

development:
  <<: *default
  database: ◯◯_development

#test:
#  <<: *default
#  database: db/test.sqlite3

production:
  <<: *default
  database: ◯◯_production
$ cd ◯◯_app
$ bundle install 
$ rake db:migrate RAILS_ENV=development
$ sudo service httpd restart
$ passenger-config restart-app

ブラウザでアドレス打って確認。

その他

仮想環境内でzshを使う場合

zshがインストールされているかを確認

$ zsh --version

yumでzshをインストール

$ sudo yum -y install zsh

使用可能なshellの確認

$ cat /etc/shells

ログイン・シェルに設定

$ chsh -s /bin/zsh

設定ファイルを編集

$ cd ~
$ vim .zshenv(何も記入しないまま保存)
$ vim .zshrc

.zshrcはまた後日ご紹介の予定。

$ zsh
$ source ~/.zshrc

rbenvのパスを通す

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshenv
$ echo 'eval "$(rbenv init -)"' >> ~/.zshenv
$ source ~/.zshenv

動作確認(バージョンが確認できればOK)

$ rbenv -v
1
4
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
1
4