More Related Content Similar to もう XAMPP / MAMP はいらない!
Vagrant で作る PHP 開発環境 (20) More from Masashi Shinbara (16) もう XAMPP / MAMP はいらない!
Vagrant で作る PHP 開発環境2. @shin1x1
(c) 2013 Masashi Shinbara @shin1x1
Shin x blog
http://www.1x1.jp/blog/
PHP / DevOps / AWS /
Varnish / Fabric / Chef
7. エンジニアSさん
(c) 2013 Masashi Shinbara @shin1x1
1162 <VirtualHost *:80>
1163 ServerName candycane.local
1164 DocumentRoot "/Users/shin/sandbox/demo/candycane/app/webroot"
1165 </VirtualHost>
1166
1167 <VirtualHost *:80>
1168 ServerName demo.local
1169 DocumentRoot "/Users/shin/sandbox/demo/20130601_phpcon"
1170 php_value vld.active 1
1171 </VirtualHost>
1172
1173 <VirtualHost *:80>
1174 ServerName project1.local
1175 DocumentRoot "/Users/shin/project1/app/webroot"
1176 </VirtualHost>
1177
1178 <VirtualHost *:80>
1179 ServerName project2.local
1180 DocumentRoot "/Users/shin/project2/app/webroot"
1181 </VirtualHost>
8. エンジニアSさん
(c) 2013 Masashi Shinbara @shin1x1
1162 <VirtualHost *:80>
1163 ServerName candycane.local
1164 DocumentRoot "/Users/shin/sandbox/demo/candycane/app/webroot"
1165 </VirtualHost>
1166
1167 <VirtualHost *:80>
1168 ServerName demo.local
1169 DocumentRoot "/Users/shin/sandbox/demo/20130601_phpcon"
1170 php_value vld.active 1
1171 </VirtualHost>
1172
1173 <VirtualHost *:80>
1174 ServerName project1.local
1175 DocumentRoot "/Users/shin/project1/app/webroot"
1176 </VirtualHost>
1177
1178 <VirtualHost *:80>
1179 ServerName project2.local
1180 DocumentRoot "/Users/shin/project2/app/webroot"
1181 </VirtualHost>
溢れる
VirtualHost
17. Vagrantの構成
(c) 2013 Masashi Shinbara @shin1x1
•Vagrant
•仮想サーバ [ Provider ]
(VirtualBox, VMWare, AWS等)
•プロビジョニング [ Provisioning ]
(Chef, Puppet, ShellScript等)
21. Vagrant 覚えておきたい用語
(c) 2013 Masashi Shinbara @shin1x1
•Boxファイル
=> 仮想サーバイメージファイル
•Vagrantfile
=> 仮想サーバ構築設定
•vagrantコマンド
=> 全ての操作はこのコマンドで
22. Vagrant 主なコマンド
(c) 2013 Masashi Shinbara @shin1x1
• Vagrantfile作成
$ vagrant init [boxファイル名] [boxファイルURL]
• 仮想サーバ起動
$ vagrant up
• 仮想サーバへ SSH ログイン
$ vagrant ssh
23. vagrant 主なコマンド
(c) 2013 Masashi Shinbara @shin1x1
• 仮想サーバシャットダウン
$ vagrant halt
• 仮想サーバサスペンド
$ vagrant suspend
• 仮想サーバシャットダウン+破棄
$ vagrant destroy
26. (c) 2013 Masashi Shinbara @shin1x1
ファイル構成
$ tree /path/to/project
.
!"" src
# !"" CONTRIBUTING.md
# !"" README.md
# !"" app
# !"" build.properties
# !"" build.xml
# !"" index.php
# !"" lib
# !"" plugins
# $"" vendors
$"" vagrant
!"" .gitignore
!"" .vagrant
$"" Vagrantfile
アプリケーション
Vagrant
Git で管理
27. (c) 2013 Masashi Shinbara @shin1x1
$ cd vagrant
$ cat .gitignore
.vagrant
•.vagrant/ は仮想サーバ固有の
id が保存されているので git で
管理しない
.gitignore
28. (c) 2013 Masashi Shinbara @shin1x1
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos64_ja"
config.vm.network :private_network, ip: "192.168.33.100"
config.vm.synced_folder "../src", "/src", :create =>
true, :owner=> 'vagrant', :group=>'vagrant', :extra =>
'dmode=777,fmode=666'
$ vim Vagrantfile
Vagrantfile
29. (c) 2013 Masashi Shinbara @shin1x1
•ホストPCのディレクトリと
仮想サーバのディレクトリを同期
•開発はホストPCで、
実行は仮想サーバで
•PhpStormやEclipseで開発可能
synced_folder
30. (c) 2013 Masashi Shinbara @shin1x1
config.vm.synced_folder "../src", "/src", :create =>
true, :owner=> 'vagrant', :group=>'vagrant', :extra =>
'dmode=777,fmode=666'
•ホストPC: ../src と 仮想サーバ: /src を共有
•仮想サーバに /src が無ければ自動生成
•/src は vagrant を owner / group に
•/src 以下のパーミッションは ディレクトリが777、ファイルが666
synced_folder - Vagrantfile
31. synced_folder 注意点
(c) 2013 Masashi Shinbara @shin1x1
•仮想サーバでの権限変更が反映されない
•ホストPCでファイルが多いディレクトリは避ける
•仮想サーバでのディスクアクセスは遅い
=> アプリケーションで大量のディスクIOが
発生する場合は synced_folder 以外の
領域を利用する
32. (c) 2013 Masashi Shinbara @shin1x1
provision = <<-"EOS"
sudo yum -y update
rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/ius-
release-1.0-11.ius.centos6.noarch.rpm
yum -y install php54 php54-pdo php54-cli php54-pgsql php54-mbstring php54-mcrypt
php54-common php54-pecl-memcache php54-gd php54-xml php54-devel php54-pecl-
xdebug php54-pecl-apc
yum -y install httpd
echo "date.timezone = Asia/Tokyo" >> /etc/php.ini
echo "<VirtualHost *:80>" >> /etc/httpd/conf/httpd.conf
echo " DocumentRoot /src/app/webroot" >> /etc/httpd/conf/httpd.conf
echo "</VirtualHost>" >> /etc/httpd/conf/httpd.conf
/sbin/service httpd start
/sbin/chkconfig httpd on
EOS
config.vm.provision :shell, :inline => provision
end
Vagrantfile
synced_folder を
document_root に指定
34. (c) 2013 Masashi Shinbara @shin1x1
$ vim app/Config/bootstrap.php
$ open http://192.168.33.100/
ホストPCでソース変更
35. (c) 2013 Masashi Shinbara @shin1x1
<?php
phpinfo();
$ cd ../src/
$ vim app/webroot/info.php
ホストPCでファイル追加
$ open http://192.168.33.100/info.php
36. 本格的な開発環境へ
(c) 2013 Masashi Shinbara @shin1x1
•複雑なプロビジョニングは Chef や Puppet で
•できるだけプロビジョニングで自動化
=> DB構築、初期データ投入、設定ファイル変更
•プロファイリングツールや phpMyAdmin など
の管理ツールも一緒に入れると便利
37. (c) 2013 Masashi Shinbara @shin1x1
$ git clone http://extemple.com/project
$ cd project/vagrant
$ vagrant up
•環境構築はこれだけ!
Vagrantfile が git にあれば
39. (c) 2013 Masashi Shinbara @shin1x1
Configuration Version
Vagrant::Config.run do |config|
....
end
• Version1
Vagrant.configure("2") do |config|
Vagrant.configure("1") do |config|
....
end
• Version2
•blog等を見る時は、設定バージョンに注意。
バージョンが異なると動作しないことも。
40. Sahara Plugin
(c) 2013 Masashi Shinbara @shin1x1
• Vagrant Plugin
https://github.com/jedi4ever/sahara
• 仮想サーバの状態を DB の
ROLLBACK のように戻せる
42. Sahara Plugin
(c) 2013 Masashi Shinbara @shin1x1
$ vagrant sandbox on
• sandbox モード開始 = BEGIN;
$ vagrant sandbox rollback
• 元に戻す = ROLLBACK;
$ vagrant sandbox commit
• 変更確定 = COMMIT;
$ vagrant sandbox off
• sandbox モード終了
46. Summary
(c) 2013 Masashi Shinbara @shin1x1
• プロジェクト毎に独立した環境を構築
=> プロジェクト毎にVagrantfile
• 他人と全く同じ環境を共有
=> Boxファイル、Vagrantfileを共有
• ホストPCとは分離された環境を構築
=> 開発はホストPC、実行は仮想マシン