How to compile PHP from source on Fedora/RHEL/etc

Published On25 Sep 2021

Compiling PHP from source on Fedora and Enterprise Linux (RHEL, CentOS, etc)

Compiling PHP from source on Fedora and Enterprise Linux (RHEL, CentOS, etc.) is quite straight forward, and is on par with how PHP is compiled on Ubuntu/Debian.

Using Compiled Binaries

Compiled PHP binaries are readily available for Fedora and Fedora-alike operating systems in their default software repositories. For PHP setups that do not require compile-step customizations, it might be easier to install PHP from the software repositories without compiling PHP on the target server.

dnf install php

The PHP versions available at the default software repositories may not be the latest version. For example, Fedora 34 ships with PHP 7.4, and not PHP 8.0, which is the latest version.

Thanks to Remi Collet's Remi repo more PHP versions are available to install without having to compile PHP. Instructions on adding this repo is available at the official web site.

Once the repository is added, PHP can be installed from the Remi repository:

sudo dnf module install php:remi-8.0

Compiling PHP from Source

Compiling PHP from its source files entails fetching the PHP source code, installing the compilers and its tools on the server, configuring the build, and finally compiling it.

Looking for a TL;DR? A short version of the guide for the copy-paste pleasure is available at the TL;DR section.

Prerequisites

  • The Git repository of PHP source code, when cloned, is around 450 MB. PHP source, and its required dependencies will require about 1 GB of empty disk space.
  • The initial setup will also require downloading ~ 600 MB of data from archives.

Install Build Tools

sudo dnf install git make gcc gcc-c++ binutils glibc-devel autoconf libtool bison re2c automake libxml2-devel

The command above install the C compiler, and other tools required to compile PHP. This is similar to installing build-essential package in Debian/Ubuntu systems.

Install build tools

  • gcc, gcc-c++ are C/C++ compilers.
  • make is a utility to direct the compilation scripts.
  • autoconf is used to generate the configure script that is used later in the compilation.
  • libtool is a tool that helps to manage and locate shared libraries.
  • bison is YACC-compatible parser generator
  • re2c is a tool that is used to generate the PHP's lexer.
  • automake is used to create Makefiles.
  • binutils is a collection of binaries for creating/modifying/extracting archives, displaying binary sizes, information, etc.
  • libxml2-devel is one of the dependencies that PHP uses for its XML support. While it is possible to completely disable it, it is more common to compile PHP with XML support.

Fetch PHP Source Code

PHP source code is available at php/php-src repository on GitHub.

git clone https://github.com/php/php-src.git --branch=master

This clones the PHP source code to the current working directory, and checkout the master branch. The master branch contains the latest PHP source code.

To fetch a specific PHP version, specify its branch. For example, to checkout PHP 7.4.6:

git clone https://github.com/php/php-src.git --branch=PHP-7.4.6

git clone php-src The PHP php-src repository will be cloned to a php-src directory in the current working directory. Once done, cd into the new directory:

cd php-src

Build ./configure Script

Running the buildconf command creates a configuration script inphp-src/configure, which can then be used to configure the build.

./buildconf

Run buildconf on php-src On subsequent compilations, it might be necessary to rebuild the ./configure script by running the buildconf command with the --force option. This will ensure the ./configure script is up to date.

Run buildconf on php-src and force it

Configure the build

The ./configure script is used to enable/disable build configurations. This includes enabling/disabling certain PHP modules, enabling specific features of PHP (such as image type toggles for the GD extension, toggle IPv6 support, etc).

To view all the configuration flags and options available, run ./configure --help.

configure script help The flags shown in the ./configure --help follow a pattern of --enable-XYZ, --disable-XYZ, or --with-XYZ. It accepts multiple flags, and is often a very long one in most PHP setups primed for production use.

example

./configure --enable-ftp --with-openssl --disable-cgi

--enable-XYZ

If the flag is passed, the extension/SAPI/feature with name XXX will be enabled.

For extensions, using --enable-XXX=shared pattern makes the extension compiled to a separate file so it can be enabled/disabled from PHP INI files.

For example, running ./configure --enable-ftp enables the FTP extension; Running ./configure --enable-ftp=shared enables the extension to be compiled as a shared extension; the extension will be compiled to a separate .so file, so it can be enabled/disabled using a PHP configuration file.

Not all extensions support compiling to a shared extension.

In addition to extensions, the --enable-XYZ options are available for Server APIs (SAPIs) and specific features as well. Notably, the --enable-zts enables thread-safety feature in the build.

--with-XYZ

This option is similar to --enable-XYZ pattern that they enable various PHP extensions and features. Note these extensions/features often require additional dependencies.

For example, the OpenSSL extension, enabled with ./configure --with-openssl depends on the development files of the OpenSSL library. In Fedora and alike, they can be easily installed with the openssl-devel package. The -devel suffix to the package name indicates that it is a development package. To fulfill the requirements for OpenSSL extension, install libssl-dev package:

sudo dnf install openssl-devel

Appendix: Extension Dependencies An up-to-date list of extension requirements are listed in Appendix: Extension Dependencies section of this guide.

--disable-XYZ and --without-XYZ

The opposite of --enable-XYZ flags. The presence of this flag means PHP is configured to include that extension/feature/SAPI by default, unless the --disable-XYZ option is passed.

Additionally, the --disable-all flag disables all extensions, which allows a clean slate for individual extensions to be enabled with --enable-XYZ flags.


By default, PHP compiles with SQLite support built-in. Disabling the SQLite3 extensions makes it possible to compile PHP without having to install SQLite3 dependencies.

./configure --without-sqlite3 --without-pdo-sqlite

Subsequent Builds

Calling the ./configure command again and again with several options is cumbersome. When the ./configure script is run, it saves the command to a ./config.nice file, that executes the exact same command as before, and optionally append additional options.

After the first ./configure run has completed, using the ./config.nice file helps to avoid typing the same ./configure options again and again.

./config.nice

Compile!

Once the ./configure/./config.nice script has completed, it is now time to run the compiler.

Depending on the CPU cores and threads available, this can take anywhere in the range from 2 minutes to 15-20 minutes.

make -j $(nproc)

The make command is used to run the compilation using the C compiler. It accepts a -j parameter, that can be used to configure parallel processing. Output of the nproc command, which returns the number of available CPU threads in the system is then set for the make -j parameter.

If the -j option is not present, it will use a single CPU thread by default. To set a specific number of threads, simply specify the number for the -j option:

make -j4

Install/try it out

The compiled binaries will be available in the ./sapi directory. For example, to immediately run the PHP CLI, call the ./sapi/cli/php binary.

./sapi/cli/php -v

php -v


Alternately, the compiled PHP version can be installed on the system, so other tools can easily use the php binary in PATH.

sudo make install

sudo make install


Appendix: Extension Dependencies

Following are command-line arguments can be passed to the ./configure script to enable/disable/configure PHP extensions and features.

Core extensions

The following extensions are PHP core extensions, and cannot be disabled. Older PHP versions might have had a flag to toggle this extension, but they are not valid anymore for these extensions.

Extension Notes
Date
Hash Core extension since PHP 7.4
JSON Core extension since PHP 8.0
PCRE Pass --without-pcre-jit to Just-In-Time compilation. Since PHP 7.3, it uses PCRE2
Reflection
SPL Core extension since PHP 5.3

Enabled by default

The following extensions are enabled by default, but can be disabled if necessary. The --disable-all flag also disables all of them.

Extension Disable flag
Ctype --disable-ctype
Fileinfo --disable-fileinfo
Filter --disable-filter
Opcache --disable-opcache, or --disable-opcache-jit to disable JIT
PDO --disable-pdo
Phar --disable-phar
POSIX --disable-posix
Session --disable-session
SimpleXML --disable-simplexml
SQLite --without-sqlite3
Tokenizer --disable-tokenizer
XML --disable-xml
XMLReader --disable-xmlreader
XMLWriter --disable-xmlwriter

Disabled by default

Compiling additional extensions often require its dependencies in place. Here is a list of PHP extensions and their dependencies, and the ./configure flag to enable it.

The Dependencies column lists the package names in Ubuntu/Debian repositories. Install them using the package manager:

sudo dnf install <package-name>
Extension Enable flags Dependencies
BCMath --enable-bcmath none
BZ2 --with-bz2 bzip2-devel
Curl --with-curl libcurl-devel
Exif --enable-exif none
FFI --with-ffi libffi-devel
FTP --enable-ftp none
GD* --enable-gd --with-jpeg --with-webp --with-avif libzip-devel libpng-devel libjpeg-devel libwebp-devel libavif-devel
GMP --with-gmp gmp-devel
IMAP --with-imap --with-imap-ssl --with-kerberos libc-client-dev libkrb5-devel
Intl --enable-intl libicu-devel
LDAP --with-ldap openldap-devel
Mbstring --enable-mbstring oniguruma-devel
OpenSSL --with-openssl openssl-devel
PDO: MySQL --with-pdo-mysql none
PDO: PgSQL --with-pdo-mysql libpq-devel
Pspell --with-pspell aspell-devel
Readline --with-readline readline-devel
Sockets --enable-sockets none
Sodium --with-sodium libsodium-devel
Soap --enable-soap --with-libxml libxml2-devel
Zip --with-zip libzip-devel

Notes


TL;DR

The entire list of commands above are shortened below:

Initial run:

sudo dnf install git make gcc gcc-c++ binutils glibc-devel autoconf libtool bison re2c automake libxml2-devel
git clone https://github.com/php/php-src.git --branch=master
cd php-src
./buildconf
./configure
make -j $(nproc)
sudo make install

Routine runs:

cd php-src
git pull --rebase
./buildconf --force
./config.nice
make -j $(nproc)
sudo make install

A ready-to-use ./config.nice is also available.

Recent Articles on PHP.Watch

All ArticlesFeed 
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

A guide for Debian and Ubuntu on how to install PHP 8.4 on a new server or how to upgrade an existing PHP setup to PHP 8.4.
How to fix `mysql_native_password` not loaded errors on MySQL 8.4

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to fix the SQLSTATE[HY000] [1524] Plugin 'mysql_native_password' is not loaded errors caused in MySQL 8.4 no longer enabling the mysql_native_password plugin by default.
How to fix PHP Curl HTTPS Certificate Authority issues on Windows

How to fix PHP Curl HTTPS Certificate Authority issues on Windows

On Windows, HTTPS requests made with the Curl extension can fail because Curl has no root certificate list to validate the server certificates. This article discusses the secure and effective solutions, and highlights bad advice that can leave PHP applications insecure.
Subscribe to PHP.Watch newsletter for monthly updates

You will receive an email on last Wednesday of every month and on major PHP releases with new articles related to PHP, upcoming changes, new features and what's changing in the language. No marketing emails, no selling of your contacts, no click-tracking, and one-click instant unsubscribe from any email you receive.

Support PHP.Watch — If you find the articles, version information, Codex, and other PHP.Watch contributions useful, consider supporting through GitHub Sponsors. Your sponsorship helps dedicate more time to creating valuable content and improving the PHP community. Together, we can keep the momentum going — thank you for your support!

Thanks to the highest tier sponsor: @TomasVotruba for your generous support to keep PHP.Watch moving 💜