5
6

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でLAMP環境を構築してみる

Last updated at Posted at 2016-09-16

CentOS7とかPHP7を使ってみたかったので
お手軽に開発環境を用意するときのメモ書きとして作りました

作業環境

  • Windows7

前提

  1. Vagrantを使用する
  2. Virtualboxを使用する
  3. 構成管理ツールは使用しない

サーバー構成

  • CentOS-7
  • Apache/2.4.6
  • PHP7
  • MySQL5.6

CentOS-7 の BOXを追加

vagrant box add コマンドを使用します

$ vagrant box add centos7 https://github.com/holms/vagrant-centos7-box/releases/download/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box

==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7' (v0) for provider:
    box: Downloading: https://github.com/holms/vagrant-centos7-box/releases/down
load/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box
    box: Progress: 100% (Rate: 10.4M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!

追加済みのBOXのリストを確認

vagrant box list コマンドを使用します

$ vagrant box list

centos7    (virtualbox,0)

ちなみに追加されたBOXの本体は C:/Users/{USER}/.vagrant.d/boxes/ の中に追加されています

VMを初期化

適当な場所にVM用のフォルダを作成します

フォルダに移動してBOX名を指定してVMの初期化を行います

$ vagrant init centos7

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

初期化を行うとVagrantfileというファイルが生成されます
VagrantfileはVMの設定を記載するファイルです

Vagrantfile内容

  1. 使用するBOX名の指定
  2. プライベートIPアドレスの指定
  3. VMのメモリ量設定
  4. provisionファイルの指定
Vagrant.configure(2) do |config|
  config.vm.box = "centos7"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--memory", "1024"]
  end
  config.vm.provision :shell do |sh|
    sh.path = "provision.sh"
  end
end

provision.sh内容

  1. Webサーバーインストール
  • Firewallの設定
  • PHP7インストール
  • php.ini書き換え
  • MySQLインストール
  • ntp設定
# !/bin/bash

# WEB Server
echo "Web Server ############################################";
sudo yum -y install httpd
sudo systemctl start httpd.service
sudo systemctl enable httpd.service

# Firewall
echo "Firewall ############################################";
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service

# PHP7
echo "PHP7 ############################################";
sudo yum -y install epel-release
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo yum -y install --enablerepo=remi,remi-php70 php php-devel php-mbstring php-pdo php-gd

# php.ini
echo "php.ini ############################################";
sudo cp /etc/php.ini /etc/php.ini.org
echo "date.timezon = Asia\/Tokyo";
sudo sed -i -e "s/;date.timezone =/date.timezone = Asia\/Tokyo/" /etc/php.ini
echo "mbstring.language = Japanese";
sudo sed -i -e "s/;mbstring.language = Japanese/mbstring.language = Japanese/" /etc/php.ini
echo "mbstring.internal_encoding =UTF-8";
sudo sed -i -e "s/;mbstring.internal_encoding =/mbstring.internal_encoding = UTF-8/" /etc/php.ini
echo "mbstring.encoding_translation = Off";
sudo sed -i -e "s/;mbstring.encoding_translation = Off/mbstring.encoding_translation = Off/" /etc/php.ini
echo "mbstring.detect_order = auto";
sudo sed -i -e "s/;mbstring.detect_order = auto/mbstring.detect_order = auto/" /etc/php.ini
echo "max_execution_time = 180";
sudo sed -i -e "s/max_execution_time = 30/max_execution_time = 180/" /etc/php.ini

# mysql5.6
echo "MySQL5.6 ############################################";
sudo rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el6-7.noarch.rpm
sudo yum -y install yum-utils
sudo yum-config-manager --disable mysql57-community
sudo yum-config-manager --enable mysql56-community
sudo yum -y install mysql-server
sudo systemctl restart httpd.service
sudo systemctl enable mysqld.service
sudo systemctl start mysqld.service

# ntp
echo "ntp ############################################";
sudo yum -y install ntp
sudo cp /etc/ntp.conf /etc/ntp.conf.org
sudo sed -i -e "s/server 0.centos.pool.ntp.org iburst/server -4 ntp.nict.jp/" /etc/ntp.conf
sudo sed -i -e "s/server 1.centos.pool.ntp.org iburst/server -4 ntp1.jst.mfeed.ad.jp/" /etc/ntp.conf
sudo sed -i -e "s/server 2.centos.pool.ntp.org iburst/server -4 ntp2.jst.mfeed.ad.jp/" /etc/ntp.conf
sudo sed -i -e "s/server 3.centos.pool.ntp.org iburst/server -4 ntp3.jst.mfeed.ad.jp/" /etc/ntp.conf
sudo systemctl start ntpd.service
sudo systemctl enable ntpd.service
sudo timedatectl set-timezone Asia/Tokyo

VMを立ち上げる

予めプラグインをインストールしておく

$ vagrant plugin install vagrant-vbguest

Vagrantfileがあるフォルダでvagrant upコマンドを使用します

$ vagrant up

vagrant statusを使用してVMが立ち上がっているか確認します

$ vagrant status

Current machine states:

default                   running (virtualbox)

ブラウザからアクセスしてみて動作が問題ないか確認する

firstaccess.png

PHPの動作確認

/var/www/html に index.phpというファイルを作成し以下の内容を入力します

<?php
echo phpinfo();

ブラウザにアクセスを行いPHPの設定情報が表示されればOKです

5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?