LoginSignup
13
13

More than 5 years have passed since last update.

Vagrant + Chef + chef-wp-cli を使って WordPress サイトを構築してみる

Last updated at Posted at 2013-09-24

とりあえず、画面が表示されるまでを目標に

テスト環境

Mac: 10.8.5
Ruby: 1.9.3
Vagrant: 1.3.3
Chef: 11.4.4

クックブック

クックブック自体は、既に海外の方が作られている。
https://github.com/francescolaffi/chef-wp-cli

ただ、このクックブックは元々 Chef 10 系の物の為、11 系で動かすにはレシピを修正する必要あり。

↓動かなくなった理由はこちら

Chef 11 In-Depth: Attributes Changes
http://www.opscode.com/blog/2013/02/05/chef-11-in-depth-attributes-changes/

↓修正してみたのがこちら
https://github.com/wakuworks/chef-wp-cli

プロビジョニング

ディレクトリ構造


.
├── Vagrantfile
└── chef
    ├── Berksfile
    ├── cookbooks
    ├── data_bags
    ├── nodes
    ├── roles
    └── site-cookbooks

多分こんな感じ

chef/Berksfile
site :opscode
cookbook 'wp', git: 'https://github.com/wakuworks/chef-wp-cli.git'
Vagrantfile
# -*- 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 = "CentOS-6.4"
  config.vm.box_url = `uname -a`.index("x86_64") != nil ?
    "http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box" :
    "http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20130427.box"
  config.vm.network :private_network, ip: "192.168.33.10"

  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "512"]
  end

  config.vm.provision :shell, :inline => <<-EOT
    sudo /sbin/iptables -F && sudo /etc/init.d/iptables save
    if ! type rsync >/dev/null 2>&1; then
      yum install -y rsync
    fi
  EOT

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "chef/cookbooks"
    chef.add_recipe "php::module_mysql"
    chef.add_recipe "apache2"
    chef.add_recipe "wp::apache" # 現在は wp::apache のレシピしかない
    chef.json = {
      "wp" => {
        "installs" => {
          "wptest1.com" => {
            "url" => "wptest1.com",
            "title" => "my theme test"
          },
          "wptest2.com" => {
            "url" => "wptest2.com",
            "title" => "my theme test",
            "locale" => "ja" # 日本語化
          }
        }
      },
      "mysql" => {
        "server_root_password"   => "iloverandompasswordsbutthiswilldo",
        "server_repl_password"   => "iloverandompasswordsbutthiswilldo",
        "server_debian_password" => "iloverandompasswordsbutthiswilldo"
      }
    }
  end

  # vagrant-berkshelf をインストールしている状態で、
  # 下記の二行のコメントアウトを外すと自動的にダウンロードしてくれるのだが、
  # Vagrant 1.3.3 + vagrant-berkshelf 1.3.3 の環境だとエラーになる。
  #
  # config.berkshelf.enabled = true
  # config.berkshelf.berksfile_path = "chef/Berksfile"
  #
  # CookbookNotFound error on Vagrant 1.3.3 with Git and GitHub location
  # https://github.com/RiotGames/vagrant-berkshelf/issues/93
  #
  # 仕方ないので、chef ディレクトリに移動して直接ダウンロードを実行する。
  # $ cd chef
  # $ berks install --path=cookbooks
end

プロビジョニングが成功すると、/var/www/wptest1.com, /var/www/wptest2.com が出来上がる。
ディレクトリのパーミッションは、出来上がった状態のままだとアクセス出来ないので、適切な物に変更する。
後は Mac 側の hosts を、下記の様に変更してからブラウザでアクセス。

192.168.33.10 wptest1.com
192.168.33.10 wptest2.com

結果

cap.png

参考

Vagrantfile サンプル

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13