LoginSignup
15
14

More than 5 years have passed since last update.

Windows7上でVagrant/Chef-soloによるLAMP開発サーバ構築(201407)

Posted at

軽くWindows上でのVagrant/Chef-soloを動かそうと思ったら予想外にハマったので備忘録を残す。

概要

Windows 7上のVirtualBoxにVagrant/Chef-soloで以下を構築
・Centos6.5 64bit
・Apache 2.2/yumでインストール
・mysql 5.1/yumでインストール,擬似Secure_Installの実行
・php 5.3/yumでインストール
・phpMyAdmin/yumでインストール
・ブリッジネットワーク追加
・github連携はなし

virtualbox

virtualbox.org
から最新版をダウンロード、インストール

vagrant

www.vagrantup.com/downloads.html
から最新版をダウンロードしインストール

PATH

Ruby PATH設定

vgrantバンドル版のrubyとchef-soloを連携させるため環境変数等でPATHを通す
例 : C:\HashiCorp\Vagrant\embedded\bin
※既存のrubyにPATHが通っている際はvagrant作業時のみPATHを通してください。
rubyバージョンが表示されることを確認
> where ruby
C:\HashiCorp\Vagrant\embedded\bin\ruby.exe

> ruby -v
2.0.0p353 (2013-11-22) [i386-mingw32]

Cygwin PATH設定

●●●●●●●●●●cygwinファイル名記載予定
sshとrsyncのファイルをディレクトリに保存しパスを通す
C:\HashiCorp\Vagrant\cygwin-bin\
●●●●●●●●●●
sshとrsyncのバージョンが表示されることを確認する
> where ssh
C:\HashiCorp\Vagrant\cygwin-bin\ssh.exe

> where rsync
C:\HashiCorp\Vagrant\cygwin-bin\rsync.exe

> ssh -V
OpenSSH_6.6.1p1, OpenSSL 1.0.1h 5 Jun 2014

> rsync --version
rsync version 3.1.0 protocol version 31

Chef-solo関連

bundler経由gemでknifeのインストール

win32-apiのバージョンによりknifeがエラーとなるため
bundler で1.5.1上をインストール

> gem install bundler
> cd C:\
> mkdir localdev
> cd C:\localdev
> mkdir GemDir
> cd GemDir
> bundle init 

出来上がったC:\localdev\GemDir\Gemfileを編集し1.5.1以上をインストール

Gemfile
source "https://rubygems.org"
gem 'win32-api', '~>1.5.1'
gem 'knife-solo'
> bundle install
> gem list | grep win32
1.5.1以上が存在すること
> gem list | grep knife
インストールされていること
> gem list | grep chef
インストールされていること

chefのリポジトリ作成

workstationにchefのリポジトリを作る。
下記で、レシピのテンプレートが作られる。このchef-repoというディレクトリにレシピを作成する。

> cd C:\localdev
> knife solo init chef-repo

クックブック作成

> cd C:\localdev\chef-repo
> knife cookbook create base -o site-cookbooks
> knife cookbook create LAMP -o site-cookbooks

レシピの作成とテンプレートファイルの設置

C
#
# Cookbook Name:: base
# Recipe:: default
#
# Copyright 2014, NAME
#
# All rights reserved - Do Not Redistribute
#

# install pack
%w{traceroute wget sysstat tcpdump ntpdate ntp iotop vim mlocate }.each do |setup| #
  package setup do
    action :install
  end
end

execute "yum.update" do #
  command "yum -y update"
  action :run
end

# Centos Timezone JST 
link "/etc/localtime" do #
  to "/usr/share/zoneinfo/Asia/Tokyo"
end

# chkconfig ntpd ntpdate
%w{ntpdate ntpd}.each do |ntpservice| #
  service ntpservice do
    action [:enable]
  end
end

# start ntpdate
###service "ntpdate" do #
###  action [:restart]
###end

# mod /etc/ntp.conf
# restart ntpd
template "ntp.conf" do #
  path "/etc/ntp.conf"
  source "ntp.conf.erb"
  mode 0644
  notifies :restart, 'service[ntpd]' 
end
C
#
# Cookbook Name:: LAMP
# Recipe:: default
#
# Copyright 2014, NAME
#
# All rights reserved - Do Not Redistribute
#

bash 'add_epel' do
  user 'root'
  code <<-EOC
    rpm -ivh http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
    sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/epel.repo
  EOC
  creates "/etc/yum.repos.d/epel.repo"
end

###bash 'add_rpmforge' do
###  user 'root'
###  code <<-EOC
###    rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
###    sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/rpmforge.repo
###  EOC
###  creates "/etc/yum.repos.d/rpmforge.repo"
###end

###bash 'add_remi' do
###  user 'root'
###  code <<-EOC
###    rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
###    sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/remi.repo
###  EOC
###  creates "/etc/yum.repos.d/remi.repo"
###end

# install lamp

%w{php mysql-server php-mysql php-mcrypt httpd }.each do |lamp| #
  package lamp do
    action :install
  end
end

# install phpMyAdmin
package "phpMyAdmin" do
  action :install
end

# chkconfig httpd on
service "httpd" do #
  action [:restart, :enable]
end

# mysqld start
# chkconfig mysqld on
service "mysqld" do #
  action [:restart, :enable]
end

script "Secure_Install" do
  interpreter 'bash'
  user "root"
  only_if "mysql -u root -e 'show databases'"
  code <<-EOL
    mysqladmin -u root password "your_password"
    mysql -u root -pyour_password -e "DELETE FROM mysql.user WHERE User='';"
    mysql -u root -pyour_password -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1');"
    mysql -u root -pyour_password -e "DROP DATABASE test;"
    mysql -u root -pyour_password -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
    mysql -u root -pyour_password -e "FLUSH PRIVILEGES;"
  EOL
end

# php.ini
##template "php.ini" do #
##  path "/etc/php.ini"
##  source "php.ini.erb"
##  mode 0644
##end

##template "index.html" do #
##  path "/var/www/html/index.html"
##  source "index.html.erb"
##  mode 0644
##end

# httpd.conf
##template "httpd.conf" do 
##  path "/etc/httpd/conf/httpd.conf"
##  source "httpd.conf.erb"
##  mode 0644
##  notifies :restart, 'service[httpd]' 
##end

# my.cnf
##template "my.cnf" do 
##  path "/etc/my.cnf"
##  source "my.cnf.erb"
##  mode 0644
##  notifies :restart, 'service[mysqld]' 
##end

# mod /etc/httpd/conf.d/phpMyAdmin.conf
# restart httpd
template "phpMyAdmin.conf" do #
  path "/etc/httpd/conf.d/phpMyAdmin.conf"
  source "phpMyAdmin.conf.erb"
  mode 0644
  notifies :restart, 'service[httpd]' 
end
C
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1 
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server ntp.jst.mfeed.ad.jp


#broadcast 192.168.1.255 autokey    # broadcast server
#broadcastclient            # broadcast client
#broadcast 224.0.1.1 autokey        # multicast server
#multicastclient 224.0.1.1      # multicast client
#manycastserver 239.255.254.254     # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
C
# phpMyAdmin - Web based MySQL browser written in php
# 
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       Require ip 127.0.0.1
       Require ip ::1
     </RequireAny>
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 10.0.0.0/255.0.0.0
     Allow from 172.16.0.0/255.240.0.0
     Allow from 192.168.0.0/255.255.255.0
     Allow from 127.0.0.1
     Allow from ::1
   </IfModule>
</Directory>

<Directory /usr/share/phpMyAdmin/setup/>
   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       Require ip 127.0.0.1
       Require ip ::1
     </RequireAny>
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 127.0.0.1
     Allow from ::1
   </IfModule>
</Directory>

# These directories do not require access over HTTP - taken from the original
# phpMyAdmin upstream tarball
#
<Directory /usr/share/phpMyAdmin/libraries/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>

<Directory /usr/share/phpMyAdmin/setup/lib/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>

<Directory /usr/share/phpMyAdmin/setup/frames/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>

# This configuration prevents mod_security at phpMyAdmin directories from
# filtering SQL etc.  This may break your mod_security implementation.
#
#<IfModule mod_security.c>
#    <Directory /usr/share/phpMyAdmin/>
#        SecRuleInheritance Off
#    </Directory>
#</IfModule>

Vagrant関連

Vagrant box 定義

www.vagrantbox.es
CentOS 6.5 x86_64のVirtualBoxのURLをコピー
「COPY」ボタンを押して、URLをコピーしておき以下を実行。

例 : vagrant box add dev-host01 "コピーしたBOX定義URL"
>cd C:\localdev
C:\>vagrant box add dev-host01 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
※時間がかかるためWindowsが休止にならないよう注意
C:\>vagrant init dev-host01

GUIでVirtual BOXを起動してまだ仮想マシンが作成されてないことを確認

NIC追加

※デフォルトのローカルNAT NICとは別に追加でブリッジNICを作成する

Vagrantfileのpublic_networkが有効であることを確認する

Vagrantfile
config.vm.network "public_network"
>cd C:\localdev
>vagrant up

VMはC:\Users[User名].vagrant.d\boxesに存在する

boxがリストアップされたことを確認

>vagrant box list

chef-solo

knife-soloのインストール

※仮想OSでインターネット接続できなければエラーとなる。
 その際はssh localhost 2222 でログインしDNSやルーティング確認をする

knife solo prepare を実行すると、VMにchefをインストールしてくれる。

例 : > knife solo prepare -i "c:\Users\[ユーザー名]\.vagrant.d\insecure_private_key" -p 2222 vagrant@127.0.0.1
> knife solo prepare -i "c:\Users\hoge\.vagrant.d\insecure_private_key" -p 2222 vagrant@127.0.0.1

C:\localdev\chef-repo\nodes\127.0.0.1.jsonを編集しレシピを追記する

C
{
  "run_list": [
  "recipe[base]",
  "recipe[LAMP]"
  ],
  "automatic": {
    "ipaddress": "127.0.0.1"
  }
}

knife-soloの実行

> knife solo cook -i "c:\Users\vuser\.vagrant.d\insecure_private_key" -p 2222 vagrant@127.0.0.1

レシピbase,LAMPによるミドル設定が行われる。

ゲストOS再起動と確認

> vagrant reload

localhost:2222にsshログインしifconfigでブリッジネットワークが動作していことを確認。
ミドルも必要であれば確認。

15
14
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
15
14