読者です 読者をやめる 読者になる 読者になる

k-holyのPHPとか諸々メモ

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

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

Vagrant Windows

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

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側から呼ぶプラグインかな?

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