以前、VagrantとVirtualBoxを使用し、LAMP環境の構築の仕方を紹介しました。
今回の記事では、VagrantとVirtualBoxを使用し、LEMP環境(CentOS7.4/PHP7.2/MySQL5.7/Nginx1.12.2)の構築の仕方について紹介しようと思います。
Contents
バージョン
- ホスト (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
- Nginx 1.12.2
VagrantとVirtualBoxのインストール
OSに合ったVagrantをダウンロードしてインストールします。
Download Vagrant
OSに合ったVirtualBoxをダウンロードしてインストールします。
Download VirtualBox
環境構築
作業用ディレクトリの構築
mkdir lnmp20180625 cd lnmp20180625/
今回は、作業用のディレクトリの名前をlnmp20180625としました。任意で作業用ディレクトリを決めてください。
Vagrantfile作成
vagrant init "bento/centos-7.4"
Vagrantfileの編集
config.vm.network "private_network", ip: "192.168.33.10"
ホストから仮想マシンにアクセスするためのIPを設定するため、35行目にある上記の行のコメントアウトを外します。
Vagrantの起動
vagrant up
これで、CentOS7が入った仮想環境が構築されます。
仮想環境にLEMP環境を構築する
作成したVMの環境にLEMP環境を構築していきます。
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
Nginxのインストール
インストール
yum --enablerepo=epel install nginx
Nginxの起動
systemctl start nginx
Nginxのバージョンの確認
nginx -v nginx version: nginx/1.12.2
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 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-fpmの設定
NginxにてPHPを使用出来るようにするためにphp-fpmをインストールする必要があります。今回は、PHPモジュールのインストールですでにインストールしました。
次に、php-fpmの設定を行います。
vi /etc/php-fpm.d/www.conf ;変更 user = nginx group = nginx ;変更と追加 listen = /var/run/php-fpm.sock listen.owner = nginx listen.group = nginx ;コメントアウト ;listen = 127.0.0.1:9000
php-fpmの起動
systemctl start php-fpm
nginx.confの編集
vi /etc/nginx/nginx.conf server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; #ここから location ~ .php$ { fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; } #ここまでを追記 }
serverの部分にlocationの部分を追記します。
nginxの再起動
systemctl restart nginx
php.iniの設定
初期ファイルのバックアップ
cp /etc/php.ini /etc/php.ini.bk
必要に応じて設定してください。
動作の確認
vi /usr/share/nginx/html/phpinfo.php <?php phpinfo(); ?>
http://192.168.33.10/phpinfo.phpに接続します。phpinfoの表示がされれば、成功です。
今回の記事では、VagrantとVirtualBoxを使用して、LEMPの仮想環境を構築してみました。
VagrantとVirtualBoxを使用していますが、CentOS7環境下であれば、同様の手順でLEMPの環境を構築できると思います。
LEMPの仮想環境を構築しようと考えている方は参考にしてください。