Mac上にVagrantをインストールして、複数のWEBサーバを立ち上げるまで

# Vagrant for Mac OS Xのダウンロード
  
```  
http://www.vagrantup.com/downloads.html
```  
パッケージ「Vagrant.pkg」がダウンロードされるので、そのままインストール。
コンソール上でインストール出来たかの確認
  
```  
$ vagrant --version
Vagrant 1.4.3

$ which vagrant
/usr/bin/vagrant
```  
# BOX(仮想OSのテンプレート)を取得する
  
```  
vagrant box add <任意のBOX名> <ダウンロードするBOXのURL>
```  
### 例:CentOS6.4 64bit(VirtualBox用)のBOXを"testOS_CentOS"という名前でダウンロードする
  
```  
$ vagrant box add testOS_CentOS https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box
```
=>Successfulyと出ればOK.  
###boxの確認方法  
  
```  
$ vagrant box list
testOS_CentOS (virtualbox)
```
###boxの削除方法  
  
```  
$ vagrant box remove testOS_CentOS
testOS_CentOS (virtualbox)
```  

### 例:CentOS6.4 64bit(VirtualBox用)のBOXを"CentOS64"という名前でダウンロードする
  
```  
$ vagrant box add CentOS64 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box

$ vagrant box list
CentOS64 (virtualbox)
```  

# 取得したBOXから仮想OSを立ち上げる
任意のディレクトリ下に仮想OSそれぞれのディレクトリ(今回はws01)を作り、そこを仮想OS用に初期化する。  
  
```
$ cd 
$ mkdir -p ~/vagrantOS/ws01
$ cd ~/vagrantOS/ws01
$ vagrant init CentOS64
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.
```

# vagrant経由の仮想OSのｺﾏﾝﾄﾞ  
状態確認
  
```  
$ vagrant status
```  
起動と終了
  
```  
$ vagrant up
$ vagrant halt
```  
スリープと復帰
  
```  
$ vagrant supend
$ vagrant resume
```  
再起動
  
```  
$ vagrant reload
```  
※仮想OSの削除
  
```  
$ vagrant destroy
```  
#WEBサーバの立ち上げ
プライベートIPの有効化  
  
```  
$ cd ~/vagrantOS/ws01
$ vim Vagrantfile
```  
```text:Vagrantfile
- # config.vm.network :private_network, ip: "192.168.33.10"
+ config.vm.network :private_network, ip: "192.168.33.10"
```  
再起動による設定再読み込み  
  
```  
$ vagrant reload
```  
##ssh接続  
```
$ vagrant ssh
```
※ここから仮想OS上※
apacheのインストールと立ち上げ
  
```
$ sudo yum -y install httpd
$ sudo service httpd start
$ sudo chkconfig httpd on
4 sudo chkconfig httpd --list
httpd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
```  
FWの停止と自動立ち上げのオフ  
  
```
$ sudo service iptables stop
$ sudo chkconfig iptables off
$ sudo chkconfig iptables --list
iptables       	0:off	1:off	2:off	3:off	4:off	5:off	6:off
```
SELINUXの無効化  
  
```  
$ sudo vi /etc/sysconfig/selinux
```
```:/etc/sysconfig/selinux
+ SELINUX=disabled
```  
# WEBページの作成と表示  
indexページの作成  
  
```  
$ sudo vi /var/www/html/index.html
```  
```:index.html
<h1>Hello World !</h1>
```  
ブラウザに「192.168.33.10」と入れて「Hello World !」と表示されればOK.

#複数の仮想OSを立ち上げるには
後日作成

# 開発環境で必要そうなもの  
### root権限
```
$ su
Password:vagrant
# 
```  
### vimのインストール  
  
```   
$ sudo yum install vim-enhanced
```
### yumリポジトリの確認
  
```  
$ sudo yum repolist all
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * epel: ftp.kddilabs.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: ftp.tsukuba.wide.ad.jp
repo id                repo name                                 status
C6.0-base              CentOS-6.0 - Base                         disabled
C6.0-centosplus        CentOS-6.0 - CentOSPlus                   disabled
C6.0-contrib           CentOS-6.0 - Contrib                      disabled
C6.0-extras            CentOS-6.0 - Extras                       disabled
C6.0-updates           CentOS-6.0 - Updates                      disabled
C6.1-base              CentOS-6.1 - Base                         disabled
C6.1-centosplus        CentOS-6.1 - CentOSPlus                   disabled
C6.1-contrib           CentOS-6.1 - Contrib                      disabled
C6.1-extras            CentOS-6.1 - Extras                       disabled
C6.1-updates           CentOS-6.1 - Updates                      disabled
C6.2-base              CentOS-6.2 - Base                         disabled
C6.2-centosplus        CentOS-6.2 - CentOSPlus                   disabled
C6.2-contrib           CentOS-6.2 - Contrib                      disabled
C6.2-extras            CentOS-6.2 - Extras                       disabled
C6.2-updates           CentOS-6.2 - Updates                      disabled
C6.3-base              CentOS-6.3 - Base                         disabled
C6.3-centosplus        CentOS-6.3 - CentOSPlus                   disabled
C6.3-contrib           CentOS-6.3 - Contrib                      disabled
C6.3-extras            CentOS-6.3 - Extras                       disabled
C6.3-updates           CentOS-6.3 - Updates                      disabled
C6.4-base              CentOS-6.4 - Base                         disabled
C6.4-centosplus        CentOS-6.4 - CentOSPlus                   disabled
C6.4-contrib           CentOS-6.4 - Contrib                      disabled
C6.4-extras            CentOS-6.4 - Extras                       disabled
C6.4-updates           CentOS-6.4 - Updates                      disabled
base                   CentOS-6 - Base                           enabled: 6359+8
c6-media               CentOS-6 - Media                          disabled
centosplus             CentOS-6 - Plus                           disabled
contrib                CentOS-6 - Contrib                        disabled
debug                  CentOS-6 - Debuginfo                      disabled
epel                   Extra Packages for Enterprise Linux 6 - x enabled:  10500
epel-debuginfo         Extra Packages for Enterprise Linux 6 - x disabled
epel-source            Extra Packages for Enterprise Linux 6 - x disabled
epel-testing           Extra Packages for Enterprise Linux 6 - T disabled
epel-testing-debuginfo Extra Packages for Enterprise Linux 6 - T disabled
epel-testing-source    Extra Packages for Enterprise Linux 6 - T disabled
extras                 CentOS-6 - Extras                         enabled:     14
updates                CentOS-6 - Updates                        enabled: 511+24
repolist: 17384
```  

# yum upgradeをするとエラる
vagrant内でyum upgradeするとVirtualBoxのGuest Additionsが動作しなくなり、 共有フォルダのマウントに失敗してしまう。
この時はGuest Additionsを再ビルドすれば良い。

* エラー

```text:cmd
[vagrant]$ yum upgrade -y
[vagrant]$ exit
logout
Connection to 127.0.0.1 closed.
[iganari]$
[iganari]$
[iganari]$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/vpn-igarashi/vagrant/dev-vagrant
Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

The error output from the last command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device
```

* 対処法

```text:cmd
[iganari]$
[iganari]$ vagrant ssh
[vagrant]$   <------ 一応、sshログインは出来る
[vagrant]$
[vagrant]$ sudo yum install -y gcc perl kernel-devel
[vagrant]$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]
[vagrant]$ exit
logout
Connection to 127.0.0.1 closed.
[iganari]$
[iganari]$
[iganari]$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/vpn-igarashi/vagrant/dev-vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

----> 次は成功
```


参考URL
[Vagrantの上のCentOS 6.5でyum updateした後、vagrant upで発生したエラーの原因と対処](http://blog.chopschips.net/blog/2014/09/08/vagrant-problem-with-yum-update/)


# vagrant上のdateがズレる

```
[vagrant]$ date
2015年  2月 12日 木曜日 11:37:57 EST
[vagrant]$ 
[vagrant]$ sudo rm /etc/localtime
[vagrant]$ sudo ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
[vagrant]$ date
2015年  2月 13日 金曜日 01:40:02 JST
```


# boxを作り直す
一度作成したVagrantBoxに対して、updateや追加のコマンドを最初から入れておきたい場合には、修正を加えて新たにBaseBoxにしてしまえばいい。

```
[iganari]$ vagrant package
```
package.boxという名前のBoxファイルが生成されるので、新しくVBのBaseBoxとして作成する。

```
[iganari]$ vagrant box add new_box package.box
```