文系プログラマの勉強ノート

スマホアプリ開発やデザインなどについて勉強したことをまとめています

Dockerで PHP+MySQLの環境構築

2018年になりました。今年はスマホアプリだけでなくWeb開発にも挑戦していきたいと思います。

ということで手始めに『PHP+MySQLマスターブック』でPHPを勉強することにしました。
book.mynavi.jp

PHP5.4&MySQL5.5対応と少しバージョンが古いですが、ログイン機能を持つ会員管理システムが作れる、Smarty(テンプレートエンジン)にも触れているということで、一冊終わる頃には実用的な知識が身につきそうです。

同書ではXAMMPの使用を推奨していますが、今回はDockerを使って環境構築しました。以下、その手順です。

Dockerとは

Dockerについては以下のサイトがわかりやすいです。本記事ではDockerのインストールは済んでいることを前提とします。
employment.en-japan.com

構築する環境

  • CentOS 7.7(2018.1時点の最新版)
  • Apache 2.4.3(2018.1時点の最新版)
  • PHP 5.4
  • MySQL 5.5

1.コンテナの準備

CentOS最新版のイメージを取得します。

$ docker pull centos

centosイメージからコンテナを作成・起動します。

$ docker run --privileged -it -d -p 8080:80 -p 3306:3306 -v "/Users/yourname/htdocs/:/var/www/html/" --name MySqlTest centos /sbin/init

色々オプションがついていますが、それぞれの意味は次の通りです。

  • --privileged:特権モードを指定。systemctlコマンドを使用するために付加
  • -it:コンテナ内で操作できる
  • -d :コンテナの実行をバックグラウンドで行う
  • -p 8080:80:ローカルマシンの8080番ポートとコンテナの80番ポートを紐付ける。これによりローカルマシンのWebブラウザから"http://localhost:8080/"でWebページにアクセス可能となる
  • -p 3306:3306:同上。3306番ポートはMySQLのポート番号
  • -v "/Users/yourname/htdocs/:/var/www/html/":ローカルマシンの任意のディレクトリ(htdocs)とコンテナのドキュメントルート(/var/www/html/)を紐付ける
  • --name MySqlTest:コンテナの名前
  • /sbin/init :systemctlコマンドによるOS起動時のサービス自動起動のために付加

以下のコマンドでMySqlTestが表示されれば、正しくコンテナを起動できています。

$ docker ps

作成したコンテナにログインします。

$ docker exec -it MySqlTest /bin/bash

タイムゾーンを日本時間に変更します。

# timedatectl set-timezone Asia/Tokyo
# timedatectl

2.Apacheのインストール

Apache最新版をインストールします。

# yum install -y httpd

httpd.confを編集します。今回は開発用に必要な部分しか設定していません。
念のためオリジナルを複製しておきます。

# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
# vi /etc/httpd/conf/httpd.conf

<Directory "/var/www/html">
    # ファイル一覧を非表示
    Options -Indexes +FollowSymLinks

    # .htaccessを利用するため
    AllowOverride All

    Require all granted
</Directory>

<IfModule dir_module>
    # index.phpを省略可
    DirectoryIndex index.php index.html
</IfModule>

Apacheを起動します。また、OS再起動時に自動起動するように設定しておきます。

# systemctl start httpd.service
# systemctl enable httpd.service

webブラウザから"http://localhost:8080"にアクセスし、Apacheのデフォルトページが表示されればOKです。

3.PHPのインストール

PHP5.4とその他必要なライブラリをインストールします。
CentOS7のPHPは標準で5.4なので特にバージョンは指定しなくて大丈夫です。

# yum install -y php php-mbstring php-mysql php-mcrypt

php.iniを編集します。

# cp /etc/php.ini /etc/php.ini.bak
# vi /etc/php.ini

default_charset = "UTF-8"
date.timezone = "Asia/Tokyo"

mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = UTF-8
mbstring.http_output = pass
mbstring.encoding_translation = On
mbstring.substitute_character = none;

php.iniを保存したら、変更を反映するためApacheを再起動します。

# systemctl restart httpd.service

以下のコードをphpinfo.phpという名前でドキュメントルート(本記事だと"/Users/yourname/htdocs/")に保存します。

<?php
phpinfo();
?>

webブラウザから"http://localhost:8080/phpinfo.php"にアクセスし、PHPに関する情報が表示されればOKです。

4.MySQLのインストール

CentOS7には標準でMariaDBがインストールされていますので、競合を防止するために削除します。

# yum remove -y mariadb-libs
# rm -rf /var/lib/mysql/

リポジトリを追加します。

# yum install -y http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

インストールしたリポジトリを見るとMySQL5.7がenableになっているので、5.5に変更します。

# 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 - S 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 - S disabled
mysql-connectors-community/x86_64  MySQL Connectors Community      enabled:   42
mysql-connectors-community-source  MySQL Connectors Community - So disabled
mysql-tools-community/x86_64       MySQL Tools Community           enabled:   55
mysql-tools-community-source       MySQL Tools Community - Source  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 - So disabled
mysql56-community/x86_64           MySQL 5.6 Community Server      disabled
mysql56-community-source           MySQL 5.6 Community Server - So disabled
mysql57-community/x86_64           MySQL 5.7 Community Server      enabled:  227
mysql57-community-source           MySQL 5.7 Community Server - So disabled
mysql80-community/x86_64           MySQL 8.0 Community Server      disabled
mysql80-community-source           MySQL 8.0 Community Server - So disabled
# yum install yum-utils
# yum-config-manager --disable mysql57-community
# yum-config-manager --enable mysql55-community

これでようやくMySQL5.5をインストールできます。

# yum install -y mysql mysql-devel mysql-server

インストールが終わったら、MySQLのバージョンが5.5になっているのを確認します。

# mysqld --version

MySQLを起動して、自動起動を設定します。

# systemctl start mysqld.service
# systemctl enable mysqld.service

rootのパスワード設定などの初期設定を行います。

# mysql_secure_installation
Enter current password for root (enter for none): [そのままEnter]

// rootのパスワード設定
Set root password? [Y/n] Y
New password: [パスワードを入力]
Re-enter new password:[パスワードを再入力]
Password updated successfully!
Reloading privilege tables..
 ... Success!

// 匿名ユーザー削除
Remove anonymous users? [Y/n] y
 ... Success!

// リモートホストからのrootログイン禁止
Disallow root login remotely? [Y/n] y
 ... Success!

// テスト用データベース削除
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

// ユーザー権限が保存されているテーブルをリロード
Reload privilege tables now? [Y/n] y
 ... Success!

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

my.cnfを編集します。

# cp /etc/my.cnf /etc/my.cnf.bak
# vi /etc/my.cnf

[mysqld]
# mysqldセクションの末尾に追加
character-set-server = utf8

# clientセクションごとファイルの末尾に追加
[client]
default-character-set = utf8

MySQLを再起動します。

# systemctl restart mysqld.service

これで環境構築完了です!