29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vagrantとitamaeによるLAMP環境構築

Last updated at Posted at 2015-11-23

最近Linuxをいじることがちょっと多いので、それをコード化できるといいなぁ(設定した内容忘れるからorz)と思い、今更ながらvagrantとitamaeを使って手始めにLAMP環境を構築してみました。
サーバー設定にはchefがメジャーみたいですが、シンプルが好きなのでitamaeを選択してみました。

環境

  • Mac OS X Yosemite 10.10.5
  • VirtualBox 5.0.8 for OS X hosts
  • Vagrant 1.7.4
  • Itamae v1.6.3
  • ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

vagrant環境構築

VirtualBoxインストール
https://www.virtualbox.org/wiki/Downloads
vagrantインストール
https://www.vagrantup.com/downloads.html
作業ディレクトリ作成
mkdir -p ~/Vagrant/CentOS65
cd ~/Vagrant/CentOS65
CentOS6.5仮想環境ベース用意
vagrant box add centos65 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
初期化、設定ファイル(Vagrantfile)作成およびバックアップ
vagrant init centos65
cp Vagrantfile Vagrantfile.org
Vagrantfie修正
diff Vagrantfile.org Vagrantfile
<   # config.vm.network "private_network", ip: "192.168.33.10"
---
>   config.vm.network "private_network", ip: "192.168.33.10"

itamae環境構築

bundleインストール
sudo gem install bundler
作業ディレクトリ作成
mkdir recipe-lamp
cd recipe-lamp
Gemfile作成
vim Gemfile
Gemfile
source 'https://rubygems.org'
gem 'itamae'
itamaeインストール
bundle install

itamaeによるLAMP環境構築レシピ作成

各レシピのディレクトリ作成
mkdir cookbooks
mkdir cookbooks/apache
mkdir cookbooks/mysql
mkdir cookbooks/php
mkdir cookbooks/phpmyadmin
recipe-lamp/set.rb
# timezone設定
execute 'set timezone' do
    user "root"
    command <<-EOL
        cp -p /usr/share/zoneinfo/Japan /etc/localtime
        echo 'ZONE="Asia/Tokyo"' > /etc/sysconfig/clock
        echo 'UTC=false' >> /etc/sysconfig/clock
    EOL
end

# 言語設定
file '/etc/sysconfig/i18n' do
    action :edit
    user "root"
    block do |content|
        content.gsub!("en_US", "ja_JP")
    end
end

# vimのタブ幅、文字コード等設定
file '/home/vagrant/.vimrc' do
    action :create
    user "vagrant"
    content <<-EOL
set tabstop=4
set shiftwidth=4
set noexpandtab
set softtabstop=0
set encoding=utf-8
set fileencodings=iso-2022-jp,cp932,sjis,euc-jp,utf-8
set fileformats=unix,mac,dos
    EOL
end

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

include_recipe "./cookbooks/apache/default.rb"
include_recipe "./cookbooks/php/default.rb"
include_recipe "./cookbooks/mysql/default.rb"
include_recipe "./cookbooks/phpmyadmin/default.rb"

# Apache起動、有効化
service 'httpd' do
    action [:start, :enable]
end
recipe-lamp/cookbooks/apache/default.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
recipe-lamp/cookbooks/mysql/default.rb
# MySQLインストール
package 'mysql-server'

# 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 'mysqld' 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
recipe-lamp/cookbooks/php/default.rb
# PHPのインストール
package 'php'

# PHPのプラグインをインストール
package 'php-mysqlnd php-gd php-intl php-mbstring'

# php.iniのバックアップ
execute 'php.ini backup' do
    command 'cp /etc/php.ini /etc/php.ini.org'
end
recipe-lamp/cookbooks/phpmyadmin/default.rb
# phpMyAdminインストール
package 'phpMyAdmin'

# phpMyAdmin権限設定
execute 'phpMyAdmin permission' do
    command <<-EOL
    chown -R root:apache /usr/share/phpMyAdmin/
    EOL
end

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

# phpMyAdmin.confの設定
file '/etc/httpd/conf.d/phpMyAdmin.conf' do
    action :edit
    block do |content|
        content.gsub!("Allow from ::1", "Allow from ::1\n     Allow from 192.168.")
    end
end

LAMP環境起動

CentOS仮想環境起動
cd ~/Vagrant/CentOS65/recipe-lamp/
vagrant up
LAMP環境構築
itamae ssh --vagrant set.rb

これで
http://192.168.33.10/

http://192.168.33.10/phpmyadmin/
が表示されればOK。
phpmyadminはroot、passwordでログイン。

参考

Chef-soloからItamaeに完全移行した話
Vagrantの基本
クックパッドのサーバプロビジョニング事情
LAMP環境構築itamaeレシピを作ってみる
Itamaeのeditアクションが便利
Itamaeチートシート

29
27
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
29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?