シェルプロビジョニングを使って、LAMPサーバを構築する

2013年12月22日 11時27分

201312221127VVAW00.png

前提条件

作業の前提条件は、下記のとおりです。

ソフトウエアバージョン
OSWindows 7 Ultimate 32bit
CygwinSetup Version 2.831
VirtualBox4.3.6 r91406
Vagrant1.3.5

構築するLAMPサーバのスペックは、下記のとおりです。

ソフトウエアバージョン
CentOS6.5 i386
Apache2.2.15
MySQL5.5.37
PHP5.5.12

LAMPサーバの構築

CentOS 6.5でVirtualBox Guest Add-Inのインストールが失敗する場合の対処方法で作成したBoxをベースにして、LAMPサーバを構築します。

  1. Box起動

    Vagrantfileを作成して、Boxを起動します。

    $ mkdir -p /tmp/test-box3 && cd /tmp/test-box3
    $ vi Vagrantfile
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      # BOX名
      config.vm.box = "test-box3"
    
      # BOXのダウンロードURL
      config.vm.box_url = "file://d:/temp/CentOS65-VB436-UPDATE.box"
    
      # ブリッジネットワークの指定
      config.vm.network :public_network
    end
    
    $ vagrant up
    
    Bringing machine 'default' up with 'virtualbox' provider...
    [default] Box 'test-box3' was not found. Fetching box from specified URL for
    the provider 'virtualbox'. Note that if the URL does not have
    a box for this provider, you should interrupt Vagrant now and add
    the box yourself. Otherwise Vagrant will attempt to download the
    full box prior to discovering this error.
    Downloading or copying the box...
    Extracting box...ate: 110M/s, Estimated time remaining: --:--:--)
    Successfully added box 'test-box3' with provider 'virtualbox'!
    [default] Importing base box 'test-box3'...
    [default] Matching MAC address for NAT networking...
    [default] Setting the name of the VM...
    [default] Clearing any previously set forwarded ports...
    [default] Creating shared folders metadata...
    [default] Clearing any previously set network interfaces...
    [default] Preparing network interfaces based on configuration...
    [default] Forwarding ports...
    [default] -- 22 => 2222 (adapter 1)
    [default] Booting VM...
    [default] Waiting for machine to boot. This may take a few minutes...
    [default] Machine booted and ready!
    GuestAdditions 4.3.6 running --- OK.
    [default] Configuring and enabling network interfaces...
    [default] Mounting shared folders...
    [default] -- /vagrant
    
    201312221127VVAW01.png
  2. シェルスクリプト作成

    Boxのプロビジョニングを行う、シェルスクリプトを作成します。

    $ mkdir -p script
    $ vi script/setup_base
    
    #!/bin/sh
    
    # SSHD設定
    cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.orig
    sed -i -e "/^#RSAAuthentication/s/^#//" -e "/^#PubkeyAuthentication/s/^#//" -e "/^PasswordAuthentication/s/yes/no/" /etc/ssh/sshd_config
    service sshd restart
    
    # ネットワークルール設定
    sed -i -e "s|/etc/udev/rules.d/70-persistent-net.rules|/dev/null|" /lib/udev/write_net_rules
    
    # fastestmirror設定
    echo "include_only=.jp" >> /etc/yum/pluginconf.d/fastestmirror.conf
    
    # Firewall、SELinux設定
    chkconfig iptables off
    chkconfig ip6tables off
    cp -p /etc/selinux/config /etc/selinux/config.orig
    sed -i -e "s|^SELINUX=.*|SELINUX=disabled|" /etc/selinux/config
    
    #!/bin/sh
    
    set -e
    DONE=/tmp/.apache_done
    if [ ! -f ${DONE} ]; then
      yum -y install httpd
      cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.orig
        sed -i -e "s|^#ServerName.*|ServerName `hostname`:80|" /etc/httpd/conf/httpd.conf
      httpd -t
      chkconfig httpd on
      service httpd start
      touch ${DONE}
    fi
    
    #!/bin/sh
    set -e
    
    DONE=/tmp/.repo_done
    if [ ! -f $DONE ]; then
      touch $DONE
    
      case `uname -m` in
      i686)ARCH=i386;;
      *)   ARCH=x86_64;;
      esac
      GPG_EPEL="ftp://ftp.iij.ad.jp/pub/linux/fedora/epel/RPM-GPG-KEY-EPEL-6"
      GPG_REMI="http://rpms.famillecollet.com/RPM-GPG-KEY-remi"
      GPG_RPMF="http://apt.sw.be/RPM-GPG-KEY.dag.txt"
      RPM_EPEL="ftp://ftp.iij.ad.jp/pub/linux/fedora/epel/6/$ARCH/epel-release-6-8.noarch.rpm"
      RPM_REMI="http://rpms.famillecollet.com/enterprise/remi-release-6.rpm"
      RPM_RPMF="http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.`uname -m`.rpm"
      for GPG in ${GPG_EPEL} ${GPG_REMI} ${GPG_RPMF}
      do
        rpm --import ${GPG}
      done
      for RPM in ${RPM_EPEL} ${RPM_REMI} ${RPM_RPMF}
      do
        yum -y install $RPM
      done
      for repo in epel remi rpmforge
      do
        [ -f /etc/yum.repos.d/$repo.repo ] && \
        sed -i 's/^enabled.*$/enabled = 0/' /etc/yum.repos.d/$repo.repo
      done
    fi
    
    #!/bin/sh
    set -e
    
    #
    # SETUP MySQL
    #
    DONE=/tmp/.mysql_done
    if [ ! -f ${DONE} ]; then
      yum -y install --enablerepo=epel,remi,rpmforge mysql-server
      yum list installed | grep mysql
      chkconfig mysqld on
      service mysqld start
      mysql -u root -e "DELETE FROM mysql.user WHERE User='';"
      mysql -u root -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
      mysql -u root -e "DROP DATABASE test;"
      mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD('#{node[:mysql][:passwd]}') WHERE User='root';"
      mysql -u root -e "FLUSH PRIVILEGES;"
      touch ${DONE}
    fi
    #
    # SETUP MYCNF
    #
    DONE=/tmp/.mycnf_done
    if [ ! -f ${DONE} ]; then
      CONF=/etc/my.cnf
      WTMP=`wc -l ${CONF} | awk '{print $1}'`
      WCNT=`expr ${WTMP} - 1`
      cat <<EOF> /tmp/$$
    [mysqld]
    character-set-server=utf8
    
    EOF
      tail -${WCNT} ${CONF} >> /tmp/$$
    
      cat <<EOF>> /tmp/$$
    [mysql]
    default-character-set=utf8
    EOF
    
      cp -p ${CONF} ${CONF}.orig
      cp -p /tmp/$$ ${CONF}
      chown root:root ${CONF}
      rm -f /tmp/$$
    
      service mysqld restart
    
      touch ${DONE}
    fi
    
    #!/bin/sh
    set -e
    
    [ ! -f /tmp/.repo_done ] && sudo /script/setup_repo
    
    DONE=/tmp/.php55_done
    if [ ! -f ${DONE} ]; then
      yum -y install --enablerepo=remi-php55 php
      yum -y install --enablerepo=remi-php55,remi php-mysql php-gd php-pdo \
      php-pear phppgsql php-mbstring php-devel php-xml
      yum -y install --enablerepo=epel,remi-php55,remi,rpmforge php-mcrypt
    
      cp -p /etc/php.ini /etc/php.ini.orig
      cat /etc/php.ini | \
      sed 's/;date\.timezone.*$/date\.timezone = Asia\/Tokyo/' > /tmp/$$
      cp -p /tmp/$$ /etc/php.ini
      rm -f /tmp/$$
      touch ${DONE}
    fi
    
  3. Vagrantfile修正

    作成したシェルスクリプトをVagrantfileに追加します。

    $ vi Vagrantfile
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      # BOX名
      config.vm.box = "test-box3"
    
      # BOXのダウンロードURL
      config.vm.box_url = "file://d:/temp/CentOS65-VB436-UPDATE.box"
    
      # ブリッジネットワークの指定
      config.vm.network :public_network
    
      # シェルプロビジョニング
      config.vm.provision "shell", path: "script/setup_base"
      config.vm.provision "shell", path: "script/setup_apache"
      config.vm.provision "shell", path: "script/setup_repo"
      config.vm.provision "shell", path: "script/setup_mysql"
      config.vm.provision "shell", path: "script/setup_php55"
    end
    
  4. プロビジョニング

    プロビジョニングを実行します。

    $ vagrant provision
    
    [default] Running provisioner: shell...
    [default] Running: D:/TEMP/vagrant-shell20140511-6000-mh35ij
    sshd を停止中: [  OK  ]
    sshd を起▒‹▒中: [  OK  ]
    [default] Running provisioner: shell...
    [default] Running: D:/TEMP/vagrant-shell20140511-6000-ox8svl
    Loaded plugins: fastestmirror
    Determining fastest mirrors
    Including mirror: ftp.tsukuba.wide.ad.jp
    Including mirror: www.ftp.ne.jp
    Including mirror: ftp.iij.ad.jp
    Including mirror: ftp.nara.wide.ad.jp
    Including mirror: ftp.jaist.ac.jp
    Including mirror: mirror.fairway.ne.jp
     * base: ftp.tsukuba.wide.ad.jp
    Including mirror: ftp.tsukuba.wide.ad.jp
    Including mirror: www.ftp.ne.jp
    Including mirror: ftp.iij.ad.jp
    Including mirror: ftp.nara.wide.ad.jp
    Including mirror: ftp.jaist.ac.jp
    Including mirror: mirror.fairway.ne.jp
    
    Transaction Summary
    ================================================================================
    Install       3 Package(s)
    
    Total download size: 177 k
    Installed size: 409 k
    Downloading Packages:
    --------------------------------------------------------------------------------
    Total                                           122 kB/s | 177 kB     00:01
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Installing : libmcrypt-2.5.8-9.el6.i686                                   1/3
      Installing : libtool-ltdl-2.2.6-15.5.el6.i686                             2/3
      Installing : php-mcrypt-5.5.12-1.el6.remi.i686                            3/3
      Verifying  : libtool-ltdl-2.2.6-15.5.el6.i686                             1/3
      Verifying  : php-mcrypt-5.5.12-1.el6.remi.i686                            2/3
      Verifying  : libmcrypt-2.5.8-9.el6.i686                                   3/3
    
    Installed:
      php-mcrypt.i686 0:5.5.12-1.el6.remi
    
    Dependency Installed:
      libmcrypt.i686 0:2.5.8-9.el6        libtool-ltdl.i686 0:2.2.6-15.5.el6
    
    Complete!
    
    201312221127VVAW02.png
    201312221127VVAW03.png

    再度実行すると、指定した5つのシェルスクリプトが実行されていることが確認できます。

    $ vagrant provision
    
    [default] Running provisioner: shell...
    [default] Running: D:/TEMP/vagrant-shell20140511-5756-a9zqub
    sshd を停止中: [  OK  ]
    sshd を起▒‹▒中: [  OK  ]
    [default] Running provisioner: shell...
    [default] Running: D:/TEMP/vagrant-shell20140511-5756-h7u6z9
    [default] Running provisioner: shell...
    [default] Running: D:/TEMP/vagrant-shell20140511-5756-1ikdyom
    [default] Running provisioner: shell...
    [default] Running: D:/TEMP/vagrant-shell20140511-5756-18qt90z
    [default] Running provisioner: shell...
    [default] Running: D:/TEMP/vagrant-shell20140511-5756-1aosjcf
    
    201312221127VVAW04.png