vagrantでcentos7+apache+php7.3+MariaDBの環境を作る


Vagrantfile を用意

適当なフォルダーを作ってその中に以下のようなVagrantfileを置く。

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 3000

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 1234, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
   config.vm.network "private_network", ip: "192.168.33.30"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
   config.vm.synced_folder ".", "/var/www/html", create: true, owner: 'vagrant', group: 'vagrant', mount_options: ['dmode=777','fmode=777']

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

で、そのフォルダー内で

vagrant up

これでcentosがインストールされる。
完了したらそのまま

vagrant ssh

でインストールしたcentosにログインする。

OSアップデート

ログインしたらひとまずアップデートする。
これは結構時間がかかります。

sudo yum update

時刻合わせ

終わったら時刻を日本にする。
今の時間や設定は、

date
localectl
locale

などのコマンドで確認できる。以下で日本に設定。

sudo localectl set-locale LANG=ja_JP.UTF-8

再起動しないと変わらないのでサーバー再起動して確認。

Webサーバーを入れる

以下でインストール

sudo yum install -y httpd

確認する。

httpd -v
systemctl status httpd.service

停止している。設定を一箇所書き換える。

sudo vim /etc/httpd/conf/httpd.conf

以下のAllowOverrideをAllに書き換える。

/etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
   AllowOverride All

httpdを起動する。

sudo systemctl start httpd.service

自動で起動する設定を行う。

sudo systemctl enable httpd.service

URLを確認。

通信エラーとか出て見られない。

SELinux無効化

getenforce

で確認

Enforcing

になっている。

sudo vi /etc/selinux/config

設定ファイルを開いて、

SELINUX=disabled

に書き換える。

これもOSを再起動しないと変わらないので再起動する。

ファイヤウォール

systemctl status firewalld

で確認。

active (running)になっている。

sudo firewall-cmd --permanent --zone=public --add-service=http

success と出る。

sudo firewall-cmd --reload

success と出る。

再びURLを確認。

ドキュメントルート確認

デフォルトで表示されるページはアパッチの隠しページなので自前でドキュメントルートにファイルを置いてちゃんと表示されるか確認しておく。

cd /var/www/html/
vim index.html

PHPインストール

sudo yum install -y epel-release
sudo yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
ls /etc/yum.repos.d
yum list
sudo yum install -y --enablerepo=remi-php73 php php-gd php-intl php-json php-mysqlnd php-mbstring php-xml
php -v
php -m | grep -e intl -e mbstring -e SimpleXML

phpの設定ファイルを開く。

sudo vim /etc/php.ini

以下のようにtimezone設定追加

/etc/php.ini
date.timezone = Asia/Tokyo

phpインフォも確認しておく。

vim /var/www/html/info.php
/var/www/html/info.php
<?php
phpinfo();

ブラウザから確認。

データベース

インストール

sudo yum install mariadb mariadb-server
mysql --version

手動で起動する。

sudo systemctl start mariadb
systemctl status mariadb

rootのパスワードを設定する。

sudo mysql_secure_installation

自動起動する設定をしておく。

sudo systemctl enable mariadb

DB操作やってみる。

mysql -u root -p

DB設定しておく。

sudo vim /etc/my.cnf

以下を追加

/etc/my.cnf
[mysqld]
character-set-server=utf8

コンポーザー

インストールする。

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php

どこからでも使えるように設定。

sudo mv composer.phar /usr/local/bin/composer

動作確認。

composer

composer.json を用意していろいろインストール。

開発開始!