【WordPress】PHP 5.5.x から PHP 7.0.x への移行

WordPress (5.0.2) の PHP を 5.5.9 から 7.0.33 にアップグレードしたので備忘録を残しておきます。

  • 移行前のバージョンの確認
  • PHP 7.0.x のインストール
  • FastCGI / Nginx の設定変更

環境は Ubuntu 14.04 です。

移行前のバージョン確認

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

$ which php
/usr/bin/php
$ ls -l $(which php)
lrwxrwxrwx 1 root root 21 May 23  2015 /usr/bin/php -> /etc/alternatives/php
$ php -v
PHP 5.5.9-1ubuntu4.14 (cli) (built: Oct 28 2015 01:34:46)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

PHP 7.0.x のインストール

/etc/apt/sources.list は設定済みの APT data sources のリストを記述したファイルである。
PHP 7.0.x をインストールするため, add-apt-repository コマンドで非公式リポジトリ PPA (Personal Package Archive) を /etc/apt/sources.list.d/ 以下に追加する。

$ sudo add-apt-repository ppa:ondrej/php
 Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.

Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting

...
OK
$ cat /etc/apt/sources.list.d/ondrej-php-trusty.list
deb http://ppa.launchpad.net/ondrej/php/ubuntu trusty main
# deb-src http://ppa.launchpad.net/ondrej/php/ubuntu trusty main
$ sudo apt update
Reading package lists... Done

今回インストールする PHP 7.0 と PHP 拡張モジュールは以下。 これらは WordPress の利用状況によって異なるので参考までに。

  • php7.0-cli: PHP の CLI を提供する拡張モジュール
  • php7.0-curl: CURL を扱うための拡張モジュール
  • php7.0-gd: PHP からグラフィックを直接操作する GD (Graphics Draw) の拡張モジュール
  • php7.0-fpm: PHP の FastCGI 実装である FPM(FastCGI Process Manager) の拡張モジュール
  • php7.0-mysql: MySQL ドライバを提供する拡張モジュール
  • php7.0-mbstring: マルチバイト文字列をサポートする拡張モジュール

上記パッケージの詳細を確認。

$ sudo apt show php7.0 php7.0-cli php7.0-gd php7.0-fpm php7.0-mysql php7.0-mbstring

インストールを実行する。

$ sudo apt get php7.0 php7.0-cli php7.0-curl php7.0-gd php7.0-fpm php7.0-mysql php7.0-mbstring 

PHP 7.0.33 がインストールされたことを確認。

$ php7.0 -v
PHP 7.0.33-1+ubuntu14.04.1+deb.sury.org+1 (cli) (built: Dec  7 2018 09:26:06) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33-1+ubuntu14.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

FastCGI / Nginx の設定変更

Nginx から FastCGI への接続を TCP socket から UNIX domain sockets に変更する。

まず, php7.0-fpm の設定を変更する。具体的には listen に 127.0.0.1:9000 (TCP socket) でなく UNIX domain sockets の path を指定, listen.owner, listen.group を www-data に変更する。

$ sudo vim /etc/php/7.0/fpm/pool.d/www.conf
...
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php7.0-fpm.sock
...
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
...

次に Nginx の設定を変更する。 fastcgi_pass を 127.0.0.1:9000 から unix:/run/php/php7.0-fpm.sock に変更する。

$ sudo vim /etc/nginx/nginx.conf
...
            location ~ \.php$ {
                root /var/www/public_html;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_buffers 8 128k;
                fastcgi_buffer_size 256k;
                fastcgi_read_timeout 500;
                client_max_body_size 3M;
                include fastcgi_params;
            }
...

php7.0-fpm, Nginx を再起動し設定変更を反映する。

$ sudo service php7.0-fpm restart
php7.0-fpm stop/waiting
php7.0-fpm start/running, process 29548
$ sudo service php7.0-fpm status
php7.0-fpm start/running, process 29548
$ sudo service nginx restart
 * Restarting nginx nginx
   ...done.
$ sudo service nginx status
 * nginx is running

最後に確認を行う。Nginx のドキュメントルート直下に以下のファイルを置く。

ubuntu@ubuntu /var/www/public_html]$ cat testinfo.php
<?php phpinfo(); ?>

testinfo.php にアクセスすると PHP の設定情報が出力される。

詳細な設定が確認でき便利な一方, 必要以上に設定情報を公開することはセキュリティ上のリスクとなるため適切にアクセス制限を行うか使用後にファイルを削除するなどの対策が必要。

[1] nginx error connect to php5-fpm.sock failed (13: Permission denied)
[2] PHP 5.6.x から PHP 7.0.x への移行