CentOS Stream 9

Centos Stream 9にPHP 7.4をインストールする方法

Centos

CentOS 9には公式リポジトリでPHP 7.4がサポートされていないため、Remiリポジトリを使用してPHP 7.4をインストールする必要があります。以下の手順に従ってインストールを行います。

ステップ 1: 依存パッケージのインストール

まず、必要な依存パッケージをインストールします。ターミナルで以下のコマンドを実行してください:

sudo dnf install epel-release -y
sudo dnf install dnf-utils -y

画面ログ:

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

Last metadata expiration check: 0:16:14 ago on Tue 10 Sep 2024 12:38:33 PM JST.
Dependencies resolved.
=============================================================================================================================================================================================
 Package                                            Architecture                            Version                                     Repository                                      Size
=============================================================================================================================================================================================
Installing:
 epel-release                                       noarch                                  9-7.el9                                     extras-common                                   19 k
Installing weak dependencies:
 epel-next-release                                  noarch                                  9-7.el9                                     extras-common                                  8.1 k

Transaction Summary
=============================================================================================================================================================================================
Install  2 Packages

Total download size: 27 k
Installed size: 29 k
Downloading Packages:
(1/2): epel-release-9-7.el9.noarch.rpm                                                                                                                       273 kB/s |  19 kB     00:00
(2/2): epel-next-release-9-7.el9.noarch.rpm                                                                                                                  115 kB/s | 8.1 kB     00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                         69 kB/s |  27 kB     00:00
CentOS Stream 9 - Extras packages                                                                                                                            2.1 MB/s | 2.1 kB     00:00
Importing GPG key 0x1D997668:
 Userid     : "CentOS Extras SIG (https://wiki.centos.org/SpecialInterestGroup) <[email protected]>"
 Fingerprint: 363F C097 2F64 B699 AED3 968E 1FF6 A217 1D99 7668
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                     1/1
  Installing       : epel-release-9-7.el9.noarch                                                                                                                                         1/2
  Running scriptlet: epel-release-9-7.el9.noarch                                                                                                                                         1/2
Many EPEL packages require the CodeReady Builder (CRB) repository.
It is recommended that you run /usr/bin/crb enable to enable the CRB repository.

  Installing       : epel-next-release-9-7.el9.noarch                                                                                                                                    2/2
  Running scriptlet: epel-next-release-9-7.el9.noarch                                                                                                                                    2/2
  Verifying        : epel-next-release-9-7.el9.noarch                                                                                                                                    1/2
  Verifying        : epel-release-9-7.el9.noarch                                                                                                                                         2/2
Installed products updated.

Installed:
  epel-next-release-9-7.el9.noarch                                                                epel-release-9-7.el9.noarch

Complete!
[root@localhost ~]# sudo dnf install dnf-utils -y
Updating Subscription Management repositories.
Unable to read consumer identity

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

Extra Packages for Enterprise Linux 9 - x86_64                                                                                                                16 MB/s |  23 MB     0
Extra Packages for Enterprise Linux 9 openh264 (From Cisco) - x86_64                                                                                         234  B/s | 2.5 kB     0
Extra Packages for Enterprise Linux 9 - Next - x86_64                                                                                                        341 kB/s | 276 kB     0
Package yum-utils-4.3.0-16.el9.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!

ステップ 2: Remiリポジトリのインストール

Remiリポジトリは、複数のPHPバージョンを提供しているサードパーティのリポジトリです。以下のコマンドでRemiリポジトリをインストールします:

sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
[root@localhost ~]# sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
Updating Subscription Management repositories.
Unable to read consumer identity

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

Last metadata expiration check: 0:01:02 ago on Tue 10 Sep 2024 12:56:23 PM JST.
remi-release-9.rpm                                                                                                                                            28 kB/s |  30 kB     0
Dependencies resolved.
====================================================================================================================================================================================
 Package                                       Architecture                            Version                                           Repository
====================================================================================================================================================================================
Installing:
 remi-release                                  noarch                                  9.4-2.el9.remi                                    @commandline

Transaction Summary
====================================================================================================================================================================================
Install  1 Package

Total size: 30 k
Installed size: 31 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :
  Installing       : remi-release-9.4-2.el9.remi.noarch
  Verifying        : remi-release-9.4-2.el9.remi.noarch
Installed products updated.

Installed:
  remi-release-9.4-2.el9.remi.noarch

Complete!

ステップ 3: Remiリポジトリを有効化し、PHP 7.4を選択

次に、PHP 7.4のモジュールを有効化する必要があります。まず、利用可能なPHPモジュールをリストします:

sudo dnf module list php
[root@localhost ~]# sudo 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 "rhc" or "subscription-manager" to register.

Remi's Modular repository for Enterprise Linux 9 - x86_64                                                                                                    256 kB/s | 680 kB     0
Safe Remi's RPM repository for Enterprise Linux 9 - x86_64                                                                                                   365 kB/s | 1.0 MB     0
Last metadata expiration check: 0:00:01 ago on Tue 10 Sep 2024 12:58:10 PM JST.
CentOS Stream 9 - AppStream
Name                                 Stream                                   Profiles                                                  Summary
php                                  8.1                                      common [d], devel, minimal                                PHP scripting language
php                                  8.2                                      common [d], devel, minimal                                PHP scripting language

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

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

表示されたリストから「PHP 7.4」を確認し、以下のコマンドで有効化します:

sudo dnf module enable php:remi-7.4 -y
[root@localhost ~]# sudo dnf module enable php:remi-7.4 -y
Updating Subscription Management repositories.
Unable to read consumer identity

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

Last metadata expiration check: 0:00:21 ago on Tue 10 Sep 2024 12:58:10 PM JST.
Dependencies resolved.
====================================================================================================================================================================================
 Package                                      Architecture                                Version                                         Repository
====================================================================================================================================================================================
Enabling module streams:
 php                                                                                      remi-7.4

Transaction Summary
====================================================================================================================================================================================

Complete!

ステップ 4: PHP 7.4のインストール

Remiリポジトリを有効にした後、以下のコマンドでPHP 7.4とその関連パッケージをインストールします:

sudo dnf install php php-cli php-common php-mysqlnd php-fpm php-json php-xml php-mbstring php-zip php-gd php-curl -y
This system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.

Last metadata expiration check: 0:00:41 ago on Tue 10 Sep 2024 12:58:10 PM JST.
Dependencies resolved.
====================================================================================================================================================================================
 Package                                         Architecture                         Version                                               Repository
====================================================================================================================================================================================
Installing:
 php                                             x86_64                               7.4.33-17.el9.remi                                    remi-modular
 php-cli                                         x86_64                               7.4.33-17.el9.remi                                    remi-modular
 php-common                                      x86_64                               7.4.33-17.el9.remi                                    remi-modular
 php-fpm                                         x86_64                               7.4.33-17.el9.remi                                    remi-modular
 php-gd                                          x86_64                               7.4.33-17.el9.remi                                    remi-modular
 php-json                                        x86_64                               7.4.33-17.el9.remi                                    remi-modular
 php-mbstring                                    x86_64                               7.4.33-17.el9.remi                                    remi-modular
 php-mysqlnd                                     x86_64                               7.4.33-17.el9.remi                                    remi-modular
 php-pecl-zip                                    x86_64                               1.22.3-1.el9.remi.7.4                                 remi-modular
 php-xml                                         x86_64                               7.4.33-17.el9.remi                                    remi-modular
Installing dependencies:
 gd3php                                          x86_64                               2.3.3-9.el9.remi                                      remi-safe
 highway                                         x86_64                               1.2.0-2.el9                                           epel
 libaom                                          x86_64                               3.9.0-1.el9                                           epel
 libavif                                         x86_64                               0.11.1-4.el9                                          epel
 libdav1d                                        x86_64                               1.2.1-1.el9                                           epel
 libimagequant                                   x86_64                               2.17.0-1.el9                                          epel
 libjxl                                          x86_64                               0.7.0-1.el9                                           epel
 libraqm                                         x86_64                               0.8.0-1.el9                                           epel
 libsodium                                       x86_64                               1.0.18-8.el9                                          epel
 libvmaf                                         x86_64                               2.3.0-2.el9                                           epel
 oniguruma5php                                   x86_64                               6.9.9-1.el9.remi                                      remi-safe
 php-pdo                                         x86_64                               7.4.33-17.el9.remi                                    remi-modular
...

ステップ 5: PHPのバージョンを確認

インストールが完了したら、以下のコマンドでPHPのバージョンを確認できます:

php -v

「PHP 7.4」が表示されれば、インストールは正常に完了しています。

[root@localhost html]# php -v
PHP 7.4.33 (cli) (built: Aug 26 2024 13:25:54) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

ステップ 6: PHP-FPMの起動と有効化

Webサーバー(NginxやApache)とPHPを連携させるためにPHP-FPMを使用する場合、以下のコマンドでPHP-FPMを起動および有効化します:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm
[root@localhost html]# sudo systemctl enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

以上の手順で、CentOS 9にPHP 7.4をインストールし、動作させることができます。NginxやApacheとの連携方法についても、別の記事で解説していますので、ご参考ください。

ハッシュタグ: #CentOS9 #PHP7_4 #インストールガイド #サーバー構築

購読
通知
0 Comments
Inline Feedbacks
View all comments