$ vagrant -v
Vagrant 1.4.3
$ vagrant box list
centos6.5 (virtualbox)
$ vagrant init centos6.5
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の中身
# -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "centos6.5" end
$ vagrant up
色々表示されて、最後にエラーが表示される。
$ vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run vagrant halt
to
shut it down forcefully, or you can run vagrant suspend
to simply
suspend the virtual machine. In either case, to restart it again,
simply run vagrant up
.
Saharaプラグインを使う
スナップショット機能が使えるやつ。
$ vagrant plugin install sahara
$ vagrant plugin list
sahara (0.0.17) vagrant-aws (0.4.1) vagrant-berkshelf (1.3.7) vagrant-omnibus (1.3.0) vagrant-vbguest (0.10.0)
Saharaをインストールすると、sanboxコマンドがつかえる。 vagrant sandbox onで、sandboxモードが有効になる。vagrant sandbox statusで状態確認できる。
$ vagrant sandbox status
[default] Sandbox mode is off
$ vagrant sandbox on
[default] Starting sandbox mode... 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
vagrant sandbox rollbackでサーバの状態を元に戻せる
vagrant ssh sudo -i yum install php php -v バージョン表示される exit exit vagrant sandbox rollback 戻る vagrant ssh php -v コマンドないといわれる
$ vagrant ssh-config --host hoge >> ~/.ssh/config
これで、ssh hogeでアクセスできるようになる。
chefでPHP、MySQL、Apacheをインストール・設定する
chefをインストールする 参考:初心者にお勧めするChefの正しいインストール方法 https://downloads.chef.io/chef-dk/ここからダウンロードして、インストールする。
knife-soloをインストールする。
$ sudo chef gem install knife-solo
Successfully installed knife-solo-0.4.2
Knifeの初回設定
$ knife configure
全部エンター押すと、~/.chef/knife.rbにknifeの設定ファイルが保存される。
vagrant hogeにchefをインストールする
$ knife solo prepare hoge
chefのリポジトリをつくる
$ knife solo init chef-repo
Creating kitchen... Creating knife.rb in kitchen... Creating cupboards... Setting up Berkshelf...
cookbookをつくる
$ knife cookbook create lamp -o site-cookbooks
recipeをつくる
$ vim site-cookbooks/lamp/recipes/default.rb
内容は、一旦下記を参考にする。 http://qiita.com/skinoshita/items/57ac059ff8b1008f5e1d#3-12
# stop iptables service "iptables" do action [:stop, :disable] end # install apache %w{httpd httpd-devel}.each do |pkg| package pkg do action :install end end # install php %w{php php-common php-devel php-mbstring php-xml php-pear php-mysql}.each do |pkg| package pkg do action :install end end # install mysql %w{mysql-server mysql-devel}.each do |pkg| package pkg do action :install end end # service configuration service "httpd" do supports :status => true, :restart => true, :reload => true action [:enable, :start] end service "mysqld" do action [:enable, :start] end
JSONファイルを編集する
$ vim ../nodes/hoge.json
{ "run_list": [ "lamp" ], "automatic": { "ipaddress": "hoge" } }
Vagrantfileを編集する
# -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "centos6.5" config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "chef-repo/site-cookbooks" chef.add_recipe "lamp" end end
$ vagrant reload これで、chefが動作するが、reload中に下記のようなエラーが出る場合は、ここに記載の対応が必要。
vagrant reloadの結果、下記のようにchefが動いた。