Mais conteúdo relacionado Semelhante a Vagrant で作る PHP 開発環境 [実践編] (20) Mais de Masashi Shinbara (13) Vagrant で作る PHP 開発環境 [実践編]4. Vagrant 覚えておきたい用語
(c) 2013 Masashi Shinbara @shin1x1
•Boxファイル
=> 仮想マシンイメージファイル
•Vagrantfile
=> 仮想マシン構築設定
•vagrantコマンド
=> 全ての操作はこのコマンドで
5. (c) 2013 Masashi Shinbara @shin1x1
Vagrantについては
http://www.amazon.co.jp/dp/B00F418SQ8
15. (c) 2013 Masashi Shinbara @shin1x1
ファイル構成
$ tree /path/to/project
.
!"" app
!"" composer.json
!"" composer.lock
!"" index.php
!"" lib
!"" plugins
#"" vendors
#"" vagrant
!"" .gitignore
!"" .vagrant
#"" Vagrantfile
アプリケーション
Vagrant
Git で管理
16. (c) 2013 Masashi Shinbara @shin1x1
$ cd vagrant
$ cat .gitignore
.vagrant
.vagrant/ は仮想サーバ固有の id が
保存されているので Git で管理しない
.gitignore
18. (c) 2013 Masashi Shinbara @shin1x1
vagrant up
1. 仮想マシン構築、起動、OS起動
2. PHP, Apache, DB インストール
3. DB 構築, データ投入
4. アプリケーションデプロイ
19. (c) 2013 Masashi Shinbara @shin1x1
vagrant up
プロバイダ
1. 仮想マシン構築、起動、OS起動
2. PHP, Apache, DB インストール
3. DB 構築, データ投入
4. アプリケーションデプロイ
20. 仮想マシン構築、起動
(c) 2013 Masashi Shinbara @shin1x1
config.vm.box = "centos64_ja"
• Box ファイル名
config.vm.network :private_network, ip: "192.168.33.30"
• IPアドレス
config.vm.network :forwarded_port, guest: 80, host: 8080
• ポートフォワード
21. 仮想マシン構築、起動
(c) 2013 Masashi Shinbara @shin1x1
config.vm.provider :virtualbox do |vb|
# GUI or Headless
vb.gui = true
# memory = 1024MB
vb.customize ["modifyvm", :id, "--memory", 1024]
end
• プロバイダ(VirtualBox)
22. (c) 2013 Masashi Shinbara @shin1x1
vagrant up
1. 仮想マシン構築、起動、OS起動
2. PHP, Apache, DB インストール
3. DB 構築, データ投入
4. アプリケーションデプロイ
プロビジョニング
24. プロビジョニング
(c) 2013 Masashi Shinbara @shin1x1
http://docs.vagrantup.com/v2/provisioning/chef_solo.html
Warning: If you're not familiar with Chef andVagrant
already, I recommend starting with the shell provisioner.
However, if you're comfortable withVagrant already,
Vagrant is the best way to learn Chef.
25. シェル
(c) 2013 Masashi Shinbara @shin1x1
config.vm.provision :shell, inline: "echo 'Hello!'"
• シェル(インライン)
config.vm.provision :shell, path: "script.sh"
• シェル(外部ファイル)
config.vm.provision :shell, inline: <<-EOT
/sbin/service iptables stop
/sbin/chkconfig iptables off
EOT
• シェル(インライン複数行)
26. シェル
(c) 2013 Masashi Shinbara @shin1x1
• yumリポジトリ
config.vm.provision :shell, inline: <<-EOT
#
# yum repository
#
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 update
27. シェル
(c) 2013 Masashi Shinbara @shin1x1
• PHP
#
# php
#
yum -y install php54 php54-cli php54-pdo php54-mbstring
php54-mcrypt php54-pecl-memcache php54-mysql php54-devel
php54-common php54-pgsql php54-pear php54-gd php54-xml php54-
pecl-xdebug php54-pecl-apc
touch /var/log/php.log && chmod 666 /var/log/php.log
cp -a /vagrant/php.ini /etc/php.ini
用意した php.ini で上書き
28. シェル
(c) 2013 Masashi Shinbara @shin1x1
•Apache
#
# Apache
#
cp -a /vagrant/httpd.conf /etc/httpd/conf/
/sbin/service httpd start
/sbin/chkconfig httpd on
#
用意した httpd.conf で上書き
29. シェル
(c) 2013 Masashi Shinbara @shin1x1
•PostgreSQL
#
# PostgreSQL
#
yum -y install postgresql-server
/sbin/service postgresql start
/sbin/chkconfig postgresql on
if [ ! -d /var/lib/pgsql/data ]; then
su postgres -c 'initdb --no-locale -D /var/lib/pgsql/
data'
cp -a /vargrant/postgresql.conf /var/lib/pgsql/data/
createuser -Upostgres -d -S -R -A vagrant
createdb -Uvagrant -E UTF-8 -T template0 app
psql -Uvagrant app < /share/pgsql.dmp
fi
初期のみDB構築
30. シェル
(c) 2013 Masashi Shinbara @shin1x1
•Composer
#
# Composer
#
cd /share/
curl -s http://getcomposer.org/installer | php
./composer.phar install
33. シェルでの冪等性は?
(c) 2013 Masashi Shinbara @shin1x1
• if / test で、ある程度可能
• yum install は再インストールして
もエラーにならない
• 開発環境らしい割り切りも
(destroy && up)
34. (c) 2013 Masashi Shinbara @shin1x1
1. 仮想マシン構築、起動、OS起動
2. PHP, Apache, DB インストール
3. DB 構築, データ投入
4. アプリケーションデプロイ
vagrant up
synced_folder
35. (c) 2013 Masashi Shinbara @shin1x1
•ホストPCのディレクトリと
仮想サーバのディレクトリを同期
•開発はホストPCで、
実行は仮想サーバで
•PhpStormやEclipseで開発可能
synced_folder
36. (c) 2013 Masashi Shinbara @shin1x1
config.vm.synced_folder "../", "/share",
create: true, owner: 'vagrant', group: 'vagrant',
mount_options: ['dmode=777,fmode=666']
•ホストPC: ../と 仮想サーバ: /share を共有
•仮想サーバに /share が無ければ自動生成
•/share は vagrant を owner / group に
•/share 以下のパーミッションは ディレクトリが777、ファイルが
666
synced_folder
37. (c) 2013 Masashi Shinbara @shin1x1
synced_folder
$ ls ../
app/ lib/ composer.phar
$ ls /share/
app/ lib/ composer.phar
ホストPC
仮想マシン
38. (c) 2013 Masashi Shinbara @shin1x1
synced_folder
<?php
$foo = Foo::foo();
<?php
$foo = Foo::foo();
ホストPC
仮想マシン
PHPコード変更
自動で同期
39. synced_folder 注意点
(c) 2013 Masashi Shinbara @shin1x1
•仮想サーバでの権限変更が反映されない
•ホストPCでファイルが多いディレクトリは避ける
•仮想サーバでのディスクアクセスは遅い
=> アプリケーションで大量のディスクIOが
発生する場合は synced_folder 以外の
領域を利用する
40. (c) 2013 Masashi Shinbara @shin1x1
synced_folder + NFS
• ホストPCでnfsdを実行
• 仮想マシンからNFSマウント
• 高パフォーマンス / パーミッション
• トラブルもしばしば
41. (c) 2013 Masashi Shinbara @shin1x1
synced_folder + NFS
config.vm.synced_folder "../", "/share", nfs: true
• NFS synced_folder
42. (c) 2013 Masashi Shinbara @shin1x1
/vagrant
config.vm.synced_folder ".", "/vagrant"
• 実は、Vagrantfile があるディレクトリは
synced_folder
•仮想マシンでは /vagrant でアクセスできる
イメージとしては
43. /vagrant
(c) 2013 Masashi Shinbara @shin1x1
$ ls
Vagrantfile httpd.conf
$ vagrant up
$ ls /vagrant
Vagrantfile httpd.conf
ホストPC
仮想マシン
46. (c) 2013 Masashi Shinbara @shin1x1
AWSに環境構築
• EC2インスタンス起動
• プロビジョニング実行
• テストサーバとして公開できる
• 場合によっては、本番公開も
47. AWS プロバイダ
(c) 2013 Masashi Shinbara @shin1x1
$ vagrant plugin install vagrant-aws
•インストール
$ vagrant box add dummy https://
github.com/mitchellh/vagrant-aws/raw/
master/dummy.box
•dummy Boxファイル追加
48. AWS プロバイダ
(c) 2013 Masashi Shinbara @shin1x1
c.vm.provider :aws do |aws, override|
aws.access_key_id = ENV['AWS_ACCESS_KEY_ID']
aws.secret_access_key = ENV['AWS_SECRET_KEY']
aws.availability_zone = "ap-northeast-1c"
aws.instance_ready_timeout = 300
aws.instance_type = "t1.micro"
aws.region = "ap-northeast-1"
aws.ami = "ami-xxxxxxx"
aws.security_groups = ["web"]
aws.keypair_name = "xxxxx"
aws.tags = {
'Name' => 'vagrant_phpcon',
}
override.ssh.username = "vagrant"
override.ssh.private_key_path = "~/.ssh/id_rsa"
end
AWSの設定
50. ポイント
(c) 2013 Masashi Shinbara @shin1x1
•APIキーなどはVagrantfileには書かない
=> 環境変数などで取得
•synced_folderは、rsyncで同期
=> vagrant provision でデプロイできる
58. プロジェクトごとに作成
(c) 2013 Masashi Shinbara @shin1x1
ホストPC
プロジェクトA
PHP 5.1.6
PostgreSQL 8.2
プロジェクトB
PHP 5.3.27
PostgreSQL 9.2
プロジェクトC
PHP 5.4.17
MySQL 5.5
60. CIサーバでも使える
(c) 2013 Masashi Shinbara @shin1x1
プロジェクトA
PHP 5.1.6
PostgreSQL 8.2
プロジェクトB
PHP 5.3.27
PostgreSQL 9.2
プロジェクトC
PHP 5.4.17
MySQL 5.5
CIサーバ
68. (c) 2013 Masashi Shinbara @shin1x1
絶賛開発中
(近日公開予定)
Mac OS X 用
GUIアプリ
VagrantX