はじめに

Red Hat社が AWS 上で配布するコミュニティ AMI を使用し、
EC2 上に起動した Red Hat Enterprise Linux 9 の環境において、
デフォルト状態では PHP 8.0系がインストールされる状態にあります。

PHP 8.0系は 2023/11/26 で EOL を迎えており、今から導入することは推奨されません。
本記事では最新の8.3系を導入するには?について記載しています。
Unsupported Branches

 

いきなりまとめ

PHP 8.2系および8.3系は remi リポジトリで提供がありますので、
subscription-manager を無効化した上で、Appstream を指定してインストールします。

# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y
# dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
# cat /etc/yum/pluginconf.d/subscription-manager.conf
[main]
enabled=0

# When following option is set to 1, then all repositories defined outside redhat.repo will be disabled
# every time subscription-manager plugin is triggered by dnf or yum
disable_system_repos=0
# dnf module install php:remi-8.3 -y
# php -v
PHP 8.3.1 (cli) (built: Dec 20 2023 12:44:38) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies

 

EC2 インスタンス起動直後

本記事では RHEL 9.2 を使用します。

AMI名 AMI ID
RHEL-9.2.0_HVM-20230905-x86_64-38-Hourly2-GP2 ami-04fdeff70b7359022
$ cat /etc/redhat-release
Red Hat Enterprise Linux release 9.2 (Plow)

デフォルト状態での有効なリポジトリは Red Hat社が提供する BaseOS, AppStream および
rhui-client-config-server-9 があります。

# dnf repolist
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

repo id repo name
rhel-9-appstream-rhui-rpms Red Hat Enterprise Linux 9 for x86_64 - AppStream from RHUI (RPMs)
rhel-9-baseos-rhui-rpms Red Hat Enterprise Linux 9 for x86_64 - BaseOS from RHUI (RPMs)
rhui-client-config-server-9 Red Hat Enterprise Linux 9 Client Configuration

利用可能な PHP バージョンを確認すると 8.1系の提供がありました。

# dnf module list php
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:00:41 ago on Wed 27 Dec 2023 05:25:50 AM UTC.
Red Hat Enterprise Linux 9 for x86_64 - AppStream from RHUI (RPMs)
Name Stream Profiles Summary
php 8.1 common [d], devel, minimal PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

特に何も考えずに PHP をインストールすると、AppStream リポジトリ経由で 8.0.30 が入るようです。

# dnf install php -y
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:02:31 ago on Wed 27 Dec 2023 05:03:51 AM UTC.
Dependencies resolved.
============================================================================================================================================================================================================================================
Package Architecture Version Repository Size
============================================================================================================================================================================================================================================
Installing:
php x86_64 8.0.30-1.el9_2 rhel-9-appstream-rhui-rpms 8.8 k
Installing dependencies:
libxslt x86_64 1.1.34-9.el9 rhel-9-appstream-rhui-rpms 247 k
nginx-filesystem noarch 1:1.20.1-14.el9_2.1 rhel-9-appstream-rhui-rpms 11 k
oniguruma x86_64 6.9.6-1.el9.5 rhel-9-appstream-rhui-rpms 221 k
php-common x86_64 8.0.30-1.el9_2 rhel-9-appstream-rhui-rpms 684 k
Installing weak dependencies:
php-cli x86_64 8.0.30-1.el9_2 rhel-9-appstream-rhui-rpms 3.1 M
php-fpm x86_64 8.0.30-1.el9_2 rhel-9-appstream-rhui-rpms 1.6 M
php-mbstring x86_64 8.0.30-1.el9_2 rhel-9-appstream-rhui-rpms 470 k
php-opcache x86_64 8.0.30-1.el9_2 rhel-9-appstream-rhui-rpms 512 k
php-pdo x86_64 8.0.30-1.el9_2 rhel-9-appstream-rhui-rpms 85 k
php-xml x86_64 8.0.30-1.el9_2 rhel-9-appstream-rhui-rpms 138 k

Transaction Summary
============================================================================================================================================================================================================================================
Install 11 Packages
# php -v
PHP 8.0.30 (cli) (built: Aug 3 2023 17:13:08) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologies
with Zend OPcache v8.0.30, Copyright (c), by Zend Technologies

一度アンインストールし、Stream 8.1 を選択した上で PHP を再度インストールすることで 8.1系が入りました。

# dnf module install php:8.1 -y
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:03:35 ago on Wed 27 Dec 2023 05:25:50 AM UTC.
Dependencies resolved.
============================================================================================================================================================================================================================================
Package Architecture Version Repository Size
============================================================================================================================================================================================================================================
Installing group/module packages:
php-cli x86_64 8.1.14-1.module+el9.2.0+17911+b059dfc2 rhel-9-appstream-rhui-rpms 3.5 M
php-common x86_64 8.1.14-1.module+el9.2.0+17911+b059dfc2 rhel-9-appstream-rhui-rpms 687 k
php-fpm x86_64 8.1.14-1.module+el9.2.0+17911+b059dfc2 rhel-9-appstream-rhui-rpms 1.8 M
php-mbstring x86_64 8.1.14-1.module+el9.2.0+17911+b059dfc2 rhel-9-appstream-rhui-rpms 476 k
php-xml x86_64 8.1.14-1.module+el9.2.0+17911+b059dfc2 rhel-9-appstream-rhui-rpms 147 k
Installing dependencies:
libxslt x86_64 1.1.34-9.el9 rhel-9-appstream-rhui-rpms 247 k
nginx-filesystem noarch 1:1.20.1-14.el9_2.1 rhel-9-appstream-rhui-rpms 11 k
oniguruma x86_64 6.9.6-1.el9.5 rhel-9-appstream-rhui-rpms 221 k
Installing module profiles:
php/common
Enabling module streams:
php 8.1

Transaction Summary
============================================================================================================================================================================================================================================
Install 8 Packages
# php -v
PHP 8.1.14 (cli) (built: Jan 4 2023 17:23:14) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.14, Copyright (c) Zend Technologies

ただ、8.1系は本記事を書いている段階で EOL を迎えるまで1年を切っており、
2024/11/25 でサポートが終了となります

2024/8/7 追記
本日確認したところ、PHP 8.1系の EOL (セキュリティサポート)が、2025/12/31 まで延長されております。

引き続き、以下では8.2系もしくは8.3系をインストールする方法を確認します。
Supported Versions

 

EPEL, remiリポジトリ追加

Appstream リポジトリでは現状PHP 8.1系の提供になるため、EPEL, remi リポジトリを追加します。

# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y
# dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
# dnf repolist
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

repo id repo name
epel Extra Packages for Enterprise Linux 9 - x86_64
epel-cisco-openh264 Extra Packages for Enterprise Linux 9 openh264 (From Cisco) - x86_64
remi-modular Remi's Modular repository for Enterprise Linux 9 - x86_64
remi-safe Safe Remi's RPM repository for Enterprise Linux 9 - x86_64
rhel-9-appstream-rhui-rpms Red Hat Enterprise Linux 9 for x86_64 - AppStream from RHUI (RPMs)
rhel-9-baseos-rhui-rpms Red Hat Enterprise Linux 9 for x86_64 - BaseOS from RHUI (RPMs)
rhui-client-config-server-9 Red Hat Enterprise Linux 9 Client Configuration Red Hat Enterprise Linux 9 Client Configuration

両リポジトリを追加した後、8.2系および8.3系がインストール可能になります。

# dnf module list php
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Remi's Modular repository for Enterprise Linux 9 - x86_64 248 kB/s | 644 kB 00:02
Safe Remi's RPM repository for Enterprise Linux 9 - x86_64 317 kB/s | 973 kB 00:03
Last metadata expiration check: 0:00:01 ago on Wed 27 Dec 2023 05:44:01 AM UTC.
Remi's Modular repository for Enterprise Linux 9 - x86_64
Name Stream Profiles Summary
php remi-7.4 common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
php remi-8.1 common [d], devel, minimal PHP scripting language
php remi-8.2 common [d], devel, minimal PHP scripting language
php remi-8.3 common [d], devel, minimal PHP scripting language

Red Hat Enterprise Linux 9 for x86_64 - AppStream from RHUI (RPMs)
Name Stream Profiles Summary
php 8.1 [e] common [d] [i], devel, minimal PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

 

subscription-manager 無効化

EPEL や Remi リポジトリ経由でパッケージのインストールを試みると、
subscription-manager でサブスクリプション登録を促すメッセージが表示されます。

# dnf module install php:remi-8.3 -y
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:01:50 ago on Wed 27 Dec 2023 05:44:01 AM UTC.
Dependencies resolved.
The operation would result in switching of module 'php' stream '8.1' to stream 'remi-8.3'
Error: It is not possible to switch enabled streams of a module unless explicitly enabled via configuration option module_stream_switch.
It is recommended to rather remove all installed content from the module, and reset the module using 'dnf module reset ' command. After you reset the module, you can install the other stream.

実際には RHEL AMI には、アップデートやメンテナンスによる修正を受けるための
基本サブスクリプションが含まれており、subscription-manager に登録する必要はありません。

subscription-manager はデフォルト状態で enabled=1 (有効) になっているので enabled=0 (無効) にします。

# cat /etc/yum/pluginconf.d/subscription-manager.conf
[main]
enabled=0

# When following option is set to 1, then all repositories defined outside redhat.repo will be disabled
# every time subscription-manager plugin is triggered by dnf or yum
disable_system_repos=0

 

PHP 8.3系のインストール

以下の通り、晴れて8.3系をインストールすることができました。

# dnf module install php:remi-8.3 -y
Last metadata expiration check: 0:17:30 ago on Wed 27 Dec 2023 05:44:01 AM UTC.
Dependencies resolved.
============================================================================================================================================================================================================================================
Package Architecture Version Repository Size
============================================================================================================================================================================================================================================
Upgrading:
php-cli x86_64 8.3.1-1.el9.remi remi-modular 5.5 M
php-common x86_64 8.3.1-1.el9.remi remi-modular 1.2 M
php-fpm x86_64 8.3.1-1.el9.remi remi-modular 1.9 M
php-mbstring x86_64 8.3.1-1.el9.remi remi-modular 575 k
php-xml x86_64 8.3.1-1.el9.remi remi-modular 216 k
Installing dependencies:
oniguruma5php x86_64 6.9.9-1.el9.remi remi-safe 219 k
Installing module profiles:
php/common
Enabling module streams:
php remi-8.3

Transaction Summary
============================================================================================================================================================================================================================================
Install 1 Package
Upgrade 5 Packages
# php -v
PHP 8.3.1 (cli) (built: Dec 20 2023 12:44:38) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies