Vagrant上のCentOSにItamaeでLAMP環境構築

#環境

- CentOS7 minmal

*予め、itamaeのレシピである「root_recipe.rb」「apache.rb」「php.rb」「mysql.rb」「test_recipe.rb」はvagarantディレクトリ内に作成しておく*

itamaeのコマンドはsshでログイン後に実行する
`sudo itamae local /home/vagrant/recipe/test_recipe.rb`

#vagrantfile
```ruby:vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.box = "centos7min/es"
  config.vm.network "forwarded_port", guest: 80, host: 50505, id:"http"
  config.vm.network "private_network", ip: "192.168.33.13"
  config.vm.synced_folder "../shared", "/vagrants"
  config.vbguest.auto_update = false
  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
    vb.memory = "4096"
  end
  config.vm.provision "shell", inline: <<-SHELL

    ##GUIの準備
    yum -y groupinstall "Server with GUI"
    systemctl set-default graphical.target

    ##Itamaeを使う為の設定
    yum -y install rubygems
    gem install itamae
    echo "itameのバージョン"
    gem list itamae 

    ##レシピのコピー
    mkdir /home/vagrant/recipe
    cp -p /vagrant/*.rb /home/vagrant/recipe

    echo "起動後実行"
    echo systemctl start graphical.target

    echo ""
    echo -Defaults env_reset
    echo +#Defaults env_reset
    echo ""
    echo +Defaults env_keep += "PATH"
    echo ""
    echo -Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
    echo +#Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
    echo ""
    
    echo ""
    echo "sudo itamae local /home/vagrant/recipe/test_recipe.rb --dry-run"
    echo "sudo itamae local /home/vagrant/recipe/root_recipe.rb --dry-run"

    ##次回起動時実行
    echo vagrant vbguest

  SHELL
end


```

```ruby:root_recipe.rb
# 言語とtimezone設定
execute 'set timezone' do
  user "root"
  command <<-EOL
    yum -y install ImageMagick ImageMagick-devel ipa-pgothic-fonts ibus-kkc vlgothic-*
    localectl set-locale LANG=ja_JP.UTF-8
    timedatectl set-timezone Asia/Tokyo
    timedatectl set-local-rtc no
  EOL
end

# 念の為にfirewalld停止、無効化
service 'firewalld' do
  action [:disable, :stop]
end

# 各レシピを実行
include_recipe "./apache.rb"
include_recipe "./php.rb"
include_recipe "./mysql.rb"

service 'firewalld' do
  action [:start, :enable]
end

execute 'open http' do
  command <<-EOL
    firewall-cmd --zone=public --add-service=http --permanent
    firewall-cmd --reload
  EOL
end

# Apache起動、有効化
service 'httpd' do
  action [:start, :enable]
end
```

```ruby:apache.rb
# Apacheインストール
package 'httpd'

# httpd.confのバックアップ
execute 'httpd.conf backup' do
    command 'cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org'
end

# wwwユーザ追加
execute 'add www user' do
    user "root"
    command <<-EOL
        useradd www
        gpasswd -a apache www
        chown -R www:apache /var/www/html
    EOL
end
```

```ruby:php.rb
# PHPのインストール
%w(php php-mysqlnd php-gd php-intl php-mbstring).each do |pkg|
  package pkg do
    action :install
  end
end

# php.iniのバックアップ
execute 'php.ini backup' do
    command 'cp /etc/php.ini /etc/php.ini.org'
end
```
```ruby:mysql.rb
# MySQLインストール
%w(mariadb-server mariadb-devel).each do |pkg|
  package pkg do
    action :install
  end
end

# MySQL権限設定
execute 'mysql permission' do
    command <<-EOL
        chown mysql:mysql /var/log/mysqld.log
        chown -R mysql:mysql /var/lib/mysql
    EOL
end

# my.cnfのバックアップ
execute 'my.cnf backup' do
    command 'cp /etc/my.cnf /etc/my.cnf.org'
end

# MySQL起動、有効化
service 'mariadb' do
    action [:start, :enable]
end

# MySQL初期設定
execute "mysql_secure_installation" do
    user "root"
    only_if "mysql -u root -e 'show databases' | grep information_schema" # パスワードが空の場合
    command <<-EOL
        mysqladmin -u root password "password"
        mysql -u root -ppassword -e "DELETE FROM mysql.user WHERE User='';"
        mysql -u root -ppassword -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1');"
        mysql -u root -ppassword -e "DROP DATABASE test;"
        mysql -u root -ppassword -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
        mysql -u root -ppassword -e "FLUSH PRIVILEGES;"
    EOL
end
```
```ruby:test_recipe.rb
execute "echo HelloWorld!" do
  command "echo HelloWorld"
end
```