【Vagrant+VirtualBox】CentOS8のLAMP開発環境を構築


概要

VagrantとVirtualBoxがインストールされていること前提で構築方法を記述する。なおローカルで使用するためセキュリティは考慮しない。

環境

MACOS Mojave 10.14.5(18F132)
Vagrant 2.2.7
VirtualBox 6.0.16 r135674 (Qt5.6.3)

Vagrantfileの作成

ターミナルより作成ディレクトリに移動した後、下記を実行するとVagrantfileが作成される。
基本的には、必要な設定を追記していく。

command
vagrant init bento/centos-8

記述方法

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

 # Vagrant設定この中に記述していく

  config.vm.provision "shell", inline: <<-SHELL
   # SHELL設定この中に記述していく
  SHELL

end

Vagrantfileの編集

ここで行うことは下記の通り。

  • ネットワークの設定
  • 仮想端末のスペック設定
  • モジュール設定(SHELL)
    • apache、php、mysql
  • サービス起動(SHELL)

ネットワークの設定

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "10.10.10.74"
end

仮想端末のスペック設定

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.provider :VirtualBox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "3072"]
    vb.customize ["modifyvm", :id, "--cpus", "3"]
  end
end

モジュール設定(SHELL)

基本設定

Vagrantfile
config.vm.provision "shell", inline: <<-SHELL
    # リポジトリ登録
    dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
    dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

    # OSアップデート
    dnf -y update kernel
    dnf -y install kernel-devel kernel-headers dkms gcc gcc-c++
    dnf -y update
SHELL

apache2.4

Vagrantfile
config.vm.provision "shell", inline: <<-SHELL
  dnf -y install httpd
  cat >> /etc/httpd/conf/httpd.conf << "EOF"
<Directory "/var/www/html">
  AllowOverride All
  Require all granted
</Directory>
EOF
SHELL

PHP7.4

Vagrantfile
config.vm.provision "shell", inline: <<-SHELL
  dnf module install -y php:remi-7.4
  dnf install -y php-mbstring
  dnf install -y php-mcrypt
  dnf install -y php-mysqlnd
  dnf install -y php-xmlrpc
  dnf install -y php-pear
  dnf install -y php-gd
  dnf install -y php-pdo
  dnf install -y php-intl
  dnf install -y php-mysql

  cat >> /etc/php.ini << "EOF"
date.timezone = Asia/Tokyo
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = UTF-8
mbstring.http_output = pass
mbstring.encoding_translation = On
mbstring.detect_order = UTF-8,EUC-JP,SJIS,JIS,ASCII
mbstring.substitute_character = none
sendmail_path = /usr/local/sbin/ssmtp -t -i
upload_max_filesize = 128M
post_max_size = 128M
EOF
SHELL

MySQL8

Vagrantfile
config.vm.provision "shell", inline: <<-SHELL
  dnf -y module install mysql

  cat >> /etc/my.cnf << "EOF"
[client]
default-character-set = utf8
[mysql]
default-character-set = utf8
[mysqldump]
default-character-set = utf8
EOF
SHELL

サービス起動

Vagrantfile
config.vm.provision "shell", inline: <<-SHELL
  systemctl start php-fpm
  systemctl start mysqld.service
  systemctl start httpd.service

  systemctl enable php-fpm
  systemctl enable mysqld.service
  systemctl enable httpd.service
EOF
SHELL

確認

ブラウザで10.10.10.74にアクセスする。