メインコンテンツへスキップ
  1. ブログ/

AlmaLinux8でPHP-FPMをソースからビルドする方法

·
PHP ビルド
目次

今回はPHP-FPMをソースからビルドする方法行う方法につて解説致します。

前提
#

RedHat系OSを利用している場合、通常はyumdnfといったパッケージ管理システムを使って簡単にPHPをインストールできます。

しかし、利用したいPHPのバージョンが標準リポジトリに存在しないことがあります。

このような場合、他リポジトリを追加しインストールを行う方法やPHPのソースをダウンロードしビルドする方法があります。

今回は後者のPHPのソースをダウンロードしビルドする方法のご紹介となります。

利用環境
#

今回利用する環境は「AlmaLinux8」を例としてご紹介させていただきます。

cat /etc/almalinux-release
AlmaLinux release 8.6 (Sky Tiger)

下記コマンドを使用する事でAppStreamリポジトリにてインストール可能なバージョンリストを確認する事ができます。

dnf module list php

下記コマンドの結果となります。

AlmaLinux 8 - AppStream
Name                Stream                 Profiles                                  Summary
php                 7.2 [d]                common [d], devel, minimal                PHP scripting language
php                 7.3                    common [d], devel, minimal                PHP scripting language
php                 7.4                    common [d], devel, minimal                PHP scripting language
php                 8.0                    common [d], devel, minimal                PHP scripting language
php                 8.2                    common [d], devel, minimal                PHP scripting language

コマンドでの確認結果ではデフォルト指定バージョンが「PHP7.2」となっており「PHP8.2」までのバージョンが選択可能となっておりました。

それでは今回は例として8系のAppStreamリポジトリに公開されていない「PHP8.1」をビルドしていきます。

ソースのダウンロードと解凍
#

今回はwgetコマンドを使用して/optにソースのダウンロードを行います。

初期設定ではwgetは使用できない為、以下を使用しインストールを行います。

dnf install wget

次にインストールディレクトリに移動します。

cd /opt

それではwgetコマンドを使用してソースのダウンロードを行います。

wget https://www.php.net/distributions/php-8.1.28.tar.gz

その他のバージョンを利用したい方は以下よりURLを確認の上、修正しご利用下さい。

PHPダウンロード

PHPダウンロード

次にtarコマンドを使用してダウンロードしたファイルの解凍を行います。

tar  -zxvf php-8.1.28.tar.gz

lsコマンドでディレクトリ内を確認するとダウンロードしたファイルと解凍したファイルが以下の様に表示されます。

ls /opt
php-8.1.28  php-8.1.28.tar.gz

ビルドの事前準備
#

次にビルドの事前準備としてmakeコマンドとgccコンパイラのインストールを行います。

以下のようにインストールを実施します。

dnf install make
dnf install gcc

インストールの準備
#

はじめに先ほどダウンロードし解凍したディレクトリに移動します。

cd /opt/php-8.1.28

次にconfigureを使用しビルドの詳細を指定します。

./configure \

–prefixを使用しインストールパスを指定します。今回は/opt/php8.1として指定します。

PHP-FPMとして動作させたい場合は–enable-fpmを指定します。

その他細かいオプションは各自でご調整の上、ご指定よろしくお願いします。

./configure \
    --prefix=/opt/php8.1 \
    --enable-mbstring \
    --with-pdo-mysql \
    --enable-gd \
    --with-jpeg=/usr/include/ \
    --with-freetype=/usr/include/ \
    --with-openssl \
    --enable-xml \
    --with-curl \
    --with-bz2 \
    --with-zip \
    --with-zlib \
    --with-mysqli \
    --enable-fpm \
    --enable-exif \
    --enable-mbstring \
    --with-libdir=lib64

上記を実行すると依存関係に必要なパッケージがない事によりエラーが発生します。

必要に合わせてパッケージのインストールを行なってください。

依存関係のインストール
#

・libxml-2.0

Package 'libxml-2.0', required by 'virtual:world', not found
dnf install libxml2-devel

・openssl

Package 'openssl', required by 'virtual:world', not found
dnf install openssl-devel
※RedHat9系を利用する場合はopenssl 1.1.系をインストールする必要があるとの事です。

・sqlite3

Package 'sqlite3', required by 'virtual:world', not found
dnf install sqlite-devel

・BZip2

configure: error: Please reinstall the BZip2 distribution
dnf install bzip2-devel

・libcurl

Package 'libcurl', required by 'virtual:world', not found
dnf install libcurl-devel

・libpng

Package 'libpng', required by 'virtual:world', not found
dnf install libpng-devel

・libjpeg

Package 'libjpeg', required by 'virtual:world', not found
dnf install libjpeg-turbo-devel

・freetype2

Package 'freetype2', required by 'virtual:world', not found
dnf install freetype-devel

・oniguruma

Package 'oniguruma', required by 'virtual:world', not found
[RHEL9系の場合]
dnf --enablerepo=crb install oniguruma-devel

[RHEL8系の場合]
dnf --enablerepo=powertools install oniguruma-devel

・libzip

Package 'libzip', required by 'virtual:world', not found

[RHEL9系の場合]
dnf --enablerepo=crb install libzip-devel

[RHEL8系の場合]
dnf --enablerepo=powertools install libzip-devel

ライセンスについて
#

依存関係に問題がなければ以下のように、ライセンスのメッセージが表示されます。

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

コンパイル
#

依存関係、およびライセンスに問題がなれければmakeコマンドにてコンパイルを行なっていきます。

makeコマンドは先ほどのconfigureにて作成されたMakefileを使用しソースコードをコンパイルします。

同一のディレクトリにて下記を実行します。

make

ビルドが完了すると以下のように表示されます。

Build complete.
Don't forget to run 'make test'.

インストール
#

最後に以下のコマンドを使用してインストールを行います。

make install

実行すると以下のように表示されます。

Installing shared extensions:     /opt/php8.1/lib/php/extensions/no-debug-non-zts-20210902/
Installing PHP CLI binary:        /opt/php8.1/bin/
Installing PHP CLI man page:      /opt/php8.1/php/man/man1/
Installing PHP FPM binary:        /opt/php8.1/sbin/
Installing PHP FPM defconfig:     /opt/php8.1/etc/
Installing PHP FPM man page:      /opt/php8.1/php/man/man8/
Installing PHP FPM status page:   /opt/php8.1/php/php/fpm/
Installing phpdbg binary:         /opt/php8.1/bin/
Installing phpdbg man page:       /opt/php8.1/php/man/man1/
Installing PHP CGI binary:        /opt/php8.1/bin/
Installing PHP CGI man page:      /opt/php8.1/php/man/man1/
Installing build environment:     /opt/php8.1/lib/php/build/
Installing header files:          /opt/php8.1/include/php/
Installing helper programs:       /opt/php8.1/bin/
  program: phpize
  program: php-config
Installing man pages:             /opt/php8.1/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PDO headers:           /opt/php8.1/include/php/ext/pdo/

以上でビルド完了となります。

バージョン確認
#

ビルドした実行ファイルに対して-vを使用してバージョンを確認してみます。

/opt/php8.1/sbin/php-fpm -v

上記を実行すると下記のように表示されます。

PHP 8.1.28 (fpm-fcgi) (built: Sep 23 2024 18:15:56)
Copyright (c) The PHP Group
Zend Engine v4.1.28, Copyright (c) Zend Technologies

インストールしたPHPバージョンが表示されたら成功です。

モジュール確認
#

PHPのモジュールを確認するにはインストールした実行ファイルに対して-mを使用してを確認します。

/opt/php8.1/sbin/php-fpm -m

上記を実行すると下記のように表示されます。

[PHP Modules]
bz2
cgi-fcgi
Core
ctype
curl
date
dom
exif
fileinfo
filter
gd
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib

起動確認
#

インストールディレクトリ内の設定ファイルを利用環境に合わせて修正して下さい。

今回はデフォルト設定にて起動する為、下記を実行します。

cp -a /opt/php8.1/etc/php-fpm.conf.default /opt/php8.1/etc/php-fpm.conf
cp -a /opt/php8.1/etc/php-fpm.d/www.conf.default /opt/php8.1/etc/php-fpm.d/www.conf

起動前に-tを使用し設定ファイルをテストします。

/opt/php8.1/sbin/php-fpm -t

設定に問題がなければ以下のようにsuccessfulが表示されます。

NOTICE: configuration file /opt/php8.1/etc/php-fpm.conf test is successful

問題がなければ以下を実行しPHPを起動します。

/opt/php8.1/sbin/php-fpm

プロセス表示し起動している事を確認します。

root      159948       1  0 20:02 ?        00:00:00 php-fpm: master process (/opt/php8.1/etc/php-fpm.conf)
nobody    159949  159948  0 20:02 ?        00:00:00 php-fpm: pool www
nobody    159950  159948  0 20:02 ?        00:00:00 php-fpm: pool www

関連記事

【RHEL9の変更点】php_modの廃止について
PHP Apache
【超簡単】PHPで現在のモジュール一覧を確認する方法
PHP コマンド
IMAP修正UTF-7エンコードをデコードする方法
PHP プログラム
AlmaLinux8でruby2.6以上をインストールする方法
Linux プログラム
Linux CLIから「Zed Attack Proxy」でセキュリティテストを実行する方法
セキュリティ
Mailmanのバージョンを確認する方法
Linux メール