Current Location: Home> Latest Articles> Comprehensive Guide to Compiling PHP on CentOS 7

Comprehensive Guide to Compiling PHP on CentOS 7

gitbox 2025-06-30

Guide to Compiling PHP on CentOS 7

PHP is a widely-used server-side scripting language, particularly in web development. Compiling PHP manually on CentOS 7 provides better customization options, making it ideal for scenarios where specific modules or configurations are needed.

Why Compile PHP Manually

While installing PHP via YUM is straightforward, compiling from source offers the following benefits:

  • Choose exactly which extensions and modules to include
  • Optimize configuration for better performance
  • Install the latest PHP version with the newest features and security updates

Preparation: Install Dependencies

Start by installing the required packages for building PHP. Run the following command in your terminal:

<span class="fun">sudo yum install -y gcc make autoconf bison re2c libxml2-devel curl-devel libpng-devel libjpeg-devel libXpm-devel gmp-devel mariadb-devel aspell-devel recode-devel</span>

These packages are essential for the PHP compilation process.

Download and Extract PHP Source Code

Go to the official PHP website and download the latest source code. For example, to get PHP 8.1.x:

<span class="fun">wget https://www.php.net/distributions/php-8.1.X.tar.gz</span>

Then extract the archive:

<span class="fun">tar -xzvf php-8.1.X.tar.gz</span>

Navigate into the extracted directory:

<span class="fun">cd php-8.1.X</span>

Configure PHP Build Options

Use the ./configure script to define the desired build configuration:

./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php \
--enable-mbstring \
--with-curl \
--with-mysqli

You can add or remove options according to your project requirements.

Compile and Install PHP

Once configured, start the compilation process. This may take several minutes depending on your system:

make
sudo make install

After compilation, PHP will be available in the target directory, e.g., /usr/local/php.

Verify PHP Installation

To confirm that PHP was installed correctly, run:

<span class="fun">/usr/local/php/bin/php -v</span>

You should see the PHP version output if everything went well.

Conclusion

This guide has walked you through the full process of compiling PHP on CentOS 7 — from installing dependencies and downloading the source code to configuring, compiling, and verifying the installation. Manual compilation is ideal for developers needing more control over their PHP environment and a deeper understanding of the build process.