PHPの勉強をしたいけど、どうやって開発環境を作ったらよいのかなぁと悩んでいる方多いと多いと思います。
そんな時は、VirtualBox をインストールして、VM 上に環境を作るのがおすすめです。
バージョン
- ホスト (Mac)
- MacOS High Sierra 10.13.5
- VirtualBox 5.2.12
- Vagrant 2.1.1
- ゲストVM (CentOS 7)
- PHP 7.2.7
- MySQL 5.7.22
- Apache 2.4.6
VagrantとVirtualBoxのインストール
OSに合ったVagrantをダウンロードしてインストールします。
Download Vagrant
OSに合ったVirtualBoxをダウンロードしてインストールします。
Download VirtualBox
環境構築
作業用ディレクトリの構築
mkdir lamp20180617 cd lamp20180617/
今回は、作業用のディレクトリの名前をlamp20180617としました。任意で作業用ディレクトリを決めてください。
Vagrantfile作成
vagrant init "bento/centos-7.4"
Vagrantfileの編集
config.vm.network "private_network", ip: "192.168.33.10"
ホストから仮想マシンにアクセスするためのIPを設定するため、35行目にある下記の行のコメントアウトを外します。
Vagrantの起動
vagrant up
これで、CentOS7が入った仮想環境が構築されます。
仮想環境にLAMP環境を構築する
作成したVMの環境にLAMP環境を構築していきます。
vagrant ssh
で仮想環境へ接続します。
rootユーザーになる
sudoをつけるのは面倒なので、rootユーザーになります。
sudo -i
リポジトリの設定
インストールに使うリポジトリの設定をします。
CentOSには、公式のリポジトリが用意されていて、通常のyumコマンドで利用可能ですが、保守的で最新のソフトウェアやバージョン更新が行われないので、epelやremiというパッケージをインストールして、各リポジトリにある最新のソフトウェアを利用します。
epelパッケージのインストール
yum install epel-release.noarch
[epel]の部分のenable=1を、enable=0 に書き換える
vi /etc/yum.repos.d/epel.repo [epel] name=Extra Packages for Enterprise Linux 7 - $basearch #baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch failovermethod=priority enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
remiのインストール
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
[remi]の部分のenable=1を、enable=0 に書き換える
vi /etc/yum.repos.d/remi.repo [remi] name=Remi's RPM repository for Enterprise Linux 7 - $basearch #baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/ #mirrorlist=https://rpms.remirepo.net/enterprise/7/remi/httpsmirror mirrorlist=http://cdn.remirepo.net/enterprise/7/remi/mirror enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
apacheのインストール
インストール
yum install httpd
apacheの起動
systemctl start httpd.service
MySQLのインストール
CentOS7では、mariaDB(MySQL互換のDB)がデフォルトでインストールされている場合があるので、MySQLと競合を起こさないように削除します。
mariaDB、既存MySQL削除
yum remove mariadb-libs yum remove mysql*
MySQL 5.7のインストール
yum install http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm yum -y install mysql-community-server
MySQLのバージョン確認
mysqld --version mysqld Ver 5.7.22 for Linux on x86_64 (MySQL Community Server (GPL))
MySQLの起動
systemctl start mysqld.service
rootユーザーの初期パスワード
MySQL5.7では、初回起動と同時にrootユーザーにランダム文字列のパスワードが設定され、ログに出力されます。
cat /var/log/mysqld.log | grep 'password is generated' 2018-06-23T02:05:46.201038Z 1 [Note] A temporary password is generated for root@localhost: *hppN2erqA>e
MySQL 5.7の初期設定
mysql_secure_installationコマンドで設定していきます。
※新しいルートのパスワードは、大文字、小文字、数字、記号の全てが入っている必要あります。
$ mysql_secure_installation Enter password for user root: //ログに出力されていた初期パスワードを入力 Set root password? //ルートのパスワードを変更しますか? Yでパスワード設定 Remove anonymous users? //誰でもログイン可能になっているが消しますか? Yで消す Disallow root login remotely? //リモートからrootログインを許可しませんか? Yでしない Remove test database and access to it? //テストデータベース消していいですか? Yで消す Reload privilege tables now? //設定をすぐに反映しますか? Yで反映 All done!
MySQL接続確認
mysql -u root -p
my.cnfの設定
・ストレージエンジンを、InnoDBに設定
・データファイル・ログファイルをテーブルごとに設定
・文字コードをutf8に設定する
変更前の状態
mysql> show variables like 'char%' ; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.01 sec)
my.cnfに追記し、MySQLを再起動(systemctl restart mysqld)します。
vi /etc/my.cnf [mysqld] default-storage-engine=InnoDB innodb_file_per_table character-set-server = utf8 collation-server = utf8_general_ci [mysql] default-character-set = utf8 [client] default-character-set = utf8
変更後の状態
mysql> show variables like 'char%' ; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.01 sec)
PHP7.2のインストール
libmcryptのインストール
mcryptのインストールに必要なので先にインストールしておく。
yum --enablerepo=epel install libmcrypt -y
PHP 7.2のインストール
yum install --enablerepo=epel,remi,remi-php72 php
PHPモジュールのインストール
yum install --enablerepo=remi,remi-php72 php-mysqlnd yum install --enablerepo=remi,remi-php72 php-mbstring yum install --enablerepo=remi,remi-php72 php-gd yum install --enablerepo=remi,remi-php72 php-xml yum install --enablerepo=remi,remi-php72 php-xmlrpc yum install --enablerepo=remi,remi-php72 php-pecl-mcrypt yum install --enablerepo=remi,remi-php72 php-fpm yum install --enablerepo=remi,remi-php72 php-opcache yum install --enablerepo=remi,remi-php72 php-apcu yum install --enablerepo=remi,remi-php72 php-zip yum install --enablerepo=remi,remi-php72 unzip yum install --enablerepo=remi,remi-php72 php-pear
PHPのバージョンの確認
php -v PHP 7.2.7 (cli) (built: Jun 20 2018 08:21:26) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.7, Copyright (c) 1999-2018, by Zend Technologies
php.iniの設定
初期ファイルのバックアップ
cp /etc/php.ini /etc/php.ini.bk
必要に応じて設定してください。
動作の確認
vi /var/www/html/phpinfo.php <?php phpinfo(); ?> systemctl restart httpd
http://192.168.33.10/phpinfo.phpに接続します。phpinfoの表示がされれば、成功です。
今回の記事では、VagrantとVirtualBoxを使用して、LAMPの仮想環境を構築してみました。
LAMPの仮想環境を構築しようと考えている方は参考にしてください。