Memorandum

普段の生活の覚え書き。主に技術録

PHPバージョンアップ

新テーマ移行

WordPressに新しいテーマを入れようと思い、下記のテーマを検討していました。

https://wp-cocoon.com/

すると、そのテーマではPHP7以上を推奨しており、自分のVPSサーバでphpのバージョンを確認しました。

$ php -v
PHP 5.4.16 (cli) (built: Apr 12 2018 19:02:01)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

phpのバージョンは5.4.16でした。これはCentOS7の標準リポジトリPHPのバージョンと同じなので、特に今までPHPに関しては新しく追加したりしてこなかったみたいです。

phpにもOSと同じようにサポート期間が設定されており、最新のバージョンほどサポート期限が先に設定されています。

バージョン 初回リリース アクティブサポート
7.2 2017/11/30 2019/11/30
7.1 2016/12/01 2018/12/01
7.0 2015/12/03 2017/12/03
5.6 2014/08/28 2017/01/19
5.5 2013/06/20 2015/07/10
5.4 2012/03/01 2014/09/14

私が使用しているphp5.4は既にサポート期限が過ぎていますが、CentOS7の標準リポジトリPHPはOSサポート終了日までサポートがあるようです。 (ちなみにCentOS7のサポート終了日は2024/06/30)

また、PHPのバージョンが上がるほど処理能力が高く、さらに安全性も高まりますので導入しているソフトウェアなどで問題がなければ最新バージョンを入れていったほうがいいです。

前提条件

ではPHPバージョンアップにあたって私の環境を確認してみます。

$ cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

$ httpd -version
Server version: Apache/2.4.6 (CentOS)

CentOSが7.5、apacheが2.4.6というのが分かりました。

下記コマンドによるとPHPの最新バージョンは7.3らしいです。しかし公式?のページhttp://www.php.netによるとまだ7.3はβ版らしいので、安定版の7.2をインストールします。

$ rpm -ql remi-release
/etc/pki/rpm-gpg/RPM-GPG-KEY-remi
/etc/pki/rpm-gpg/RPM-GPG-KEY-remi2017
/etc/pki/rpm-gpg/RPM-GPG-KEY-remi2018
/etc/yum.repos.d/remi-glpi91.repo
/etc/yum.repos.d/remi-glpi92.repo
/etc/yum.repos.d/remi-glpi93.repo
/etc/yum.repos.d/remi-php54.repo
/etc/yum.repos.d/remi-php70.repo
/etc/yum.repos.d/remi-php71.repo
/etc/yum.repos.d/remi-php72.repo
/etc/yum.repos.d/remi-php73.repo
/etc/yum.repos.d/remi-safe.repo
/etc/yum.repos.d/remi.repo

事前準備

Yumリポジトリ追加を行います。EPELとRemiリポジトリを下記コマンドで追加します。 リポジトリを追加することで、yumでインストールできるパッケージやバージョン、そしてダウンロード元が増えます。

# EPEL
$ yum install epel-release
# Remi
$ rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

現在インストールされているPHPバージョンの確認をします。

$ rpm -qa | grep php
php-mysql-5.4.16-45.el7.x86_64
php-cli-5.4.16-45.el7.x86_64
php-common-5.4.16-45.el7.x86_64
php-gd-5.4.16-45.el7.x86_64
php-xml-5.4.16-45.el7.x86_64
php-pdo-5.4.16-45.el7.x86_64
php-mbstring-5.4.16-45.el7.x86_64
php-5.4.16-45.el7.x86_64

PHPのバージョンは5.4.16でした。 インストールされているPHPとその他拡張機能を覚えておいて、消しておきます。 (削除しなくてもアップデートされるみたいですが、念の為)

PHP7.2をインストール

それでは最新のPHPのインストール作業に入ります。 まず導入するPHPの詳細を確認します。

$ yum info --enablerepo=remi,remi-php72 php

利用可能なパッケージ
名前                : php
アーキテクチャー    : x86_64
バージョン          : 7.2.8
リリース            : 1.el7.remi
容量                : 3.2 M
リポジトリー        : remi-php72
要約                : PHP scripting language for creating dynamic web sites
URL                 : http://www.php.net/
ライセンス          : PHP and Zend and BSD and MIT and ASL 1.0
説明                : PHP is an HTML-embedded scripting language. PHP attempts
                   : to make it easy for developers to write dynamically
                   : generated web pages. PHP also offers built-in database
                   : integration for several commercial and non-commercial
                   : database management systems, so writing a database-enabled
                   : webpage with PHP is fairly simple. The most common use of
                   : PHP coding is probably as a replacement for CGI scripts.
                   :
                   : The php package contains the module (often referred to as
                   : mod_php) which adds support for the PHP language to Apache
                   : HTTP Server.

最新のバージョン7.2.8が利用できるみたいです。 確認ができたのでインストールを行います。

$ yum install --enablerepo=remi,remi-php72 php

================================================================================
Package          アーキテクチャー
                              バージョン                リポジトリー      容量
================================================================================
インストール中:
php              x86_64       7.2.8-1.el7.remi          remi-php72       3.2 M
依存性関連でのインストールをします:
libargon2        x86_64       20161029-2.el7            epel              23 k
php-cli          x86_64       7.2.8-1.el7.remi          remi-php72       4.8 M
php-common       x86_64       7.2.8-1.el7.remi          remi-php72       1.1 M
php-json         x86_64       7.2.8-1.el7.remi          remi-php72        62 k

トランザクションの要約
================================================================================
インストール  1 パッケージ (+4 個の依存関係のパッケージ)

上記のパッケージがインストールできました。 次に、rpm -qa | grep phpで確認した不足しているパッケージのインストールを行います。

$ yum install --enablerepo=remi --enablerepo=remi-php72 php php php-opcache php-devel php-mbstring php-mysqlnd php-gd php-xml php-mbstring php-pdo
================================================================================
Package                    Arch      Version               Repository     Size
================================================================================
Installing:
php-devel                  x86_64    7.2.8-1.el7.remi      remi-php72    1.2 M
php-gd                     x86_64    7.2.8-1.el7.remi      remi-php72     76 k
php-mbstring               x86_64    7.2.8-1.el7.remi      remi-php72    621 k
php-mysqlnd                x86_64    7.2.8-1.el7.remi      remi-php72    233 k
php-opcache                x86_64    7.2.8-1.el7.remi      remi-php72    280 k
php-pdo                    x86_64    7.2.8-1.el7.remi      remi-php72    124 k
php-xml                    x86_64    7.2.8-1.el7.remi      remi-php72    204 k

これで不足しているパッケージのインストールも完了しました。 最後にsystemctl restart httpd.serviceapacheを再起動してwordpressが動いていることを確認できたので完了です。

おまけ

phpのバージョンを上げると、処理性能の向上が期待できるというのでバージョンアップ前と後にabテスト(apache bench)を行ってみました。

バージョンアップ前(php5.4)

$ ab -n 10 -c 10 https://memoran.net/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking memoran.net (be patient).....done


Server Software:        Apache/2.4.6
Server Hostname:        memoran.net
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128

Document Path:          /
Document Length:        62819 bytes

Concurrency Level:      10
Time taken for tests:   4.587 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      631440 bytes
HTML transferred:       628190 bytes
Requests per second:    2.18 [#/sec] (mean)
Time per request:       4587.222 [ms] (mean)
Time per request:       458.722 [ms] (mean, across all concurrent requests)
Transfer rate:          134.43 [Kbytes/sec] received

Connection Times (ms)
             min  mean[+/-sd] median   max
Connect:        4   17   4.6     18      20
Processing:  2649 3432 298.6   3590    3647
Waiting:      917 1984 385.2   2121    2260
Total:       2653 3449 302.7   3608    3665

Percentage of the requests served within a certain time (ms)
 50%   3608
 66%   3610
 75%   3654
 80%   3660
 90%   3665
 95%   3665
 98%   3665
 99%   3665
100%   3665 (longest request)

あまり詳しい見方は分からないのですが、Requests per second: 2.18 [#/sec] (mean)を見ると一秒間に2.18のリクエストを受けられる性能みたいです。 次にバージョンアップ後も同様に行いました。

バージョンアップ後(php7.2)

$ ab -n 10 -c 10 https://memoran.net/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking memoran.net (be patient).....done


Server Software:        Apache/2.4.6
Server Hostname:        memoran.net
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128

Document Path:          /
Document Length:        62989 bytes

Concurrency Level:      10
Time taken for tests:   1.482 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      633120 bytes
HTML transferred:       629890 bytes
Requests per second:    6.75 [#/sec] (mean)
Time per request:       1481.672 [ms] (mean)
Time per request:       148.167 [ms] (mean, across all concurrent requests)
Transfer rate:          417.29 [Kbytes/sec] received

Connection Times (ms)
             min  mean[+/-sd] median   max
Connect:        5  159 291.0     33     821
Processing:   347  820 207.7    913     947
Waiting:      201  336  52.9    354     384
Total:        871  979  88.0    953    1168

Percentage of the requests served within a certain time (ms)
 50%    953
 66%    958
 75%    965
 80%   1104
 90%   1168
 95%   1168
 98%   1168
 99%   1168
100%   1168 (longest request)

バージョンアップ後は6.75と約3倍の性能アップを実現できました。

まとめ

無事にVPSのCentOS7でPHPのバージョンアップを行うことができました。もしかしたらプラグインや、記事の一部に不具合があるかもしれないので一度全記事の見直しをしてみたいと思います。 皆様もお気づきの点がございましたら、ご指摘頂けるとありがたいです。

次は新テーマ移行をやっていこうと思います。