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.
While installing PHP via YUM is straightforward, compiling from source offers the following benefits:
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.
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>
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.
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.
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.
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.