VagrantCloudで用意されている centos/7 イメージ ( https://app.vagrantup.com/centos/boxes/7 )を使って仮想環境を構築し、共有フォルダ上にソースコードを置いてローカルで開発、ゲストでプログラムを実行、という方針でする場合。
Vagrant
プラグインを追加
$ vagrant plugin install vagrant-vbguest
Vagrantファイルの作成
$ cd /PATH/TO/WORK/
$ vagrant init centos/7
ポートの設定
- http://localhost:1080/ でゲストのhttpdにアクセスするようにする。
- ホスト環境でもゲスト環境でもunitテストを同じconfigファイルで実行したいので、MySQLのポートを13306で共通させておく(一般的ではない作法なので、これは必須でない)
(snip)
config.vm.network "forwarded_port", guest: 80, host: 1080
config.vm.network "forwarded_port", guest: 13306, host: 13306
(snip)
共有フォルダの設定
※ ゲストOSのhttpdインストールをするまでは、httpdアカウントがないためエラーになる。
- ホストで変更したファイルを即時ゲストに反映させるため、typeはvirtualboxにする
(snip)
config.vm.synced_folder "./", "/vagrant_data", type:"virtualbox"
(snip)
ゲストマシン全体の設定
タイムゾーン変更
# 現在のタイムゾーンを確認
[root@localhost ~]# date
Sun Jun 30 09:31:51 UTC 2019
# 変更
[root@localhost ~]# timedatectl set-timezone Asia/Tokyo
# JSTに変わっていることを確認
[root@localhost ~]# date
Sun Jun 30 18:32:25 JST 2019
SELinuxの設定
vagrant共有フォルダにあるファイルをシンボリックリンクで /var/www/html に配置するとhttpdがForbiddenエラーになるので、SELinuxをoffにする。
( 参考 SELinuxを有効にしたまま、ドキュメントルートを仮想マシンの共有フォルダへのシンボリックリンクへ変更するする方法 )
setenforce 0
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
httpdのインストール
パッケージをインストール
# yum -y install httpd
設定
実行ユーザーをvagrantにする
共有フォルダの所有者はvagrantになるので、ログファイルの書き込みができるよう、実行ユーザーをvagrantに変更する
(snip)
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User vagrant
Group vagrant
(snip)
AllowOverrideを変更
FuelPHPが使っている mod_rewriteを動かすため、AllowOverrideをAllにする
(snip)
# Further relax access to the default document root:
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
(snip)
ファイルキャッシュをoffにする
ホストで変更したファイルが即時反映されるよう、httpdのキャッシュをきる
( /var/www/html に設定しても良いが、http://SERVER/ でテストしたいプロジェクトが複数あるときには、/var/www/html_project1/ のようなディレクトリを作って、ポート番号で分けることがあるので、/var/www 単位で設定している )
(snip)
#
# Relax access to content within /var/www.
#
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
EnableMMAP Off
EnableSendfile Off
</Directory>
(snip)
起動して動作確認
# systemctl start httpd
ホストマシンのブラウザで http://localhost:1080/ にアクセスして、テストページが表示されることを確認
常時起動の設定
# systemctl enable httpd
PHPのインストール
remiを使ってphp7.2をインストールする場合。パッケージは開発に必要なものだけで良い。
( 参考 : https://rpms.remirepo.net/wizard/ )
# yum -y install epel-release
# yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
# yum -y install --disablerepo=* --enablerepo=remi,remi-php72 php php-pdo php-mysql php-dom php-mbistring php-ldap php-mbstring
remiを使ってphp5.6をインストールする場合。パッケージは開発に必要なものだけで良い。
# yum -y install epel-release
# yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
# yum -y install --disablerepo=* --enablerepo=remi,remi-php56 php php-pdo php-mysql php-dom php-mbistring php-ldap php-mbstring
MySQLのインストール
MySQL 5.7をインストールする場合。
( 参考 : https://dev.mysql.com/downloads/repo/yum/ )
yumレポジトリの追加
# yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
yumレポジトリの確認
# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 108
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 90
mysql-tools-community-source MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 113
mysql80-community-source MySQL 8.0 Community Server - disabled
MySQL57のレポジトリからmysqlをインストール
# yum -y install --enablerepo=mysql57-community --disablerepo=mysql80-community mysql-community-server
インストールされたパッケージを確認
# rpm -qa | grep mysql
php-mysqlnd-7.2.19-2.el7.remi.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-common-5.7.26-1.el7.x86_64
mysql-community-client-5.7.26-1.el7.x86_64
mysql-community-libs-compat-5.7.26-1.el7.x86_64
mysql-community-libs-5.7.26-1.el7.x86_64
mysql-community-server-5.7.26-1.el7.x86_64
rootのパスワードを設定
開発環境なので、rootアカウントアクセスでパスワードを不要にする。
(snip)
skip-grant-tables
(snip)
常時起動
# systemctl enable mysqld