備忘録　Vagrant + VirtualBox + Centos7.2 の環境構築

#### ローカル環境にCentos7.2サーバーを２つ立ち上げる
- バックエンド用とフロントエンド用のサーバーを別々に立ち上げた時のメモ
[サーバー仕様]

| 設定       |       バックエンド |    フロントエンド    |備考|
|:-----------|-------------:|--------------:|:---| 
| OS         |   centos7    |      centos7 |     |
| hostname   | backend      |       front  |設定するとVirtual Boxのマシン名になった気がする|
| public ip  | 192.168.0.10 | 192.168.0.50 |     |
| private ip | 192.168.33.10| 192.168.33.50|    |
| port(guest)| 8080         |            80|    |
| port(host) | 8080         |            80|    |

``` Vagrantfile:sample1.sh
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"

  config.ssh.insert_key = false

  # server 1 (バックエンド用サーバーとして作成）
  config.vm.define :backend do | backend |
    # ホスト名
    backend.vm.hostname = "backend"
    # Disable automatic box update checking. If you disable this, then
    # boxes will only be checked for updates when the user runs
    # `vagrant box outdated`. This is not recommended.
    # config.vm.box_check_update = false

    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine. In the example below,
    # accessing "localhost:8080" will access port 80 on the guest machine.
    # NOTE: This will enable public access to the opened port
    # ホスト:8080 → ゲスト:8080
    backend.vm.network "forwarded_port", guest: 8080, host: 8080

    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine and only allow access
    # via 127.0.0.1 to disable public access
    # ホスト:80 → ゲスト:8080
    # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    backend.vm.network "private_network", ip: "192.168.33.10"

    # Create a public network, which generally matched to bridged network.
    # Bridged networks make the machine appear as another physical device on
    # your network.
    backend.vm.network "public_network", ip: "192.168.0.10"
  end

  # server 2 (フロントエンド用サーバーとして作成）
  config.vm.define :front do | front |
    # ホスト名
    front.vm.hostname = "front"
    # Disable automatic box update checking. If you disable this, then
    # boxes will only be checked for updates when the user runs
    # `vagrant box outdated`. This is not recommended.
    # config.vm.box_check_update = false

    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine. In the example below,
    # accessing "localhost:8080" will access port 80 on the guest machine.
    # NOTE: This will enable public access to the opened port
    front.vm.network "forwarded_port", guest: 80, host: 80

    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine and only allow access
    # via 127.0.0.1 to disable public access
    # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    front.vm.network "private_network", ip: "192.168.33.50"

    # Create a public network, which generally matched to bridged network.
    # Bridged networks make the machine appear as another physical device on
    # your network.
    front.vm.network "public_network", ip: "192.168.0.50"
  end
end
```

#### ローカル環境にCentos7.2サーバーを立ち上げる
- バックエンド用とフロントエンド用のサーバーを別々に立ち上げた時のメモ
- ローカル環境でいくつも仮想環境を立ち上げて開発作業などを行う際などに、ポートが衝突してVagrantが起動出来ない場合がある。そんな時はポートを指定して上げることでポートの衝突を回避できる。


[サーバー仕様]

| 設定       |     フロントエンド    |備考|
|:---------------|---------------:|:---| 
| OS             |       centos7 |     |
| hostname       |        front  |設定するとVirtual Boxのマシン名になった気がする|
| public ip      |  192.168.0.50 |     |
| private ip     |  192.168.33.50|    |
| tcp port(guest)|             80|    |
| tcp port(host) |             80|    |
| ssh port(guest)|             2200| |
| ssh port(host) |             2200| |
| udp port(guest)|             2222|上記ポートが衝突した際にudpを利用|
| udp port(host) |             2222|上記ポートが衝突した際にudpを利用|


``` Vagrantfile:sample1.sh
##############
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"

  config.ssh.insert_key = false

  # ホスト名
  config.vm.hostname = "front"
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  config.vm.network "forwarded_port", id: "ssh", guest: 2200, host: 2200
  config.vm.network "forwarded_port", guest:2222, host:2222, protocol:"udp"
  config.vm.network "forwarded_port", guest:80, host:80, protocol:"tcp"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.50"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network", ip: "192.168.0.50"
end
```

#### Vagrant コマンド
- 使ったことがあるのは今のところ大体これくらい。

| コマンド   | 説明 |
|-----------|------|
|vagrant up     | 起動 |
|vagrant halt   | 停止 |
|vagrant status | 状態確認 |
|vagrant suspend | 一時停止|
|vagrant provision| プロビジョニング|
|vagrant reload | リロード|
|vagrant ssh    | 仮想マシンへログイン |
|vagrant ssh-config| ssh config確認 |
|vagrant version| バージョン確認|
|vagrant destroy| 仮想マシン削除|
|vagrant box list| ボックス一覧表示|
|vagrant box remove boxname|ボックス(boxname)を削除|
|vagrant snapshot save snapshot_name | スナップショット(snapshot_name)を作成|
|vagrant snapshot delete snapshot_name | スナップショット(snapshot_name)を削除|
|vagrant snapshot list|スナップショット一覧表示|

#### まとめ
英語のコメントはどんな設定があったか忘れないように残しておく。