備忘録

CentOS7に最新のPHP7.3をインストール

CentOS7のデフォルトでは5.4.16とバージョンが古いので、Remiリポジトリを使って最新のPHP7.3をインストールした時のメモ

CentOS7の公式yumリポジトリで提供されているバージョン

# yum info php

利用可能なパッケージ
名前                : php
アーキテクチャー    : x86_64
バージョン          : 5.4.16
リリース            : 46.el7
容量                : 1.4 M
リポジトリー        : base/7/x86_64
要約                : PHP scripting language for creating dynamic web sites
URL                 : http://www.php.net/
ライセンス          : PHP and Zend and BSD
説明                : 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.

PHPがすでにインストールされていたら削除

PHPインストール確認

# yum list installed | grep php

Apacheの停止

# systemctl stop httpd.service

PHPの削除

# yum remove php-*

EPELリポジトリの追加

Remiで提供されているソフトウェアをインストールするにはEPELリポジトリも必要なので、先にインストールします。

# yum install epel-release

EPELリポジトリの無効化

EPELリポジトリは、インストールすると有効化の状態になっています。必要な時だけEPELリポジトリを使いたいので無効化にします。

/etc/yum.repos.d/epel.repo

enabled=1
↓
enabled=0

EPELリポジトリを無効化にしたので、使用する時は「--enablerepo=epel」といったように明示的に実行します。

Remiのリポジトリを追加

最新のPHPをインストールするのにRemiリポジトリを使います

# yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Remiリポジトリの無効化

Remiリポジトリはデフォルトで無効になっています。念のため「enabled=0」になっていることを確認します。

/etc/yum.repos.d/remi.repo

enabled=0

php7.3系をインストールしたい場合は「--enablerepo=remi-php73」といったようにリポジトリを指定して実行します。

最新のPHP7インストール

# yum --enablerepo=epel,remi-php73 install php php-mbstring php-mysql php-xml php-mcrypt php-bcmath

PHP7.0系:remi-php70
PHP 7.1系:remi-php71
PHP 7.2系:remi-php72

インストールが完了したらバージョンを確認

# php -v

PHP 7.3.8 (cli) (built: Jul 99 9999 99:99:99) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.8, Copyright (c) 1998-2018 Zend Technologies

php.iniの設定

/etc/php.ini

変更前にバックアップ

# cp -p /etc/php.ini /etc/php.ini_`date "+%Y%m%d%H%M%S"`

設定箇所抜粋。 各自の状況によって変更してください。

short_open_tag = Off
open_basedir = /var/www/:/var/lib/php/session/:/tmp/
disable_functions = phpinfo, eval, system, exec, shell_exec, pcntl_exec, passthru, popen, proc_open
expose_php = Off

max_execution_time = 30
max_input_time = 30
max_input_vars = 200

memory_limit = 128M

error_reporting = E_ALL
display_errors = Off
log_errors = On
error_log = /var/log/php/error.log

register_argc_argv = Off
post_max_size = 8M
default_charset = "UTF-8"

enable_dl = Off
file_uploads = On
upload_tmp_dir = /tmp/
upload_max_filesize = 2M
allow_url_fopen = Off
allow_url_include = Off

date.timezone = Asia/Tokyo
mail.add_x_header = Off

session.use_strict_mode = 1
session.use_cookies = 1
;session.cookie_secure =
session.use_only_cookies = 1
session.cookie_lifetime = 0
session.cookie_httponly = On
session.gc_probability = 1
session.gc_divisor = 1
session.gc_maxlifetime = 108000
session.use_trans_sid = 0

mbstring.language = Japanese
;mbstring.internal_encoding =
;mbstring.http_input =
;mbstring.http_output =
mbstring.encoding_translation = Off
mbstring.detect_order = auto
mbstring.substitute_character = none

※「mbstring.internal_encoding」「mbstring.http_input」「mbstring.http_output」は、PHP 5.6.0 で非推奨になったようで、代わりに「default_charset 」の設定を推奨しているようです。

ログの出力先

ログの出力先を変更したのでディレクトリと出力ファイルを作成

# mkdir /var/log/php/
# touch /var/log/php/error.log
# chown apache:apache /var/log/php/error.log

Apacheを再起動

編集(php.ini)した内容を反映させるためApacheを再起動します。

# systemctl restart httpd.service

モジュールを確認

ApacheのモジュールとしてPHPが読み込まれていることが確認できればOKです。

# httpd -M | grep php

php7_module (shared)