Program LOG

勉強した内容をまとめ中。。。間違ってたら教えて。。。

ChefでphpMyAdminのインストールと、VirtualHostを設定する

フロントサイド用の環境構築がひと段落したので、
今度は、サーバーサイドの環境構築をもっと使いやすくしていきたいと思います。

phpMyAdminをインストール

phpMyAdminをインストールするには、remiレポジトリを入れなければなりません。
Berkshelfを用いてインストールした後、phpMyAdminのインストールをしたいと思います。
前回作成し、利用したdevelop cookbooksを再利用したいとおもいます。
まずは、「site-cookbooks」の中にある「metadata.rb」に以下のコードを追記します。

name             'develop'
maintainer       'YOUR_COMPANY_NAME'
maintainer_email 'YOUR_EMAIL'
license          'All rights reserved'
description      'Installs/Configures develop'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version          '0.1.0'
#以下を追記
depends 'yum'

次に、Berksfileに以下を追記します。

site :opscode

cookbook 'yum'

recipeのdefault.rbに追記していきます。

yum_repository 'epel' do
  description 'Extra Packages for Enterprise Linux'
  mirrorlist 'http://mirrors.fedoraproject.org/mirrorlist?repo=epel-6&arch=$basearch'
  fastestmirror_enabled true
  gpgkey 'http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6'
  action :create
end
# add the Remi repo
yum_repository 'remi' do
  description 'Les RPM de Remi - Repository'
  baseurl 'http://rpms.famillecollet.com/enterprise/6/remi/x86_64/'
  gpgkey 'http://rpms.famillecollet.com/RPM-GPG-KEY-remi'
  fastestmirror_enabled true
  action :create
end

これでepelとremiが使えるようになります。
次に、phpmysqlなどをremiで入れていきます。
default.rbに追記していきます。

%w[php php-devel php-mbstring gd-devel php-gd].each do |pkg|
	yum_package pkg do
	  action :install
	  options "--enablerepo=remi"
	end
end
#mysqlのインストール
%w[mysql-server mysql-devel phpMyAdmin php-mysql].each do |pkg|
	package pkg do
	    action :install
	    options "--enablerepo=remi"
	end
end

それでは実行したいと思います。

$ knife solo cook develop
・・・中略・・・
  * yum_repository[epel] action createRecipe: 
  * template[/etc/yum.repos.d/epel.repo] action create
    - create new file /etc/yum.repos.d/epel.repo
    - update content in file /etc/yum.repos.d/epel.repo from none to 4dd1d3
        --- /etc/yum.repos.d/epel.repo  2014-06-28 08:14:16.262168291 +0000
        +++ /tmp/chef-rendered-template20140628-2604-yvjhmx     2014-06-28 08:14              :16.266170291 +0000
        @@ -1 +1,12 @@
        +# This file was generated by Chef
        +# Do NOT modify this file by hand.
        +
        +[epel]
        +name=Extra Packages for Enterprise Linux
        +enabled=1
        +fastestmirror_enabled=true
        +gpgcheck=1
        +gpgkey=http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
        +mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-6&arch              =$basearch
        +sslverify=1
    - change mode from '' to '0644'

  * execute[yum-makecache-epel] action run
    - execute yum -q makecache --disablerepo=* --enablerepo=epel

  * ruby_block[yum-cache-reload-epel] action create
    - execute the ruby block yum-cache-reload-epel

  * execute[yum-makecache-epel] action nothing (skipped due to action :nothing)
  * ruby_block[yum-cache-reload-epel] action nothing (skipped due to action :not              hing)


Recipe: develop::default
  * yum_repository[remi] action createRecipe: 
  * template[/etc/yum.repos.d/remi.repo] action create
    - create new file /etc/yum.repos.d/remi.repo
    - update content in file /etc/yum.repos.d/remi.repo from none to f62c82
        --- /etc/yum.repos.d/remi.repo  2014-06-28 08:14:33.670868273 +0000
        +++ /tmp/chef-rendered-template20140628-2604-h828u      2014-06-28 08:14              :33.672869273 +0000
        @@ -1 +1,12 @@
        +# This file was generated by Chef
        +# Do NOT modify this file by hand.
        +
        +[remi]
        +name=Les RPM de Remi - Repository
        +baseurl=http://rpms.famillecollet.com/enterprise/6/remi/x86_64/
        +enabled=1
        +fastestmirror_enabled=true
        +gpgcheck=1
        +gpgkey=http://rpms.famillecollet.com/RPM-GPG-KEY-remi
        +sslverify=1
    - change mode from '' to '0644'

  * execute[yum-makecache-remi] action run
    - execute yum -q makecache --disablerepo=* --enablerepo=remi

  * ruby_block[yum-cache-reload-remi] action create
    - execute the ruby block yum-cache-reload-remi

  * execute[yum-makecache-remi] action nothing (skipped due to action :nothing)
  * ruby_block[yum-cache-reload-remi] action nothing (skipped due to action :nothing)


Recipe: develop::default
  * yum_package[php] action install
    - install version 5.4.30-1.el6.remi of package php

インストールされたようですので、バージョン確認をしたいと思います。
remiでインストールされていれば、5.4系が入っているはずです。
また、phpMyAdminもインストールされているはずです。

[vagrant@localhost ~]$ php -v
PHP 5.4.30 (cli) (built: Jun 25 2014 15:27:51)
[vagrant@localhost ~]$ cd /usr/share/phpMyAdmin/
[vagrant@localhost phpMyAdmin]$ pwd
/usr/share/phpMyAdmin

ひとまず正常に入っていました!

templateリソースでVirtualHostを設定したいと思います。

まずは、以下の内容を「vhost.conf.erb」ファイルという名前で「site-cookbook/template/default/」の中に保存します。

NameVirtualHost *:80
<VirtualHost *:80>
  ServerName <%= node[:domain] %>
  DocumentRoot /var/www/html/
  ErrorLog /var/log/php_error.log
  TransferLog /var/log/php_access.log
  <Directory /var/www/html/>
    AllowOverride all
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName php.<%= node[:domain] %>
  DocumentRoot /usr/share/phpMyAdmin/
  ErrorLog /var/log/php_error.log
  TransferLog /var/log/php_access.log
  <Directory /usr/share/phpMyAdmin/>
    AllowOverride all
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

NameVirtualHost *:443
<VirtualHost *:443>
    ServerName <%= @domain %>
    DocumentRoot /var/www/html/
    ErrorLog logs/ssl_error_test_log
    CustomLog logs/ssl_access_test_log combined
    LogLevel warn
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    <Directory /var/www/html/>
        AllowOverRide ALL
    </Directory>
</VirtualHost>

次に、「nodes/develop.json」ファイルを以下のように修正します。

{
  "domain":"develop.jp",
  "run_list":[
     "recipe[develop]",
     "recipe[develop::ruby]",
     "recipe[develop::node]",
     "recipe[develop::grunt]"
     "recipe[develop::developer]"
  ]
}

前回オレオレ証明書を作成した際に作成したrecipe(ssl.rb)をリファクタリングして、
ssl.rbからdeveloper.rbというファイル名に変更して、そこに追記していきます。

#Virtual hostsを設定する
template "vhost.conf" do
  path "/etc/httpd/conf.d/vhost.conf"
  source "vhost.conf.erb"
  mode 0644
  variables(
    :domain => node["domain"]
  )
  notifies :reload, 'service[httpd]'
end

以上で、変数を用いて作成してくれるので、毎回VirtualHostを書き換える必要がなくなります!
それでは実行したいと思います。

$ knife solo cook develop
・・・中略・・・
  * template[vhost.conf] action create
    - create new file /etc/httpd/conf.d/vhost.conf
    - update content in file /etc/httpd/conf.d/vhost.conf from none to db139f
        --- /etc/httpd/conf.d/vhost.conf        2014-06-28 10:00:42.403156956 +0000
        +++ /tmp/chef-rendered-template20140628-17069-101hcd4   2014-06-28 10:00:42.404156957 +0000
        @@ -1 +1,27 @@
・・・中略・・・
    - change mode from '' to '0644'

正常になっているか、ファイルの内容を確認したいと思います。

[vagrant@localhost ~]$ vim /etc/httpd/conf.d/vhost.conf
NameVirtualHost *:80
<VirtualHost *:80>
  ServerName develop.jp
  DocumentRoot /var/www/html/
  ErrorLog /var/log/php_error.log
  TransferLog /var/log/php_access.log
  <Directory /var/www/html/>
    AllowOverride all
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName php.develop.jp
  DocumentRoot /usr/share/phpMyAdmin/
  ErrorLog /var/log/php_error.log
  TransferLog /var/log/php_access.log
  <Directory /usr/share/phpMyAdmin/>
    AllowOverride all
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

NameVirtualHost *:443
<VirtualHost *:443>
    ServerName develop.jp
    DocumentRoot /var/www/html/
    ErrorLog logs/ssl_error_test_log
    CustomLog logs/ssl_access_test_log combined
    LogLevel warn
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    <Directory /var/www/html/>
        AllowOverRide ALL
    </Directory>
</VirtualHost>

正常に動作しているか、Windowsのhostsファイルを修正してドメイン解決を行った上で、
ブラウザより確認したいと思います。
設定したドキュメントルート先:「http://develop.jp/
設定したドキュメントルート先(SSL版):「https://develop.jp/
phpMyAdmin先「http://php.develop.jp/

一応自分の環境で確認したところ、正常にページが確認する事が出来ました。
今回の開発で、phpMyAdminとVirtualHostの設定をChefで実行できるようになりました。
また、今後は開発環境をfixできるように環境構築をまとめていきたいと思います。

使用環境