vagrant+chefでcentos6.5+php5.5

秘伝のタレ状態で昔作ったVagrant環境を使いまわしていましたが
vagrantやVirtualBoxのバージョンを上げた所動かなくなったので
あらたに作り直しました。

Mac OSXへの環境構築を行います。

Vagrant
http://www.vagrantup.com/downloads.html
VirtualBox
https://www.virtualbox.org/wiki/Downloads
chef
http://www.getchef.com/downloads/chef-dk/
それぞれdmgでインストールしてください
knife soloを利用したいので

$chef gem install knife-solo

仮想環境にchefの設定を行うプラグインvagrant-omnibusも準備します。
$vagrant plugin install vagrant-omnibus

knife solo を使って初期化します。

$knife solo init centosphp(プロジェクト名等)
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
Setting up Berkshelf...

引き続きphp等のレシピを作成します。

$cd centosphp
$knife cookbook create localedef -o site-cookbooks/
$knife cookbook create apache -o site-cookbooks/
$knife cookbook create php -o site-cookbooks/

それぞれ下記ファイルを修正します。
CentOSの日本語化
/site-cookbooks/localedef/recipes/default.rb

bash 'localedef' do
  code 'sudo localedef -f UTF-8 -i ja_JP ja_JP.UTF-8'
end

Apacheの設定
ドメインでアクセスさせるためにヴァーチャルホストを設定します。
/site-cookbooks/apache/recipes/default.rb

%w[httpd httpd-devel mod_ssl
].each do |pkg|
  package "#{pkg}" do
    action :install
  end
end

service 'httpd' do
  supports :status => true, :restart => true, :reload => true
  action [ :enable, :start ]
end

template "/etc/httpd/conf/httpd.conf" do
  source "httpd-2.2.conf.erb"
end

template "/etc/httpd/conf.d/php.vm.conf" do
  source "php.vm.conf.erb"
end

/site-cookbooks/apache/templates/default/httpd-2.2.conf.erb
設定ファイル一回仮想環境で作成したもの等をコピーして
NameVirtualHost *:80
の部分のコメントを外しました。

php.vmというドメインでアクセスさせます。
ドキュメントルートは後で設定するVagrantfileの中のsyncfolderとの関連で
適時修正してください。
ここでは仮想サーバの/var/www/src/php/public
をドキュメントルートとします。

/site-cookbooks/apache/templates/default/php.vm.conf.erb

<VirtualHost *:80>
    ServerName php.vm
    DocumentRoot /var/www/src/php/public

    <Directory /var/www/src/php/public>
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
   </Directory>
</VirtualHost>

remiからPHPをインストールしたいのでrepoの登録を行ってからPHPのインストールをします。
/site-cookbooks/php/recipes/default.rb

#epel
remote_file "/tmp/#{node['epel']['file_name']}" do
  source "#{node['epel']['remote_uri']}"
  not_if { ::File.exists?("/tmp/#{node['epel']['file_name']}") }
end

package node['epel']['file_name'] do
  action :install
  provider Chef::Provider::Package::Rpm
  source "/tmp/#{node['epel']['file_name']}"
end

#remi
remote_file "/tmp/#{node['remi']['file_name']}" do
  source "#{node['remi']['remote_uri']}"
  not_if { ::File.exists?("/tmp/#{node['remi']['file_name']}") }
end

package node['remi']['file_name'] do
  action :install
  provider Chef::Provider::Package::Rpm
  source "/tmp/#{node['remi']['file_name']}"
end

%w[ php php-pdo php-mbstring php-mysqlnd].each do |pkg|
  package "#{pkg}" do
    action :install
    options "--enablerepo=remi,epel,remi-php55"
  end
end

attributeにremiとepelのrpmのダウンロード先を指定します。
/site-cookbooks/php/attributes/default.rb

default['remi']['file_name']  = "remi-release-6.rpm"
default['remi']['remote_uri'] = "http://rpms.famillecollet.com/enterprise/remi-release-6.rpm"
default['epel']['file_name']  = "epel-release-6-8.noarch.rpm"
default['epel']['remote_uri'] = "http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm"

Vagrantfileの修正

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "chef/centos-6.5"
  config.vm.network "private_network", ip: "192.168.33.104"

  config.vm.synced_folder "./src", "/var/www/src", :create => true, :owner => 'vagrant', :group => 'vagrant', :mount_options => ['dmode=777', 'fmode=775']

  config.omnibus.chef_version = :latest

  config.vm.provision "chef_solo" do |chef|
    chef.cookbooks_path = "./site-cookbooks"
    chef.add_recipe "localedef"
    chef.add_recipe "apache"
    chef.add_recipe "php"
  end
end

php.vmでアクセスできるようにhostsの設定

$ sudo vi /etc/hosts

Vagrantfileで指定したIPを記述します

192.168.33.104 php.vm

後から手順を拾ったので、そのままで動かなかったらすいません

利用バーション
VirtualBox 4.2.36
Vagrant 1.7.2
Chef: 12.3.0

参考にしたページ
http://www.d-wood.com/blog/2014/09/29_6938.html
http://shusatoo.net/infra/chef/vagrant-chef-solo-php-mysql-development-environment/
http://dqn.sakusakutto.jp/2014/05/mac_osx_linux_chef_knife_berkshelf.html
その他
ありがとうございました。

カテゴリー: Tips

Published by

kashioka

有限会社アリウープ 代表取締役社長 PHPユーザ会の発起人の一人。いちばんやさしいPHPの教本(インプレス),PHPハンドブック(ソフトバンククリエイティブ)著者。PHPカンファレンス実行委員 明日の開発カンファレンス開催 認定スクラムマスター。近年、PHPに限らずサーバレス環境でのPythonなども書いたりしている。 http://alleyoop.jp