【サーバー移行大作戦! Part-4】PHPとデータベースを導入してみよう

こんにちはー、ニアです!

今回はWordPressに必要な、PHPとデータベースをインストールしていきます。

1. パッケージをアップデートする

その前に、「yum update」コマンドで、インストール済みのパッケージを最新版にアップデートします。

yum update

途中で、アップデート可能なパッケージをアップデートするかどうかを尋ねられますので、アップデートする時は「y」と入力し、Enterキーを押します。アップデートには少し時間がかかるので、気長に待ちます。

# 中略

Transaction Summary
====================================================================================================
Install      33 Package(s)
Upgrade     309 Package(s)

Total download size: 286 M
Is this ok [y/N]:y

2. PHPを導入しよう

2.1. PHPがインストールされているか確認する

パッケージのアップデートが終わったところで、PHPの導入から始めていきます。

まず、VPSにPHPがインストールされているかどうか確認するには、「php -version」のコマンドを実行します。

# php -v でもOKです。
php -version
PHP 5.3.3 (cli) (built: Feb  9 2016 10:36:17)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

2.2. PHPの動作チェックをする

次は、PHPファイルをWWWサーバーのルートディレクトリ(/var/www/html)に置いて、PHPが動作するかどうかチェックしてみます。

vi /var/www/html/phpinfo.php

PHPのバージョン情報を表示するには、phpinfo関数を呼び出します。

<?php
phpinfo();
?>

PHPファイルを作成したら、httpdを起動して、実際にブラウザでアクセスしてみます。

service httpd start
http://[ホスト名]

sv28

http://[ホスト名]/phpinfo.php

sv29

2.3. PHPをアップデートする

KUSANAGIで使用しているPHPのバージョンは5.6なので、PHPを5.6以降にアップデートしておきます。

まず、「rpm」コマンドで、yumリポジトリに「Remi」追加します。ちなみに、リポジトリのURLにあるreleaseの後の数字には、Cent OSのメジャーバージョンが入ります。

# rpm -ivh [リポジトリ名]
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

次に、yum installコマンドで、PHPをバージョン5.6にアップデートします。途中でダウンロード及びインストールするかどうかを尋ねられたら、yを入力してEnterキーを押し、インストールします。

# yum update [パッケージ名]
yum update php --enablerepo=remi-php56,remi

PHPをアップデートしたら、 httpdを再起動します。

service httpd restart
Stopping httpd:                                  [  OK  ]
Starting httpd:                                  [  OK  ]

httpdを再起動したら、2.2.で作成したphpinfoのページにアクセスし、PHP5.6にアップデートされたかどうか確認します。

sv30

3. データベースを導入する

3.1. MariaDBとMySQL

KUSANAGIで使用しているデータベースはMariaDBなので、MariaDBをインストールしていきたいと思います。

MariaDBはMySQLから派生した、オープンソースのデータベース管理システムなので、MySQLと互換性があります。

3.2. MySQLをアンインストールする

MySQLがインストールされている場合、まずはアンインストールします。「mysql –version」で、インストールされているMySQLのバージョンを確認します。

mysql --version
mysql Ver 14.14 Distrib 5.1.73, for Linux (x86_64) using readline 5.1

「yum remove」コマンドで、MySQLをアンインストールします。途中でパッケージをアンインストールするかどうか尋ねられたら、yを入力してEnterキーを押し、アンインストールします。

# yum remove [アンインストールするパッケージ名]
yum remove mysql-libs

3.3. MariaDBをインストールする

まず、viでMariaDBのリポジトリ設定ファイルを作成します。

vi /etc/yum.repos.d/mariadb.repo

baseurlはhttp://yum.mariadb.org/から環境に合わせて設定します。ここでは、Cent OS6(64ビット)用の最新版のバージョン10.2を指定しています。

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

リポジトリ設定ファイルを作成したら、yum installコマンドでMariaDBをインストールします。

yum install MariaDB-devel MariaDB-client MariaDB-server

インストールが完了したら、Maria DBを起動します。

/etc/init.d/mysql start
Starting MySQL. SUCCESS!

3.4. データベースを作成する

MariaDBをインストールしたら、データベースの初期設定を行います。

mysql_secure_installation

途中で、現在のrootユーザー(データベースでのrootユーザーです)のパスワードの入力が求められますが、そのままEnterキーを押します。

Enter current password for root (enter for none):
OK, successfully used password, moving on...

次に、rootユーザーの新しいパスワードを設定するかを尋ねられたら、Yと入力してEnterキーを押し、新しいパスワードを2回入力します。

Setting the root password ensures that nobody can log into the MariaDB<br>root user without the proper authorisation.
Set root password? [Y/n] Y
New password:[新しいパスワード]
Re-enter new password:[新しいパスワード(上と同じ)]
Password updated successfully!
Reloading privilege tables..
 ... Success!

次に、匿名ユーザーを削除するかどうかを尋ねられたら、Yと入力してEnterキーを押します。

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] Y
 ... Success!

次に、rootユーザーのリモートログインを禁止するかどうかを尋ねられたら、Yと入力してEnterキーを押します。

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? [Y/n] Y
 ... Success!

次に、テスト用のデータベースを削除するかどうかを尋ねられたら、Yと入力してEnterキーを押します。

By default, MariaDB 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? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

最後に、変更した情報を再読み込むかどうかを尋ねられたら、Yと入力してEnterキーを押します。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

これで、データベースの初期設定は完了です。

3.5. phpMyAdminをインストールする

前述の通り、MariaDBはMySQLと互換性があります。ということで、phpMyAdminをインストールしていきます。

yum install phpmyadmin --enablerepo=remi-php56, remi

インストールしたら、viで「phpMyAdmin.conf」を編集します。

vi /etc/httpd/conf.d/phpMyAdmin.conf
# phpMyAdmin - Web based MySQL browser written in php
#
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       Require ip 127.0.0.1
       Require ip ::1
     </RequireAny>
   </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 / RequireAny」要素の中に「Require ip [アクセスするPCのホスト名]」を、「IfModule !mod_authz_core.c」要素の中に「Allow from [アクセスするPCのホスト名]」を追加します。

# 「IfModule mod_authz_core.c / RequireAny」要素の中に追加します。
Require ip [アクセスするPCのホスト名]

# 「IfModule !mod_authz_core.c」要素の中に追加します。
Allow from [アクセスするPCのホスト名]

[アクセスするPCのホスト名]には、SSH接続した時に表示されるfromの後の文字列(例:192.168.xxx.yyy)が入ります(VPSのホスト名ではないですよ)。IPv4アドレスであれば、最後の4ビットの部分は省略しても大丈夫です。

設定したら、httpdを再起動して、phpMyAdminにアクセスしてみます。

http://[VPSのホスト名]/phpMyAdmin/

phpMyAdminのログインページにアクセスできたら、ユーザー名(root)とパスワードを入力して、ログインしてみます。

sv31

sv32

4. おわりに

今回は、WordPressに必要な環境の準備に、PHP5.6とMariaDB 10.2をインストールしていきました。

次回は、このブログをVPSにアップロードして、動作テストをしていきたいと思います。

[END]

参考サイト

サーバー移行大作戦の記事一覧

コメント

タイトルとURLをコピーしました