k-holyのPHPとか諸々メモ

Webで働くk-holyがPHP(スクリプト言語)とか諸々のことをメモしていきます。ソースコードはだいたいWindowsで動かしてます。

Windows7+VirtualBox+Vagrant再入門

Vagrantは2013年の記事 Windows7にVirtualBoxとVagrantをインストールしたメモ でインストールはしてみたものの、日々の開発作業に追われるまま長らく使っていませんでした。

実はこのブログでは一番人気の記事で、言及リンクしていただいた数も最も多いのですが、書いた私自身はほぼそれっきりでVagrant使ってなかったという…。(ノ∀`)

ローカル開発で常用するには重くて厳しいし、そもそも担当者は自分だけで管理してるサーバも片手で数えられる程、root権限貰って一から構築できる機会もない、といった理由で学習コストに見合う利点がまだ感じられなかったのです。

(自分自身がそうなんだから、上司にとってはもっとそうだろうなぁと…なかなかこういう開発環境構築、みたいな作業に何日も使うのも難しいんですよね。)

しかし、今回再び新規サーバ環境を構築機会ができ、そろそろ一人担当からも卒業しなければ、という流れにもなってきましたので、再び入門してみたメモです。

以下、手順は途中まで前の記事そのままですが、Windowsで開発中のApache + MySQL + PHPアプリケーションのテスト環境構築をAnsibleで自動化するというのが真の目標なので、とりあえずVagrantBoxのインストールと起動、Vagrantfileの編集までに留めます。

また、ゲストOSからホストOSの所属するLAN上のデータベースを利用したいので、プライベートネットワークを有効にします。

VirtualBoxを入れる

Downloads - Oracle VM VirtualBox から VirtualBox 4.3.26 for Windows hosts x86/amd64 をダウンロードして実行。

Setup Wizardが起動するので Next Next。なんかOracle製のドライバ類が色々インストールされるけど気にしない。

Vagrantを入れる

Download Vagrant - Vagrant から WINDOWS Universal (32 and 64-bit) をダウンロードして実行。

Setup Wizardが起動するので Next Next。インストールが終わると「再起動しろ」と言われるので、再起動します。

Vagrant Boxファイルを入れる

A list of base boxes for Vagrant - Vagrantbox.es から、使いたい仮想サーバのイメージファイルを選択して、Vagrantに追加します。

今回も使い慣れたCentOS6系で CentOS 6.4 i386 Minimal (VirtualBox Guest Additions 4.3.2, Chef 11.8.0, Puppet 3.3.1) を選択。

以下、コマンドはNYAOSでホームディレクトリにて実行。

ホームディレクトリにてVagrant boxを追加

$ vagrant box add centos64_i386 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20131103.box
==> box: Adding box 'centos64_i386' (v0) for provider:
    box: Downloading: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20131103.box
    box: Progress: 100% (Rate: 9639k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos64_i386' (v0) for 'virtualbox'!

以前と比べて、進捗が分かりやすく表示されています。

'centos64_i386' (v0) と表示されているのは、今のVagrantではboxファイルのバージョン管理を行ってくれるためのようです。

Vagrantが管理しているboxは追加の際に付けた名前でディレクトリが作成され、例えば今回の場合は ~/.vagrant.d/boxes/centos64_i386/0/virtualbox 以下にファイルが配置されます。間の 0 がバージョン番号になるんでしょうか。

プロジェクトのVagrant設定

Vagrantの設定も含めてプロジェクト単位でgitのバージョン管理下に入れるため、以後はプロジェクトディレクトリ内で行います。

プロジェクト名は "delivery" としました。

$ cd ~/Documents/Projects/delivery
$ mkdir vagrant
$ cd vagrant
$ vagrant init centos64_i386
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 init コマンドを実行した場所にVagrantfileが作成されました。

ひとまず準備できたので起動してみます。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos64_i386'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1430453933460_60304
==> 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: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/k_horii/Documents/Projects/delivery/vagrant

以前はPageant(PuTTY authentication agent)を起動していると怒られたんですが、改善されたみたいですね。これは嬉しい。

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ pwd
/home/vagrant

無事に入れました。

[vagrant@localhost ~]$ sudo vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain

以前はPuTTY経由でないとできなかったファイルの編集も、できるようになってます。

Vagrantのプライベートネットワークを利用する

プライベートネットワークを利用して、ゲストOSからホストOSが所属するLANにアクセスできるようにします。

Vagranfileの内容を確認してみます。

# -*- 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://atlas.hashicorp.com/search.
  config.vm.box = "centos64_i386"

  # 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.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # 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.10"

  # 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"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end

めっちゃ英語 (((;゚Д゚))) ですが、変えるのはここだけ。

# 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.10"

プライベートネットワークを有効、ゲストOSのIPアドレスを "192.168.33.10" とし、vagrant reload で再起動します。

$ 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: Adapter 2: hostonly
==> 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: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/k_horii/Documents/Projects/delivery/vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

前回の起動時は Adapter 1: nat だけだったのが、Adapter 2: hostonly が追加されています。

ゲストOSに入ってネットワーク設定を確認してみます。

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ ifconfig
eth0      Link encap:Ethernet  HWaddr **:**:**:**:**:**
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe5a:fb02/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:774 errors:0 dropped:0 overruns:0 frame:0
          TX packets:579 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:81387 (79.4 KiB)  TX bytes:69757 (68.1 KiB)

eth1      Link encap:Ethernet  HWaddr **:**:**:**:**:**
          inet addr:192.168.33.10  Bcast:192.168.33.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe87:e4a1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:326 (326.0 b)  TX bytes:552 (552.0 b)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

eth1 として 192.168.33.10 が追加されています。

ホストOSからゲストOS (192.168.33.10)にpingしてみます。

$ ping -n 3 192.168.33.10

192.168.33.10 に ping を送信しています 32 バイトのデータ:
192.168.33.10 からの応答: バイト数 =32 時間 <1ms TTL=64
192.168.33.10 からの応答: バイト数 =32 時間 <1ms TTL=64
192.168.33.10 からの応答: バイト数 =32 時間 <1ms TTL=64

192.168.33.10 の ping 統計:
    パケット数: 送信 = 3、受信 = 3、損失 = 0 (0% の損失)、
ラウンド トリップの概算時間 (ミリ秒):
    最小 = 0ms、最大 = 0ms、平均 = 0ms

ちゃんと届いています。

ゲストOS (192.168.33.10)からホストOSが所属するLAN内にある開発サーバ (192.168.1.100)にpingしてみます。

[vagrant@localhost ~]$ ping -c 3 192.168.1.100
PING 192.168.1.100 (192.168.1.100) 56(84) bytes of data.
64 bytes from 192.168.1.100: icmp_seq=1 ttl=63 time=4.11 ms
64 bytes from 192.168.1.100: icmp_seq=2 ttl=63 time=1.03 ms
64 bytes from 192.168.1.100: icmp_seq=3 ttl=63 time=0.969 ms

--- 192.168.1.100 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.969/2.039/4.111/1.465 ms

こちらも届きました。

念のため、LAN内にある開発サーバ (192.168.1.100)からゲストOS (192.168.33.10)にpingしてみます。

[develop@centos6 ~]$ ping -c 3 192.168.33.10
PING 192.168.33.10 (192.168.33.10) 56(84) bytes of data.

--- 192.168.33.10 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 12000ms

プライベートネットワークなので届きません。

参考にした記事

上の記事では更に、ゲストOS同士でのホストOSでのポートフォワーディングによって、ゲストOSに接続させる方法についても書かれています。

他にもQiitaの vagarnt タグを見ると、現在1200件以上の投稿があります。

共有フォルダのマウントで引っ掛かったり、CentOSだとカーネルを更新したらVirtualBox GuestAdditionsが壊れたとか、そういった情報が散見されます。

いくつか気になる記事をメモしておきます。

後はAnsibleを覚えて、コマンドコピペ作業からちゃんと卒業したいです。

(Fabricは…今のところバックアップ作業や開発サーバへのgitリポジトリ作成などに使ってますが、再利用可能なコマンドとしてまとめる程の作業量が発生しないというか…)

Vagrant + シェルスクリプトでPHP開発環境をプロビジョニングしてみたメモ

私はシェルスクリプト力が皆無なので、タイトルはほぼ釣りです。(でも他に表現のしようがないし…)

AnsibleやFabricといった自動化ツールとの連携を検討していたんですが、ひとまずVagrant単体でできるプロビジョニングを試してみたメモです。

新原さんのスライド もう XAMPP / MAMP はいらない!?Vagrant で作る PHP 開発環境 24ページ以降の内容を手がかりに、Vagrant Documentation も参照しつつ進めます。

Windows7にVirtualBoxとVagrantをインストールしたメモ に引き続き、実行した環境はWindowsです。

プロジェクトの準備

まずはプロジェクト用ディレクトリを作成して、gitで管理します。

以下、Windows8 + NYAOSのシェルにて。 (コマンドは MinGW/MSYSNT版UNIX-like tools へのエイリアスです…自分でも把握できてない)

$ mkdir test-vagrant
$ cd test-vagrant
$ git init
Reinitialized existing Git repository in c:/Users/k_horii/Projects/test-vagrant/.git/

ソース格納用ディレクトリ(source)と、Vagrant設定用ディレクトリ(vagrant)を作成し、 すでに登録済の VagrantBox "centos64_64" を使ってVMの作成準備を行います。

$ mkdir source
$ mkdir vagrant
$ cd vagrant
$ vagrant init centos64_64
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.

カレントディレクトリに Vagrantfile が作成されました。

Vagrantfileで共有ディレクトリを設定する

config.vm.synced_folder はドキュメント (Synced Folders / Basic Usage) にもあまり詳しくは書かれていませんが、以下のようなサンプルと解説があります。

Vagrant.configure("2") do |config|
  # other config here

  config.vm.synced_folder "src/", "/srv/website"
end

The first parameter is a path to a directory on the host machine. If the path is relative, it is relative to the project root.

第1引数はホストPCのパスで、相対パスで記述した場合はプロジェクトルート(Vagrantfileを作成したディレクトリ)からのパスになるようです。

The second parameter must be an absolute path of where to share the folder within the guest machine. This folder will be created (recursively, if it must) if it doesn't exist.

第2引数はVMのどのパスと共有させるかを絶対パスで指定、存在しない場合は再帰的に作成してくれるようです。

スライドの30ページの内容を参考に、以下のような設定としました。

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "centos64_64"
  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.synced_folder "../source", "/home/vagrant/source", :create => true, :owner => 'vagrant', :group => 'vagrant', :extra => 'dmode=777,fmode=666'

end

プロビジョニングは後回しにして、まずはこの設定でVMを作成、起動してみます。事前にPageantを停止しておくことを忘れずに。

※ちなみに config.vm.synced_folder の第2引数に "/home/vagrant" などと書くと、容赦なくホームディレクトリがすっからかんにされるので注意してください。(やってしまいました)

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /home/vagrant

PuTTYで vagrant@127.0.0.1:2222 に接続して、共有ディレクトリが作成されているか確認してみます。

[vagrant@localhost ~]$ ls -lsap
total 24
4 drwx------  4 vagrant vagrant 4096 Sep  2 10:02 ./
4 drwxr-xr-x. 4 root    root    4096 Apr 27 10:05 ../
4 -rw-r--r--  1 vagrant vagrant   18 Feb 21  2013 .bash_logout
4 -rw-r--r--  1 vagrant vagrant  176 Feb 21  2013 .bash_profile
4 -rw-r--r--  1 vagrant vagrant  124 Feb 21  2013 .bashrc
0 drwxrwxrwx  1 vagrant vagrant    0 Sep  2 10:07 source/
4 drwx------  2 vagrant root    4096 Apr 27 10:05 .ssh/

確かにできてます。

ホストPC側でサブディレクトリを作成してみます。

$ mkdir public

VM側への反映を確認してみます。

[vagrant@localhost ~]$ ls -lsap source
total 4
0 drwxrwxrwx 1 vagrant vagrant    0 Sep  2 10:14 ./
4 drwx------ 4 vagrant vagrant 4096 Sep  2 10:02 ../
0 drwxrwxrwx 1 vagrant vagrant    0 Sep  2 10:07 public/

publicディレクトリが vagrant:vagrant, 0777で作成されています。

Vagrantfileで config.vm.synced_folder "../source", "/home/vagrant/source", :create => true, :owner => 'vagrant', :group => 'vagrant', :extra => 'dmode=777,fmode=666' と指定した内容が反映されているようです。

ここで一旦終了、VMを破棄します。

インラインスクリプトでプロビジョニング

スライドの32ページの内容を手がかりに、ドキュメントを読んでみます。

$script = <<SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.provision :shell, :inline => $script
end

I understand if you're not familiar with Ruby, the above may seem very advanced or foreign. But don't fear, what it is doing is quite simple: the script is assigned to a global variable $script.

ご配慮ありがとうございます!

Ruby全然分からないんですが、これなら何とかやれそうです。

まずはこのインラインスクリプトによるプロビジョニングを、以下の設定で試してみます。

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "centos64_64"
  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.synced_folder "../source", "/var/www/test/source", :create => true, :owner => 'vagrant', :group => 'vagrant', :extra => 'dmode=777,fmode=666'
  $script = <<SCRIPT
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
service iptables save
service iptables restart
mkdir -p /var/www/test/source/public
chown vagrant: /var/www/test/source/public
yum -y install httpd
touch /etc/httpd/conf.d/vhosts.conf
chown vagrant: /etc/httpd/conf.d/vhosts.conf
echo "NameVirtualHost *:80" >> /etc/httpd/conf.d/vhosts.conf
echo "<VirtualHost *:80>" >> /etc/httpd/conf.d/vhosts.conf
echo "  DocumentRoot /var/www/test/source/public" >> /etc/httpd/conf.d/vhosts.conf
echo "</VirtualHost>" >> /etc/httpd/conf.d/vhosts.conf
yum -y install install php \
php-cli \
php-common \
php-devel \
php-gd \
php-intl \
php-mbstring \
php-pdo \
php-pear.noarch \
php-xml \
php-mcrypt
echo "date.timezone = Asia/Tokyo" >> /etc/php.ini
echo "<?php phpinfo(); ?>" >> /var/www/test/source/public/index.php
chkconfig httpd on
service httpd start
SCRIPT
  config.vm.provision :shell, :inline => $script

end

VM側の同期ディレクトリはホーム以下だと何かと不都合なので、CentOSApacheで使われる /var/www 以下に /var/www/test/source を設定しました。

iptablesはとりあえず全てのポリシーをACCEPTで初期化、Apacheのドキュメントルートを /var/www/test/source/public とします。

同期させるホストPC側のディレクトリには、まだ何も入ってません。

この状態で一旦 git commit しておき、VMを初期化します。以下、長くなりますが…

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
0K
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /var/www/test/source
[default] Running provisioner: shell...
[default] Running: inline script
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]
iptables: Flushing firewall rules: [  OK  ]
iptables: Setting chains to policy ACCEPT: filter [  OK  ]
iptables: Unloading modules: [  OK  ]
iptables: Applying firewall rules: [  OK  ]
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be installed
--> Processing Dependency: httpd-tools = 2.2.15-29.el6.centos for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed
---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package httpd-tools.x86_64 0:2.2.15-29.el6.centos will be installed
---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch        Version                      Repository    Size
================================================================================
Installing:
 httpd              x86_64      2.2.15-29.el6.centos         updates      821 k
Installing for dependencies:
 apr                x86_64      1.3.9-5.el6_2                base         123 k
 apr-util           x86_64      1.3.9-3.el6_0.1              base          87 k
 apr-util-ldap      x86_64      1.3.9-3.el6_0.1              base          15 k
 httpd-tools        x86_64      2.2.15-29.el6.centos         updates       73 k
 mailcap            noarch      2.1.31-2.el6                 base          27 k

Transaction Summary
================================================================================
Install       6 Package(s)

Total download size: 1.1 M
Installed size: 3.6 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           611 kB/s | 1.1 MB     00:01
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : apr-1.3.9-5.el6_2.x86_64                                     1/6
  Installing : apr-util-1.3.9-3.el6_0.1.x86_64                              2/6
  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/6
  Installing : httpd-tools-2.2.15-29.el6.centos.x86_64                      4/6
  Installing : mailcap-2.1.31-2.el6.noarch                                  5/6
  Installing : httpd-2.2.15-29.el6.centos.x86_64                            6/6
  Verifying  : httpd-2.2.15-29.el6.centos.x86_64                            1/6
  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         2/6
  Verifying  : httpd-tools-2.2.15-29.el6.centos.x86_64                      3/6
  Verifying  : apr-1.3.9-5.el6_2.x86_64                                     4/6
  Verifying  : mailcap-2.1.31-2.el6.noarch                                  5/6
  Verifying  : apr-util-1.3.9-3.el6_0.1.x86_64                              6/6

Installed:
  httpd.x86_64 0:2.2.15-29.el6.centos

Dependency Installed:
  apr.x86_64 0:1.3.9-5.el6_2
  apr-util.x86_64 0:1.3.9-3.el6_0.1
  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
  httpd-tools.x86_64 0:2.2.15-29.el6.centos
  mailcap.noarch 0:2.1.31-2.el6

Complete!
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
Setting up Install Process
No package install available.
No package php-mcrypt available.
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-cli.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-common.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-devel.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: automake for package: php-devel-5.3.3-23.el6_4.x86_64
--> Processing Dependency: autoconf for package: php-devel-5.3.3-23.el6_4.x86_64
---> Package php-gd.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libpng12.so.0(PNG12_0)(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libpng12.so.0()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libjpeg.so.62()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libfreetype.so.6()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libXpm.so.4()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
---> Package php-intl.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libicuuc.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicuio.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicui18n.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicudata.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
---> Package php-mbstring.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-pdo.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-pear.noarch 1:1.9.4-4.el6 will be installed
---> Package php-xml.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Running transaction check
---> Package autoconf.noarch 0:2.63-5.1.el6 will be installed
---> Package automake.noarch 0:1.11.1-4.el6 will be installed
---> Package freetype.x86_64 0:2.3.11-14.el6_3.1 will be installed
---> Package libX11.x86_64 0:1.5.0-4.el6 will be installed
--> Processing Dependency: libX11-common = 1.5.0-4.el6 for package: libX11-1.5.0-4.el6.x86_64
--> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.5.0-4.el6.x86_64
---> Package libXpm.x86_64 0:3.5.10-2.el6 will be installed
---> Package libicu.x86_64 0:4.2.1-9.1.el6_2 will be installed
---> Package libjpeg-turbo.x86_64 0:1.2.1-1.el6 will be installed
---> Package libpng.x86_64 2:1.2.49-1.el6_2 will be installed
---> Package libxslt.x86_64 0:1.1.26-2.el6_3.1 will be installed
--> Running transaction check
---> Package libX11-common.noarch 0:1.5.0-4.el6 will be installed
---> Package libxcb.x86_64 0:1.8.1-1.el6 will be installed
--> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.8.1-1.el6.x86_64
--> Running transaction check
---> Package libXau.x86_64 0:1.0.6-4.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch         Version                   Repository     Size
================================================================================
Installing:
 php                 x86_64       5.3.3-23.el6_4            updates       1.1 M
 php-cli             x86_64       5.3.3-23.el6_4            updates       2.2 M
 php-common          x86_64       5.3.3-23.el6_4            updates       524 k
 php-devel           x86_64       5.3.3-23.el6_4            updates       507 k
 php-gd              x86_64       5.3.3-23.el6_4            updates       106 k
 php-intl            x86_64       5.3.3-23.el6_4            updates        70 k
 php-mbstring        x86_64       5.3.3-23.el6_4            updates       455 k
 php-pdo             x86_64       5.3.3-23.el6_4            updates        75 k
 php-pear            noarch       1:1.9.4-4.el6             base          393 k
 php-xml             x86_64       5.3.3-23.el6_4            updates       103 k
Installing for dependencies:
 autoconf            noarch       2.63-5.1.el6              base          781 k
 automake            noarch       1.11.1-4.el6              base          550 k
 freetype            x86_64       2.3.11-14.el6_3.1         updates       359 k
 libX11              x86_64       1.5.0-4.el6               base          584 k
 libX11-common       noarch       1.5.0-4.el6               base          192 k
 libXau              x86_64       1.0.6-4.el6               base           24 k
 libXpm              x86_64       3.5.10-2.el6              base           51 k
 libicu              x86_64       4.2.1-9.1.el6_2           base          4.9 M
 libjpeg-turbo       x86_64       1.2.1-1.el6               base          174 k
 libpng              x86_64       2:1.2.49-1.el6_2          base          182 k
 libxcb              x86_64       1.8.1-1.el6               base          110 k
 libxslt             x86_64       1.1.26-2.el6_3.1          base          452 k

Transaction Summary
================================================================================
Install      22 Package(s)

Total download size: 14 M
Installed size: 51 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           522 kB/s |  14 MB     00:27
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : php-common-5.3.3-23.el6_4.x86_64                            1/22
  Installing : php-cli-5.3.3-23.el6_4.x86_64                               2/22
  Installing : autoconf-2.63-5.1.el6.noarch                                3/22
  Installing : automake-1.11.1-4.el6.noarch                                4/22
  Installing : php-5.3.3-23.el6_4.x86_64                                   5/22
  Installing : libxslt-1.1.26-2.el6_3.1.x86_64                             6/22
  Installing : libicu-4.2.1-9.1.el6_2.x86_64                               7/22
  Installing : libXau-1.0.6-4.el6.x86_64                                   8/22
  Installing : libxcb-1.8.1-1.el6.x86_64                                   9/22
  Installing : 2:libpng-1.2.49-1.el6_2.x86_64                             10/22
  Installing : libX11-common-1.5.0-4.el6.noarch                           11/22
  Installing : libX11-1.5.0-4.el6.x86_64                                  12/22
  Installing : libXpm-3.5.10-2.el6.x86_64                                 13/22
  Installing : freetype-2.3.11-14.el6_3.1.x86_64                          14/22
  Installing : libjpeg-turbo-1.2.1-1.el6.x86_64                           15/22
  Installing : php-gd-5.3.3-23.el6_4.x86_64                               16/22
  Installing : php-intl-5.3.3-23.el6_4.x86_64                             17/22
  Installing : php-xml-5.3.3-23.el6_4.x86_64                              18/22
  Installing : php-devel-5.3.3-23.el6_4.x86_64                            19/22
  Installing : 1:php-pear-1.9.4-4.el6.noarch                              20/22
  Installing : php-mbstring-5.3.3-23.el6_4.x86_64                         21/22
  Installing : php-pdo-5.3.3-23.el6_4.x86_64                              22/22
  Verifying  : libjpeg-turbo-1.2.1-1.el6.x86_64                            1/22
  Verifying  : php-mbstring-5.3.3-23.el6_4.x86_64                          2/22
  Verifying  : libXpm-3.5.10-2.el6.x86_64                                  3/22
  Verifying  : php-5.3.3-23.el6_4.x86_64                                   4/22
  Verifying  : php-pdo-5.3.3-23.el6_4.x86_64                               5/22
  Verifying  : php-devel-5.3.3-23.el6_4.x86_64                             6/22
  Verifying  : automake-1.11.1-4.el6.noarch                                7/22
  Verifying  : 1:php-pear-1.9.4-4.el6.noarch                               8/22
  Verifying  : php-xml-5.3.3-23.el6_4.x86_64                               9/22
  Verifying  : php-common-5.3.3-23.el6_4.x86_64                           10/22
  Verifying  : freetype-2.3.11-14.el6_3.1.x86_64                          11/22
  Verifying  : autoconf-2.63-5.1.el6.noarch                               12/22
  Verifying  : php-gd-5.3.3-23.el6_4.x86_64                               13/22
  Verifying  : php-cli-5.3.3-23.el6_4.x86_64                              14/22
  Verifying  : libX11-common-1.5.0-4.el6.noarch                           15/22
  Verifying  : libxcb-1.8.1-1.el6.x86_64                                  16/22
  Verifying  : 2:libpng-1.2.49-1.el6_2.x86_64                             17/22
  Verifying  : libXau-1.0.6-4.el6.x86_64                                  18/22
  Verifying  : libicu-4.2.1-9.1.el6_2.x86_64                              19/22
  Verifying  : libX11-1.5.0-4.el6.x86_64                                  20/22
  Verifying  : libxslt-1.1.26-2.el6_3.1.x86_64                            21/22
  Verifying  : php-intl-5.3.3-23.el6_4.x86_64                             22/22

Installed:
  php.x86_64 0:5.3.3-23.el6_4              php-cli.x86_64 0:5.3.3-23.el6_4
  php-common.x86_64 0:5.3.3-23.el6_4       php-devel.x86_64 0:5.3.3-23.el6_4
  php-gd.x86_64 0:5.3.3-23.el6_4           php-intl.x86_64 0:5.3.3-23.el6_4
  php-mbstring.x86_64 0:5.3.3-23.el6_4     php-pdo.x86_64 0:5.3.3-23.el6_4
  php-pear.noarch 1:1.9.4-4.el6            php-xml.x86_64 0:5.3.3-23.el6_4

Dependency Installed:
  autoconf.noarch 0:2.63-5.1.el6          automake.noarch 0:1.11.1-4.el6
  freetype.x86_64 0:2.3.11-14.el6_3.1     libX11.x86_64 0:1.5.0-4.el6
  libX11-common.noarch 0:1.5.0-4.el6      libXau.x86_64 0:1.0.6-4.el6
  libXpm.x86_64 0:3.5.10-2.el6            libicu.x86_64 0:4.2.1-9.1.el6_2
  libjpeg-turbo.x86_64 0:1.2.1-1.el6      libpng.x86_64 2:1.2.49-1.el6_2
  libxcb.x86_64 0:1.8.1-1.el6             libxslt.x86_64 0:1.1.26-2.el6_3.1

Complete!
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[  OK  ]

無事に起動したでしょうか? httpdの警告は httpd.conf で ServerName を定義していないためでしょう。

とりあえずブラウザで 192.168.33.10 を開いたところ、phpinfoが表示されました。

ホストPC側の同期ディレクトリの状態を確認してみます。

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       ../source/public/
nothing added to commit but untracked files present (use "git add" to track)

$ ls -lsa ../source/public
   0 d---rwx        0 Sep  4 07:30 .             .
   0 d---rwx        0 Sep  4 07:29 ..            ..
   1 -a--rw-       20 Sep  4 07:30               index.php

ホストPCの方にも source/public ディレクトリと index.php が作成されています。

外部スクリプトでプロビジョニング

ドキュメントには、ホストPCで記述したシェルスクリプトを読み込んで実行する方法も書かれています。

The shell provisioner can also take an option specifying a path to a shell script on the host machine. Vagrant will then upload this script into the guest and execute it. An example:

Vagrant.configure("2") do |config|
  config.vm.provision :shell, :path => "script.sh"
end

Relative paths, such as above, are expanded relative to the location of the root Vagrantfile for your project. Absolute paths can also be used, as well as shortcuts such as ~ (home directory) and .. (parent directory).

なお例のごとく、相対パスで記述した場合はプロジェクトルート(Vagrantfileを作成したディレクトリ)からのパスになるようです。

とりあえず一旦VMを破棄して、今度は同じ内容で外部ファイルに移してVMを作成してみます。

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "centos64_64"
  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.synced_folder "../source", "/var/www/test/source", :create => true, :owner => 'vagrant', :group => 'vagrant', :extra => 'dmode=777,fmode=666'
  config.vm.provision :shell, :path => "provision.sh"

end

provision.sh

#!/bin/sh

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
service iptables save
service iptables restart
mkdir -p /var/www/test/source/public
chown vagrant: /var/www/test/source/public
yum -y install httpd
touch /etc/httpd/conf.d/vhosts.conf
chown vagrant: /etc/httpd/conf.d/vhosts.conf
echo "NameVirtualHost *:80" >> /etc/httpd/conf.d/vhosts.conf
echo "<VirtualHost *:80>" >> /etc/httpd/conf.d/vhosts.conf
echo "  DocumentRoot /var/www/test/source/public" >> /etc/httpd/conf.d/vhosts.conf
echo "</VirtualHost>" >> /etc/httpd/conf.d/vhosts.conf
yum -y install install php \
php-cli \
php-common \
php-devel \
php-gd \
php-intl \
php-mbstring \
php-pdo \
php-pear.noarch \
php-xml \
php-mcrypt
echo "date.timezone = Asia/Tokyo" >> /etc/php.ini
echo "<?php phpinfo(); ?>" >> /var/www/test/source/public/index.php
chkconfig httpd on
service httpd start

VMを作り直します。再作成の前に、ディレクトリ同期でホストPC側に作成されたディレクトリとファイルの削除も忘れずに。

$ vagrant destroy --force
[default] Forcing shutdown of VM...
[default] Destroying VM and associated drives...
$ rm -rf ../source/public
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
0K
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /var/www/test/source
[default] Running provisioner: shell...
[default] Running: C:/Users/k_horii/AppData/Local/Temp/vagrant-shell20130904-540-hsxmzv
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]
iptables: Flushing firewall rules: [  OK  ]
iptables: Setting chains to policy ACCEPT: filter [  OK  ]
iptables: Unloading modules: [  OK  ]
iptables: Applying firewall rules: [  OK  ]
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: ftp.nara.wide.ad.jp
 * extras: ftp.nara.wide.ad.jp
 * updates: centosx4.centos.org
http://centosx4.centos.org/centos/6.4/updates/x86_64/repodata/2131be604d1cf4779786f3f56e061bd1617a3aa16d3157be5b7511cff232b66a-primary.sqlite.bz2: [Errno 12] Timeout on http://centosx4.centos.org/centos/6.4/updates/x86_64/repodata/2131be604d1cf4779786f3f56e061bd1617a3aa16d3157be5b7511cff232b66a-primary.sqlite.bz2: (28, 'Operation too slow. Less than 1 bytes/sec transfered the last 30 seconds')
Trying other mirror.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be installed
--> Processing Dependency: httpd-tools = 2.2.15-29.el6.centos for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed
---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package httpd-tools.x86_64 0:2.2.15-29.el6.centos will be installed
---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch        Version                      Repository    Size
================================================================================
Installing:
 httpd              x86_64      2.2.15-29.el6.centos         updates      821 k
Installing for dependencies:
 apr                x86_64      1.3.9-5.el6_2                base         123 k
 apr-util           x86_64      1.3.9-3.el6_0.1              base          87 k
 apr-util-ldap      x86_64      1.3.9-3.el6_0.1              base          15 k
 httpd-tools        x86_64      2.2.15-29.el6.centos         updates       73 k
 mailcap            noarch      2.1.31-2.el6                 base          27 k

Transaction Summary
================================================================================
Install       6 Package(s)

Total download size: 1.1 M
Installed size: 3.6 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           131 kB/s | 1.1 MB     00:08
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : apr-1.3.9-5.el6_2.x86_64                                     1/6
  Installing : apr-util-1.3.9-3.el6_0.1.x86_64                              2/6
  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/6
  Installing : httpd-tools-2.2.15-29.el6.centos.x86_64                      4/6
  Installing : mailcap-2.1.31-2.el6.noarch                                  5/6
  Installing : httpd-2.2.15-29.el6.centos.x86_64                            6/6
  Verifying  : httpd-2.2.15-29.el6.centos.x86_64                            1/6
  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         2/6
  Verifying  : httpd-tools-2.2.15-29.el6.centos.x86_64                      3/6
  Verifying  : apr-1.3.9-5.el6_2.x86_64                                     4/6
  Verifying  : mailcap-2.1.31-2.el6.noarch                                  5/6
  Verifying  : apr-util-1.3.9-3.el6_0.1.x86_64                              6/6

Installed:
  httpd.x86_64 0:2.2.15-29.el6.centos

Dependency Installed:
  apr.x86_64 0:1.3.9-5.el6_2
  apr-util.x86_64 0:1.3.9-3.el6_0.1
  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
  httpd-tools.x86_64 0:2.2.15-29.el6.centos
  mailcap.noarch 0:2.1.31-2.el6

Complete!
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * extras: ftp.nara.wide.ad.jp
 * updates: centosx4.centos.org
Setting up Install Process
No package install available.
No package php-mcrypt available.
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-cli.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-common.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-devel.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: automake for package: php-devel-5.3.3-23.el6_4.x86_64
--> Processing Dependency: autoconf for package: php-devel-5.3.3-23.el6_4.x86_64
---> Package php-gd.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libpng12.so.0(PNG12_0)(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libpng12.so.0()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libjpeg.so.62()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libfreetype.so.6()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libXpm.so.4()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
---> Package php-intl.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libicuuc.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicuio.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicui18n.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicudata.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
---> Package php-mbstring.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-pdo.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-pear.noarch 1:1.9.4-4.el6 will be installed
---> Package php-xml.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Running transaction check
---> Package autoconf.noarch 0:2.63-5.1.el6 will be installed
---> Package automake.noarch 0:1.11.1-4.el6 will be installed
---> Package freetype.x86_64 0:2.3.11-14.el6_3.1 will be installed
---> Package libX11.x86_64 0:1.5.0-4.el6 will be installed
--> Processing Dependency: libX11-common = 1.5.0-4.el6 for package: libX11-1.5.0-4.el6.x86_64
--> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.5.0-4.el6.x86_64
---> Package libXpm.x86_64 0:3.5.10-2.el6 will be installed
---> Package libicu.x86_64 0:4.2.1-9.1.el6_2 will be installed
---> Package libjpeg-turbo.x86_64 0:1.2.1-1.el6 will be installed
---> Package libpng.x86_64 2:1.2.49-1.el6_2 will be installed
---> Package libxslt.x86_64 0:1.1.26-2.el6_3.1 will be installed
--> Running transaction check
---> Package libX11-common.noarch 0:1.5.0-4.el6 will be installed
---> Package libxcb.x86_64 0:1.8.1-1.el6 will be installed
--> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.8.1-1.el6.x86_64
--> Running transaction check
---> Package libXau.x86_64 0:1.0.6-4.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch         Version                   Repository     Size
================================================================================
Installing:
 php                 x86_64       5.3.3-23.el6_4            updates       1.1 M
 php-cli             x86_64       5.3.3-23.el6_4            updates       2.2 M
 php-common          x86_64       5.3.3-23.el6_4            updates       524 k
 php-devel           x86_64       5.3.3-23.el6_4            updates       507 k
 php-gd              x86_64       5.3.3-23.el6_4            updates       106 k
 php-intl            x86_64       5.3.3-23.el6_4            updates        70 k
 php-mbstring        x86_64       5.3.3-23.el6_4            updates       455 k
 php-pdo             x86_64       5.3.3-23.el6_4            updates        75 k
 php-pear            noarch       1:1.9.4-4.el6             base          393 k
 php-xml             x86_64       5.3.3-23.el6_4            updates       103 k
Installing for dependencies:
 autoconf            noarch       2.63-5.1.el6              base          781 k
 automake            noarch       1.11.1-4.el6              base          550 k
 freetype            x86_64       2.3.11-14.el6_3.1         updates       359 k
 libX11              x86_64       1.5.0-4.el6               base          584 k
 libX11-common       noarch       1.5.0-4.el6               base          192 k
 libXau              x86_64       1.0.6-4.el6               base           24 k
 libXpm              x86_64       3.5.10-2.el6              base           51 k
 libicu              x86_64       4.2.1-9.1.el6_2           base          4.9 M
 libjpeg-turbo       x86_64       1.2.1-1.el6               base          174 k
 libpng              x86_64       2:1.2.49-1.el6_2          base          182 k
 libxcb              x86_64       1.8.1-1.el6               base          110 k
 libxslt             x86_64       1.1.26-2.el6_3.1          base          452 k

Transaction Summary
================================================================================
Install      22 Package(s)

Total download size: 14 M
Installed size: 51 M
Downloading Packages:
http://centosx4.centos.org/centos/6.4/updates/x86_64/Packages/php-5.3.3-23.el6_4.x86_64.rpm: [Errno 12] Timeout on http://centosx4.centos.org/centos/6.4/updates/x86_64/Packages/php-5.3.3-23.el6_4.x86_64.rpm: (28, 'Operation too slow. Less than 1 bytes/sec transfered the last 30 seconds')
Trying other mirror.
--------------------------------------------------------------------------------
Total                                            53 kB/s |  14 MB     04:26
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : php-common-5.3.3-23.el6_4.x86_64                            1/22
  Installing : php-cli-5.3.3-23.el6_4.x86_64                               2/22
  Installing : autoconf-2.63-5.1.el6.noarch                                3/22
  Installing : automake-1.11.1-4.el6.noarch                                4/22
  Installing : php-5.3.3-23.el6_4.x86_64                                   5/22
  Installing : libxslt-1.1.26-2.el6_3.1.x86_64                             6/22
  Installing : libicu-4.2.1-9.1.el6_2.x86_64                               7/22
  Installing : libXau-1.0.6-4.el6.x86_64                                   8/22
  Installing : libxcb-1.8.1-1.el6.x86_64                                   9/22
  Installing : 2:libpng-1.2.49-1.el6_2.x86_64                             10/22
  Installing : libX11-common-1.5.0-4.el6.noarch                           11/22
  Installing : libX11-1.5.0-4.el6.x86_64                                  12/22
  Installing : libXpm-3.5.10-2.el6.x86_64                                 13/22
  Installing : freetype-2.3.11-14.el6_3.1.x86_64                          14/22
  Installing : libjpeg-turbo-1.2.1-1.el6.x86_64                           15/22
  Installing : php-gd-5.3.3-23.el6_4.x86_64                               16/22
  Installing : php-intl-5.3.3-23.el6_4.x86_64                             17/22
  Installing : php-xml-5.3.3-23.el6_4.x86_64                              18/22
  Installing : php-devel-5.3.3-23.el6_4.x86_64                            19/22
  Installing : 1:php-pear-1.9.4-4.el6.noarch                              20/22
  Installing : php-mbstring-5.3.3-23.el6_4.x86_64                         21/22
  Installing : php-pdo-5.3.3-23.el6_4.x86_64                              22/22
  Verifying  : libjpeg-turbo-1.2.1-1.el6.x86_64                            1/22
  Verifying  : php-mbstring-5.3.3-23.el6_4.x86_64                          2/22
  Verifying  : libXpm-3.5.10-2.el6.x86_64                                  3/22
  Verifying  : php-5.3.3-23.el6_4.x86_64                                   4/22
  Verifying  : php-pdo-5.3.3-23.el6_4.x86_64                               5/22
  Verifying  : php-devel-5.3.3-23.el6_4.x86_64                             6/22
  Verifying  : automake-1.11.1-4.el6.noarch                                7/22
  Verifying  : 1:php-pear-1.9.4-4.el6.noarch                               8/22
  Verifying  : php-xml-5.3.3-23.el6_4.x86_64                               9/22
  Verifying  : php-common-5.3.3-23.el6_4.x86_64                           10/22
  Verifying  : freetype-2.3.11-14.el6_3.1.x86_64                          11/22
  Verifying  : autoconf-2.63-5.1.el6.noarch                               12/22
  Verifying  : php-gd-5.3.3-23.el6_4.x86_64                               13/22
  Verifying  : php-cli-5.3.3-23.el6_4.x86_64                              14/22
  Verifying  : libX11-common-1.5.0-4.el6.noarch                           15/22
  Verifying  : libxcb-1.8.1-1.el6.x86_64                                  16/22
  Verifying  : 2:libpng-1.2.49-1.el6_2.x86_64                             17/22
  Verifying  : libXau-1.0.6-4.el6.x86_64                                  18/22
  Verifying  : libicu-4.2.1-9.1.el6_2.x86_64                              19/22
  Verifying  : libX11-1.5.0-4.el6.x86_64                                  20/22
  Verifying  : libxslt-1.1.26-2.el6_3.1.x86_64                            21/22
  Verifying  : php-intl-5.3.3-23.el6_4.x86_64                             22/22

Installed:
  php.x86_64 0:5.3.3-23.el6_4              php-cli.x86_64 0:5.3.3-23.el6_4
  php-common.x86_64 0:5.3.3-23.el6_4       php-devel.x86_64 0:5.3.3-23.el6_4
  php-gd.x86_64 0:5.3.3-23.el6_4           php-intl.x86_64 0:5.3.3-23.el6_4
  php-mbstring.x86_64 0:5.3.3-23.el6_4     php-pdo.x86_64 0:5.3.3-23.el6_4
  php-pear.noarch 1:1.9.4-4.el6            php-xml.x86_64 0:5.3.3-23.el6_4

Dependency Installed:
  autoconf.noarch 0:2.63-5.1.el6          automake.noarch 0:1.11.1-4.el6
  freetype.x86_64 0:2.3.11-14.el6_3.1     libX11.x86_64 0:1.5.0-4.el6
  libX11-common.noarch 0:1.5.0-4.el6      libXau.x86_64 0:1.0.6-4.el6
  libXpm.x86_64 0:3.5.10-2.el6            libicu.x86_64 0:4.2.1-9.1.el6_2
  libjpeg-turbo.x86_64 0:1.2.1-1.el6      libpng.x86_64 2:1.2.49-1.el6_2
  libxcb.x86_64 0:1.8.1-1.el6             libxslt.x86_64 0:1.1.26-2.el6_3.1

Complete!
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[  OK  ]

途中でyumが centosx4.centos.org を見に行ってタイムアウトしちゃってますが、なんとか完了しました。

ブラウザで 192.168.33.10 を開くとphpinfoが表示されます。

この調子でComposerのインストールまでやってみようとしたのですが、このVMにはgitが入ってないことが分かりました。

Composerに登録されているパッケージの中には、gitがないとインストールできないものが多いので、 それらもプロビジョニングでインストールしておく必要がありますね。

いずれにせよ PHPer的には 5.3.3 とかあり得ないし(composerにも怒られてしまいます…)、 yumリポジトリの追加も必要でしょうから、今回はここまでにしておきます。

Vagrantのプロビジョニングはいつ呼ばれるのか

プロビジョニングがどのタイミングで呼ばれるのかは、ドキュメント (Synced Folders / Basic Usage) に記載があります。

Provisioners are run in three cases: vagrant up, vagrant reload, and vagrant provision.

ここで指定したスクリプトは、vagrant up vagrant reload vagrant provision 実行時に呼ばれるようです。

ということは、このままだと vagrant up するたびにスクリプトが実行されて、 すでに存在するディレクトリを作成しようとしたり、ファイルに同じ内容が追記されたり、 色々と不都合が起きてしまいますね…。

A --no-provision flag can be passed to up and reload if you don't want to run provisioners.

プロビジョンを実行したくない場合は、vagrant up --no-provisionvagrant reload --no-provision とすればいいようですが、いわゆる冪等性に配慮したプロビジョニング手段を講じた方が良いのかな。

スクリプト内でtestコマンド([コマンド)を使って、ファイルが重複して生成されるのを避けたり、 ある処理を実行したことの目印になるファイルを出力して、2回目以降は処理させないという方法も考えられそうです。

コマンドやパッケージのインストールやユーザの作成前に存在確認など、その辺の手法はFabricにも通じるものがありますね。

今後の参考

一応、多少は利用経験のあるFabricとの連携を検討します。

VM作成時にプロビジョニングを実行するのではなくて、作成後にホストPCからFabricでVMに接続してやる方法。

vagrant ssh-config コマンドの戻り値をキャプチャして加工するんですね、なるほど…。

こちらはFabricスクリプトをVagrant側から呼ぶプラグインかな?

それぞれの使用感も参考になります。

Windows7にVirtualBoxとVagrantをインストールしたメモ

開発環境に便利と話題のVirtualBoxとVagrantを、ようやくインストールしてみたメモです。

こちらの記事を参考にしました。

VirtualBoxを入れる

Downloads ? Oracle VM VirtualBox から VirtualBox 4.2.16 for Windows hosts x86/amd64 をダウンロードして実行。

Setup Wizardが起動するので Next Next。なんかOracle製のドライバ類が色々インストールされるけど気にしない。

Vagrantを入れる

Vagrant - Downloads から 最新版 v1.2.7 のページに飛び Vagrant_1.2.7.msi をダウンロードして実行。

Setup Wizardが起動するので Next Next。インストールが終わると「再起動しろ」と言われるので、再起動します。

Vagrant Boxファイルを入れる

A list of base boxes for Vagrant - Vagrantbox.es から、使いたい仮想サーバのイメージファイルを選択して、Vagrantに追加します。

今回は使い慣れた CentOSの最新版っぽい CentOS 6.4 x86_64 Minimal (VirtualBox Guest Additions 4.2.12, Chef 11.4.4, Puppet 3.1.1) を選択。

以下、コマンドはMSYSのコンソール、ホームディレクトリにて実行。

$ vagrant box add centos64_64 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box
Downloading or copying the box...
Successfully added box 'centos64_64' with provider 'virtualbox'!

$ vagrant init centos64_64
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 box add を自宅でやったら回線のせいかとんでもなく時間がかかったので、可能であれば別途入手してローカルから追加した方がいいと思います。 (パス部分はローカルでも大丈夫なので)

vagrant init コマンドを実行した場所に設定ファイルVagrantfileが作成されますので、プロジェクト毎に仮想サーバを変えたい場合やバージョン管理したい場合は対象のディレクトリに移動して実行すればOKです。

Vagrant を起動してみる

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] Forcing shutdown of VM...
[default] Destroying VM and associated drives...
c:/Applications/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-2.6.8/lib/net/ssh/authentication/agent/socket.rb:81:in `neg
otiate!': unknown response from agent: 1, "\x00\x00\x00-\x01\x00\x00\x00(SSH-2.0-Ruby/Net::SSH_2.6.8 i386-mingw32" (Net:
:SSH::Authentication::AgentError)

なんか怒られました。 検索してみたところ、どうもWindowsでPuTTYのAgentを起動しているとこうなるみたいです。

AgentError, unknown response from agent on Windows with 64-bit PuTTY ・ Issue #1455 ・ mitchellh/vagrant

一旦Pageantを終了してもう一度…(普段の開発にはPageantないと不便なんだけどなぁ…)

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- /vagrant

※追記注

vagrant upコマンド実行時に作成される .vagrant/machines/default/virtualbox/id ファイルにはVirtualBox VMのIDが書き込まれていて、このIDがホームディレクトリに作成される .VirtualBox/VirtualBox.xml で参照先の .vbox ファイルに関連付けられています。

VirtualBox単体の利用経験はないのですが、VirtualBox.xml内のコメントを見たところ、このファイルは本来は VBoxManage.exe や VirtualBox Manager GUI を通じて作成されるもので、vagrantコマンドがそれを代行してくれてるみたいです。

※追記その2 vagrant upの途中で固まってVMが起動しない場合

vagrant upコマンド実行後、"Waiting for VM to boot. This can take a few minutes."で固まってしまう場合は、Vagrantfileで以下の設定を有効にして、VirtualBoxGUIモードで起動してみてください。

config.vm.provider :virtualbox do |vb|
  vb.gui = true
end

GUI上に「仮想化支援機構(VT-x/AMD-V)を有効化できません」というダイアログが出てVMが起動していない場合、BIOSで機能が無効にされているかもしれません。

自宅のWindows8 VAIO(SVE14A2AJ)のBIOS初期設定で "Intel(R) Virtualization Technology" がDisabledに設定されており、この問題に当たりました。

仮想サーバに接続してみる

仮想サーバが起動したようなので、接続してみます。

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$

何もすることなく、あっさり入れてしまいました。

[vagrant@localhost ~]$ sudo vi /etc/sysconfig/network
E437: terminal capability "cm" required
Press ENTER or type command to continue"/etc/sysconfig/network"

しかし、何かファイルを編集しようとするとエラーが発生し、画面表示がおかしくなってしまいます。

参考サイト Window 7 でVagrantでCent OS 6.3入れてみた - 僕の車輪の再発明 によればPuTTY推奨とのことなので、PuTTYに変えてみました。

ホスト vagrant@127.0.0.1 ポート 2222

パスワードを聞かれますが、"vagrant"で通りました。

[vagrant@localhost ~]$ sudo vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain

PuTTYの場合は編集もばっちりです。

PuTTYで入ってる時にVMを停止したらどうなるかな。

$ vagrant halt
[default] Attempting graceful shutdown of VM...

[vagrant@localhost ~]$
Broadcast message from vagrant@localhost.localdomain
...
The system is going down for halt NOW!

普通に落とされました(笑)

一度起動すれば普通のサーバと同じなので、ユーザー作成して公開鍵設置してsshdの設定を適宜すれば、Pageantで公開鍵認証も普通にできると思います。

(Vagrantの起動時にPageant下げなきゃいけないのが面倒ですが…)

saharaプラグインを入れてみる

Vagrantのサンドボックスモードでコミットとロールバックを実現するというsaharaプラグインを入れてみます。

$ vagrant plugin install sahara
Installing the 'sahara' plugin. This can take a few minutes...
Installed the plugin 'sahara (0.0.15)'!

$ vagrant sandbox on
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

$ vagrant up

PuTTYで入って適当にディレクトリ作成してみます。

[vagrant@localhost ~]$ mkdir .bin
[vagrant@localhost ~]$ ls -lsa
total 32
4 drwx------  4 vagrant vagrant 4096 Aug 30 06:03 .
4 drwxr-xr-x. 4 root    root    4096 Apr 27 10:05 ..
4 -rw-------  1 vagrant vagrant  290 Aug 30 05:44 .bash_history
4 -rw-r--r--  1 vagrant vagrant   18 Feb 21  2013 .bash_logout
4 -rw-r--r--  1 vagrant vagrant  176 Feb 21  2013 .bash_profile
4 -rw-r--r--  1 vagrant vagrant  124 Feb 21  2013 .bashrc
4 drwxrwxr-x  2 vagrant vagrant 4096 Aug 30 06:03 .bin
4 drwx------  2 vagrant root    4096 Apr 27 10:05 .ssh

サンドボックスをコミット(結構時間かかります)

$ vagrant sandbox commit
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

ディレクトリを削除してみます。

[vagrant@localhost ~]$ rm -rf .bin
[vagrant@localhost ~]$ ls -lsa
total 28
4 drwx------  3 vagrant vagrant 4096 Aug 30 06:08 .
4 drwxr-xr-x. 4 root    root    4096 Apr 27 10:05 ..
4 -rw-------  1 vagrant vagrant  290 Aug 30 05:44 .bash_history
4 -rw-r--r--  1 vagrant vagrant   18 Feb 21  2013 .bash_logout
4 -rw-r--r--  1 vagrant vagrant  176 Feb 21  2013 .bash_profile
4 -rw-r--r--  1 vagrant vagrant  124 Feb 21  2013 .bashrc
4 drwx------  2 vagrant root    4096 Apr 27 10:05 .ssh

一度ログアウトして、サンドボックスロールバック

$ vagrant sandbox rollback
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

ログインし直して確認してみる。

[vagrant@localhost ~]$ ls -lsap
total 32
4 drwx------  4 vagrant vagrant 4096 Aug 30 06:03 ./
4 drwxr-xr-x. 4 root    root    4096 Apr 27 10:05 ../
4 -rw-------  1 vagrant vagrant  290 Aug 30 05:44 .bash_history
4 -rw-r--r--  1 vagrant vagrant   18 Feb 21  2013 .bash_logout
4 -rw-r--r--  1 vagrant vagrant  176 Feb 21  2013 .bash_profile
4 -rw-r--r--  1 vagrant vagrant  124 Feb 21  2013 .bashrc
4 drwxrwxr-x  2 vagrant vagrant 4096 Aug 30 06:03 .bin/
4 drwx------  2 vagrant root    4096 Apr 27 10:05 .ssh/

復活してます。よしよし

vagrant-vbox-snapshotプラグインを入れてみる(→削除)

VirtualBoxのスナップショット機能をvagrantコマンドで使えるようにするという、超便利なプラグインを入れてみます。

$ vagrant plugin install vagrant-vbox-snapshot
Installing the 'vagrant-vbox-snapshot' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbox-snapshot (0.0.2)'!
$ vagrant snapshot take centos64_64_1
$ vagrant snapshot list

参考記事の通り、本当にうんともすんとも言わない…Windowsで動かないのかな。それにしてもエラーメッセージひとつ返さないのってどうなの…。

よく分からないので、なかったことにします。

$ vagrant plugin uninstall vagrant-vbox-snapshot
$ vagrant plugin list
sahara (0.0.15)

実際、環境構築の自動化までいくと、そこまでの機能は使わないんじゃないかという気もしますし。

検索しても便利だ便利だという声しか聞こえてこないので、Windowsユーザーのためにここに書き残しておきます。

IPアドレスを変更してみる

IPアドレスがこのままの 127.0.0.1 だと競合して使えないので一旦終了、Vagrantfile設定ファイルを変更してみます。

ホームディレクトリに作成されていた初期状態の Vagrantfile は、以下の内容でした。

Rubyで書かれた設定ファイルのようです。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "centos64_64"

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  # config.vm.box_url = "http://domain.com/path/to/above.box"

  # 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.
  # config.vm.network :forwarded_port, guest: 80, host: 8080

  # 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.10"

  # 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

  # If true, then any SSH connections made will enable agent forwarding.
  # Default value: false
  # config.ssh.forward_agent = true

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider :virtualbox do |vb|
  #   # Don't boot with headless mode
  #   vb.gui = true
  #
  #   # Use VBoxManage to customize the VM. For example to change memory:
  #   vb.customize ["modifyvm", :id, "--memory", "1024"]
  # end
  #
  # View the documentation for the provider you're using for more
  # information on available options.

  # Enable provisioning with Puppet stand alone.  Puppet manifests
  # are contained in a directory path relative to this Vagrantfile.
  # You will need to create the manifests directory and a manifest in
  # the file centos64_64.pp in the manifests_path directory.
  #
  # An example Puppet manifest to provision the message of the day:
  #
  # # group { "puppet":
  # #   ensure => "present",
  # # }
  # #
  # # File { owner => 0, group => 0, mode => 0644 }
  # #
  # # file { '/etc/motd':
  # #   content => "Welcome to your Vagrant-built virtual machine!
  # #               Managed by Puppet.\n"
  # # }
  #
  # config.vm.provision :puppet do |puppet|
  #   puppet.manifests_path = "manifests"
  #   puppet.manifest_file  = "init.pp"
  # end

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  #
  # config.vm.provision :chef_solo do |chef|
  #   chef.cookbooks_path = "../my-recipes/cookbooks"
  #   chef.roles_path = "../my-recipes/roles"
  #   chef.data_bags_path = "../my-recipes/data_bags"
  #   chef.add_recipe "mysql"
  #   chef.add_role "web"
  #
  #   # You may also specify custom JSON attributes:
  #   chef.json = { :mysql_password => "foo" }
  # end

  # Enable provisioning with chef server, specifying the chef server URL,
  # and the path to the validation key (relative to this Vagrantfile).
  #
  # The Opscode Platform uses HTTPS. Substitute your organization for
  # ORGNAME in the URL and validation key.
  #
  # If you have your own Chef Server, use the appropriate URL, which may be
  # HTTP instead of HTTPS depending on your configuration. Also change the
  # validation key to validation.pem.
  #
  # config.vm.provision :chef_client do |chef|
  #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
  #   chef.validation_key_path = "ORGNAME-validator.pem"
  # end
  #
  # If you're using the Opscode platform, your validator client is
  # ORGNAME-validator, replacing ORGNAME with your organization name.
  #
  # If you have your own Chef Server, the default validation client name is
  # chef-validator, unless you changed the configuration.
  #
  #   chef.validation_client_name = "ORGNAME-validator"
end

めっちゃ英語 (((;゚Д゚)))

でも、ここだけに注目すれば何とかなりそうです。

# 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.10"

公式ドキュメント Networking - Public Network - Vagrant Documentation

要は、ホストOSから見たIPアドレスをここで設定できるようです。

Webで検索すると :hostonly:bridged といった、旧バージョンの設定ファイルの書き方がたくさんヒットするので要注意です。

上記設定のコメントを削除して、vagrant reload で再起動します。(Pageantの常駐解除を忘れずに…)

Apache + PHP入れてみる

yumでガシガシ入れます。とりあえずの確認なので最小限のパッケージで、細かい設定はパス。

$ sudo yum -y install httpd
Installed:
  httpd.x86_64 0:2.2.15-29.el6.centos

Dependency Installed:
  apr.x86_64 0:1.3.9-5.el6_2                 apr-util.x86_64 0:1.3.9-3.el6_0.1  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
  httpd-tools.x86_64 0:2.2.15-29.el6.centos  mailcap.noarch 0:2.1.31-2.el6

Complete!

$ sudo yum -y install php 
Installed:
  php.x86_64 0:5.3.3-23.el6_4

Dependency Installed:
  php-cli.x86_64 0:5.3.3-23.el6_4                           php-common.x86_64 0:5.3.3-23.el6_4

Complete!

$ sudo chkconfig httpd on
$ sudo service httpd start

iptablesが初期設定ではポート22のみ通す設定になってるので、80も通すように。

$ sudo vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

$ sudo service iptables restart

phpinfo()を実行するスクリプトを設置します。

$ sudo vi /var/www/html/index.php
<?php phpinfo();

ブラウザで http://192.168.33.10/ を開くと…

http://cdn.mogile.archive.st-hatena.com/v1/image/k-holy/297783820098874484.png

キタワァ・゜゚・:.。..。.:*・゜(n'∀')η゚・

とりあえずここまでで、一旦ロールバックします。

$ vagrant sandbox rollback
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

終了する時はVagrantの停止を忘れずに。

$ vagrant halt
[default] Attempting graceful shutdown of VM...

$ vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

今後のための参考

ホストOSとのディレクトリ共有設定とか、Vagrantfileはプロジェクト毎にバージョン管理せよとか、参考になる内容です。

環境設定の自動化は、よく名前を見るChefはなんか準備がめんどくさそう、SoloとかKnifeとか色々あってよう分からんし、私のような弱小PHPerからするとRubyっていうだけでもう…。 (((;゚Д゚)))

何より、すでにFabric使っててPythonの環境があるので、最近ちょくちょく見るAnsibleの方を試してみようかと…思ったのですが…。

For the central Ansible machine, you will need an environment with Python 2.6 or greater installed. If you are running Python 2.5 on an “Enterprise Linux 5” variant, we’ll show you how to add 2.6 to your distribution, but most platforms already have a new enough Python. (Note that Windows is not supported as the Ansible control machine.)

…。

Windows is not supported as the Ansible control machine.

。・゚・(ノД`)・゚・。

[追記]

参考記事と同じBlogですが、Windows環境でのAnsibleに挑戦されてました。

Cygwinで挑戦されてる方も。

その他参考になりそうな記事

ChocolateyというWindows版apt-getのようなパッケージ管理システムを使ってインストールされた方

VagrantからのSSHの接続情報を確認する方法とか

Vagrantは色々めんどくさいことを自動で(デフォルト設定で)やってくれてるようなので、先へ進む前に公式ドキュメントも目的に合わせて読んだ方が良さそうですね…。