前回、VMware Workstation Playerを使用してWindows7上にCentOS7の仮想マシンを作成しWEBサーバーを構築する方法を紹介した。
![](https://www.onebizlife.com/wp-content/uploads/2018/05/linux_1527268363-500x250.png)
今回は、前回作成した仮想マシンにLAMP環境を構築してデータベースを使用したWEBアプリケーションを実行できるようにする。
LAMP環境とは?
LAMP環境とは、Linux + Apache + MySQL(MariaDB) + PHP(Perl,Python)で構築されたサーバー環境のことだ。
それぞれの頭文字をとってLAMP環境と呼称される。
具体的にはOS + WEBサーバー + データベースサーバー+スクリプト言語という構成だ。
- OS:Linux
- Webサーバ:Apache
- データベースサーバ:MySQL(MariaDB)
- スクリプト言語:PHP(Perl、Python)
LANP環境を構築すれば、データベースを使用したWEBアプリケーションを実行することができるようになる。
LAMP環境の構築方針
前回、Linux(CentOS)とWEBサーバー(Apache)までの環境は構築したので、後はデータベースとスクリプト言語を使えるようにしていく。
CentOS7では、データベースとしてMariaDBが公式にサポートされMySQLは含まれなくなったが、レンタルサーバーなどはまだMySQLが主流で、私が使用しているコアサーバーも2018年5月26日現在、MySQL5.7が使用されているので、データベースはMySQL5.7を導入することにする。
スクリプト言語はひとまずPHPを利用できるようにしておく。
CentOSのデフォルトではPHP5.4.16が入っているが、コアサーバーのPHPバージョンは7.1なのでPHPを7.1にアップデートする事にする。
MySQL5.7の導入
CentOS 7にMySQL5.7をインストールする。
インストール手順
mariadbの削除
mariadbがインストールされているかどうか以下のコマンドでチェック。
# rpm -qa | grep maria
「mariadb-libs-5.5.56-2.el7.x86_64」のような表示がでたらインストールされている。
mariadbとmysqlは通常では共存できないので、mariadbがインストールされていたら以下のコマンドで削除する。
# yum remove mariadb-libs
インストールの実行
YumリポジトリにMySQL公式リポジトリを追加する。
# yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
MySQL8.0リポジトリを無効化。
# yum-config-manager --disable mysql80-community
MySQL5.7リポジトリを有効化。
# yum-config-manager --enable mysql57-community
MySQL5.7をインストールする。
# yum install mysql-community-server
インストールしたバージョンを確認。5.7.22となっていて正常にインストールされている。
# mysqld --version mysqld Ver 5.7.22 for Linux on x86_64 (MySQL Community Server (GPL)
MySQLの初回起動及び仮パスワードの確認
インストールが完了したらMySQLを以下のコマンドで起動させる。
# systemctl start mysqld
MySQL5.7では、初回起動した時にrootユーザーの初期パスワードが設定され、 /var/log/mysqld.logに出力されるようになっている。
まずはこのログに出力されているrootの初期パスワードを確認しよう。
# cat /var/log/mysqld.log | grep password
以下のように出力されるはずだ。
[Note] A temporary password is generated for root@localhost: s-_:UwWZi3+g
今回の場合で言うと「s-_:UwWZi3+g」が初期パスワードだ。
mysql_secure_installationの実行
mysql_secure_installationを実行すると最低限のセキュリティ設定を行うことができる。
- root ユーザーのパスワードを設定
- リモートホストからrootアクセス不可にする。
- anonymous ユーザーアカウントの削除
- testデータベースの削除
- Validate Passwordプラグインでパスワードの強さをチェックが行われる
ローカルで使用するMySQLなのでここまでする必要はないのだが、今回このmysql_secure_installationを使って設定してみた。
MySQL5.7.8以降は、Validate Passwordプラグインがデフォルトでインストールされていて、新しいパスワード入力時に自動的にポリシーチェックが行われる。
デフォルトのポリシーでは、パスワードに8文字以上+大文字小文字+数値+記号を含める必要があるので注意してほしい。
ポリシーを満たしていないパスワードを入力すると以下のメッセージで怒られる。
では、以下のコマンドを入力して設定を開始。
# mysql_secure_installation
以下、赤文字で書いてある通り入力すれば、セキュアな設定を行うことができる。
Securing the MySQL server deployment. Enter password for user root:初期パスワードを入力する The existing password for the user account root has expired. Please set a new password. New password:新しいパスワードを入力する Re-enter new password:再度パスワードを入力する The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : n ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done!
文字コードの設定
/etc/my.cnfを編集してMySQLの文字コードを設定を行う。
まずは設定ファイルのバックアップ
# cp /etc/my.cnf /etc/my.cnf.org
Vimでmy.cnfを開く。
# vim /etc/my.cnf[mysqld]セクションに以下を追記する。
character-set-server = utf8
追記したら保存してMySQLを再起動する。
# systemctl restart mysqld
MySQLの自動起動設定
MySQLをサーバー起動時に自動的に起動するようにする。
# systemctl enable mysqld.service
PHP7.1へのアップデート
CentOSにインストールされているPHP5.4を7.1にアップデートする。
既存のPHPをアンインストール
以下のコマンドを使用してPHP5.4をアンインストールする。
# yum remove php*
リポジトリ情報のインストール
EPELリポジトリ
以下のコマンドを入力しEPELリポジトリをインストールする。
# yum -y install epel-release
Remiリポジトリ
以下のコマンドを入力しRemiリポジトリをインストールする。
# yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
remi-php71 リポジトリを有効化。
# yum-config-manager --enable remi-php71
PHP7.1のインストール
必要な拡張モジュールとともにphpをインストールする。
# yum install php php-mbstring php-intl php-mysql php-gd php-opcache php-xml
PHPの動作確認
まずApacheを再起動する。
# systemctl restart httpd
ドキュメントルート直下にPHP確認用ファイルを作成
# vim /var/www/html/phpinfo.php
以下の内容を記述して保存する。
<?php phpinfo(); ?>
WEBブラウザから「サーバーのIPアドレス/phpinfo.php」にアクセスし以下のような画面が表示されたらPHPのインストールは完了だ。
PHPバージョンは7.1.18となっている。
![](https://www.onebizlife.com/wp-content/uploads/2018/05/CentOS-Lamp1.png)
PHPの初期設定
PHPのインストールが完了したらPHPの設定を行っておこう。
PHPの設定は、「/etc/php.ini」を編集して行う。
まずは、設定ファイルをバックアップ
# cp /etc/php.ini /etc/php.ini.org
Vimで/etc/php.iniをオープン
# vim /etc/php.ini
変更した項目は以下の通り。とりあえず最低限の項目だけ変更した。
date.timezone="Asia/Tokyo"
expose_php = Off
mbstring.language = Japanese
mbstring.encoding_translation = On
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII
mbstring.substitute_character = none
変更が完了したら、ファイルを保存し、Apatcheを再起動する。
# systemctl restart httpd
以上でPHPの設定は完了となる。
phpMyAdminの導入
MySQLデータベースをWebブラウザからコントロールできるphpMyAdminを使えるようにする。
phpMyAdminのインストール
CentOS7、PHP7.1環境でのphpMyAdminのインストールについて、以下の記事を参考にさせていただいた。
https://qiita.com/100/items/8e9d9540845cc23e6111
以下のコマンドを入力してremiリポジトリ定義ファイルを編集する
# vim /etc/yum.repos.d/remi.rep[remi]セクションの最後に以下の一文を追記し保存する。
includepkgs=phpMyAdmin php-phpmyadmin-sql-parser php-phpmyadmin-motranslator php-phpseclib php-google-recaptcha php-twig-extensions php-symfony-polyfill
remi リポジトリを有効する。
# yum-config-manager --enable remi
phpMyAdminをインストール
# yum install phpMyAdmin
phpMyAdminの設定
phpMyAdminインストール直後は、ローカルPCからしかアクセスできないよう制限されている。
他のPCからアクセスできるように「/etc/httpd/conf.d/phpMyAdmin.conf」を編集してアクセス制限を解除する。
まず設定ファイルをバックアップ
# cp /etc/httpd/conf.d/phpMyAdmin.conf /etc/httpd/conf.d/phpMyAdmin.conf.org
Vimで設定ファイルを開く
# vim /etc/httpd/conf.d/phpMyAdmin.conf
# Apache 2.4の下にある「Require local」の部分を
<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8
<IfModule mod_authz_core.c>
# Apache 2.4
Require local
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2
Order Deny,Allow
Deny from All
Allow from 127.0.0.1
Allow from ::1
</IfModule>
</Directory>
以下のように変更すればアクセス制限がかからなくなる。
<IfModule mod_authz_core.c>
# Apache 2.4
Require all granted
</IfModule>
以下のような記述が可能なので自身の運用方針にあったものを使用しよう。できればアクセス可能なPCは制限しておいた方がよい。
Require local | ローカルPCのみアクセス可能 |
Require all granted | アクセス制限なし |
Require ip IPアドレス | IPアドレスで指定したPCのみアクセス可能 |
Require ip 192.168.0.0/24 | 指定のIP範囲に属するPCのみアクセス可能 |
変更したらファイルを保存し、Apacheを再起動させる。
# systemctl restart httpd
設定後、WEBブラウザで「サーバーIPアドレス/phpmyadmin/」にアクセスすれば、以下のようにphpMyAdminが表示される。
MySQLインストール時に設定したrootアカウントでログインしてみた。
![](https://www.onebizlife.com/wp-content/uploads/2018/05/CentOS-Lamp3.png)
これでいつでもphpMyAdminからMySQLデータベースを操作することができる。
まとめ
今回は、これまで構築してきたWEBサーバーでMySQLとPHPを使えるように設定した。
LAMP環境が完成したことで、データベースを活用したWEBアプリケーションを動作させることが可能になった。
次回、FTPサーバーを構築してWEBサーバーにファイルを転送できるようにして開発用WEBサーバーの構築を完了させたい。
コメント