VagrantでCakePHP2の開発環境を整える ITかあさん

ITかあさん

VagrantでCakePHP2の開発環境を整える

今時の開発環境、VagrantでCakePHP2の開発環境を整える!

Vagrant

今時の開発環境はMAMPPでもXAMPPでもない、Vagrantよ!

Vagrantってなにさ

VagrantはVirtual Boxとかの仮想環境で使えるツールです。
VagrantFileにはhost設定など
script.shにはインストールしたい環境を記載。

この2つのファイルとVagrant環境があれば、全く同じ仮想環境がほんの数分でMac/Winに作ることが出来るわけです!

※VagrantはAmazon EC2で使うことも出来るらしいっすね。

微妙なモジュール、PHPやMySQLのちょっとしたバージョンの違い、Win・Mac環境の違いで悩む日々ともオサラバなわけです。

まだまだVagrantFileを書くのは勉強中ですが、かれこれ半年以上はずっとVagrant環境で開発しています。

一度慣れるともうXAMPPやMAMPPに戻ることは出来なくなるくらい便利!

CakePHP2の開発環境をVagrantでつくっちゃうわよ!

ここからはVirtualBox及び、Vagrant/Gitのインストールが完了していることを前提に進めます。

VirtualBox・Vagrantの環境構築はこちらを参考に:CakePHPの仮想環境をVagrantを使って構築してみた

この他基本のvagrantコマンドはこちらにまとめました

全部ターミナルで!黒い画面からぽちぽちするのよ!

私は以下のようなディレクトリで作業します。ディレクトリの場所はお好きなところに。

/Users/UserName/works

cdコマンドで任意の場所に移動し、vagrantファイルをclone

vagrant-lamp-sample

$ cd /Users/UserName/works
$ git clone https://github.com/monsat/vagrant-lamp-sample

するとディレクトリはこんな感じに。

$ pwd
git clone https://github.com/monsat/vagrant-lamp-sample
このままでも問題ないけど、ディレクトリ名を変更します
$ mv vagrant-lamp-sample/ cakephp

git cloneしたvagrant-lamp-sampleのディレクトリの中身はこんな感じ。
(私はcakephpフォルダに変更したけど)

$ cd cakephp/vagrant
$ ls
README.md cake composer.json vagrant

composer.jsonは一旦置いといて、さらにvagrantフォルダの中身を確認します。

ちょっとVagrantファイルをざっくり見てみよう!

Vagrantファイルって、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|
config.vm.box = "centos64_ja"
config.vm.box_url = "https://dl.dropboxusercontent.com/u/3657281/centos64_ja.box"
config.vm.hostname = "YourHostName.net"

config.vm.network :private_network, ip: "192.168.33.40"
#config.vm.synced_folder "../", "/share", :nfs => true
config.vm.synced_folder "../", "/share", \
create: true, owner: 'vagrant', group: 'vagrant', \
mount_options: ['dmode=777,fmode=666']

config.vm.provider :virtualbox do |vb|
vb.name = "money"
vb.gui = true
#vb.customize ["modifyvm", :id, "--memory", 1024]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end

config.vm.provision :shell, :path => "script.sh"
end

赤字部分は適時書き換えて下さい。何も変更しなけば
config.vm.hostnameで記載されているlamp-sampleという名前でVirtualBoxに仮想環境が作られます。もちろん、同じVagrantファイルで、2個同じ仮想環境を自分のマシンに作ることは可能ですが、
同じhostnameの環境は2個は作れずエラーになるので、その辺は適時変更してください。IPアドレスも同様です。
Vagrantで今後たくさん仮想環境を作って行くならば、一応ここの変更だけ心得ておけばよいのかなと思います。私もVagrantファイルはそれくらいしかいじれないです。

CakePHPのプラグイン管理は、Composer使っちゃいなよ!

composer
Composerとは!!それはcomposer.jsonにインストールしたいプラグインの情報をjson形式で書くと、ライブラリの依存関係を管理しつつ、さくっとプラグイン周りをインストしてくれます。わざわざターミナルで一個ずつプラグインをgit cloneする必要がないざます。
もちろん、CakePHPのプラグインだけじゃなく、CakePHP本体だってComposerでインスト出来るんです。(CakePHP公式Book

composer.json

CakePHPにTwitterBootstrapのプラグインをインスト(boost_cake)入れました。必要なければboost_cakeは追記しなくて大丈夫です。

{
"extra": {
"installer-paths": {
"app/Plugin/Migrations": ["cakedc/migrations"],
"app/Plugin/Users": ["cakedc/users"],
"app/Plugin/Search": ["cakedc/search"],
"app/Plugin/Utils": ["cakedc/utils"],
"app/Plugin/Datasources": ["cakephp/datasources"],
"app/Plugin/DebugKit": ["cakephp/debug_kit"],
"app/Plugin/BoostCake": ["slywalker/boost_cake"]//追記
}
},
"require" : {
"php": ">=5.2",
"ext-mcrypt": "*",
"cakedc/migrations": "2.2.*",
"cakedc/users": "2.0.*",
"cakedc/search": "2.2.*",
"cakedc/utils": "1.4.*",
"cakephp/datasources": "2.3.x-dev",
"cakephp/debug_kit": "2.2.*@dev",
"cakephp/cakephp" : "2.4.*",
slywalker/boost_cake": "dev-master"//追記
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
Composerの細かい使い方は次回以降に。

vagrant upでVirturalBoxにVagrant環境一発構築!!

Vagrantfileがあるところでvagrant upを実行
$ ls
Vagrantfile httpd.conf php.ini script.sh xhprof.ini
$ vagrant up
lsコマンド叩いた時に、Vagrantfileが存在するディレクトリで vagrant upコマンドを実行するのよ!

vagrant upでサーバー構築!

The following plugins were installed with a version of Vagrant
that had different versions of underlying components. Because
these component versions were changed (which rarely happens),
the plugins must be uninstalled and reinstalled.

To ensure that all the dependencies are properly updated as well
it is _highly recommended_ to do a `vagrant plugin uninstall`
prior to reinstalling.

This message will not go away until all the plugins below are
either uninstalled or uninstalled then reinstalled.

The plugins below will not be loaded until they’re uninstalled
and reinstalled:

vagrant-vbguest
Bringing machine ‘default’ up with ‘virtualbox’ provider…
[default] Box ‘centos64_ja’ was not found. Fetching box from specified URL for
the provider ‘virtualbox’. Note that if the URL does not have
a box for this provider, you should interrupt Vagrant now and add
the box yourself. Otherwise Vagrant will attempt to download the
full box prior to discovering this error.
Downloading box from URL: https://dl.dropboxusercontent.com/u/3657281/centos64_ja.box
Progress: 2% (Rate: 561k/s, Estimated time remaining: 0:20:31))

vagrant up実行すると、script.shに記載されている yumコマンドでremiリポジトリ追加したり、PHPやMySQLインストしたりと色々ごにょごにょしてくれる!
しばらくするとvirtualboxが立ち上がります。後はブラウザからアクセス

設定したhostにアクセス。デフォルトだとhttp://192.168.33.40/。

CakePHPのディレクトリはどこに?インスト出来たけど どこがドキュメントルートなの?

おお!既にCakeのディレクトリが!

Vagrant up で環境構築したcakephpがまるっと入っているので、このディレクトリを触ればOK。
今回のVagrantfile設定そのままvagrant upしたならlamp-sample以下がドキュメントルート。

URLがIPアドレスなのは嫌

マシンのhostファイルを書き換えてください。

Mac:ターミナルからhostファイルを書き換え

$ sudo vi /etc/hosts
192.168.33.40 example.com

Win:管理者権限で実行してファイルを書き換え

C:/Windows/System32/drivers/etc/hosts

ファイルを編集してください。

このプラグインは使わない、、或はCakePHPは必要ない場合

composer.jsonから必要の無い項目を削除してからvagrant upを実行して下さい。ただディレクトリから物理的に削除しても、vagrant upする時にもう一度入ってしまうので、composer.jsonから必要のない物は削除します。逆に必要なプラグインはどんどんcomposer.jsonに追記すればよいのです!(composer.jsonの書き方は次回に。)

まとめ:CakePHP Vagrant環境構築まとめ

基本的には、VirtualBox、あとgitさえあれば git cloneで公開してあるvagrantファイルダウンロードして、任意でVagrantファイルを書き換えて、vagrant upするだけ。特に書き換えの必要すら無ければ3つのコマンドだけで終了!
vagrantファイルの存在するディレクトリで vagrant upしてあげればCakePHPの仮想環境が誰でも作ることが出来ます!素敵!

$ git clone https://github.com/monsat/vagrant-lamp-sample
$ cd vagrant-lamp-sample/vagrant
$ vagrant up
Vagrantファイル書けるようになりたいなー

今日はここまで。次回以降Vagrantの基本的なコマンドと、Composerの使い方を紹介します。

初夏のJavaScript祭 in サーキュレーションビル ForPro