Bootstrap FreeKB - PHP - Compile PHP as an Apache module
PHP - Compile PHP as an Apache module

Updated:   |  PHP articles

Compiling PHP as an Apache module basically means you will download and install Apache HTTPD and then download and run a series of commands that will create the PHP module file phplib.so. The phplib.so file is then loaded in Apache as a module which configures Apache with PHP.

First you will need to install Apache (or HTTPD).

Then download the .tar.gz or tar.bz2 file for the version of PHP that you want to compile from source from https://www.php.net/downloads.php. For example, let's say I download php-8.2.8.tar.gz. wget can be used.

AVOID TROUBLE

The system you compile PHP on should be as similar as possible as the other systems that the PHP module will be deployed to. For example, if you compile PHP on a Red Hat 7 system and then try to run it on a Red Hat 8 system, don't be surprised if errors are returned.

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

 

Use the tar extract command to extract the TAR file to the /tmp.

tar -zxpf php-8.2.8.tar.gz --directory /tmp

 

You can now remove the tar.gz file that you downloaded.

rm php-8.2.8.tar.gz

 

Move into the extracted directory.

cd /tmp/php-8.2.8/

 

The README.md file suggests installing the following packages on a Debian distribution (Ubuntu, Mint) using apt install.

sudo apt install autoconf
sudo apt install bison
sudo apt install build-essential
sudo apt install libxml2-dev
sudo apt install libsqlite3-dev
sudo apt install pkg-config
sudo apt install re2c

 

Or these packages on a Red Hat distribution (CentOS, Fedora, Red Hat) using dnf install or yum install.

sudo dnf install autoconf 
sudo dnf install bison 
sudo dnf install ccache 
sudo dnf install libtool 
sudo dnf install libxml2-devel 
sudo dnf install make 
sudo dnf install re2c 
sudo dnf install sqlite-devel

 

Create a directory that will contain the files created by the make install command.

mkdir /tmp/php_make_install

 

Run the ./configure command and include the path to your Apache HTTPD apxs file. APXS is the abbreviation for Apache Extensions.

./configure --prefix=/tmp/php_make_install --with-apxs2=/path/to/bin/apxs --with-libdir=lib64

 

Optionally, you can run configure with additional features. You'll probably need to ensure you have the devel packages installed.

sudo dnf install openssl-devel libcurl-devel

 

And then configure with the additional --with-<package> flags.

  • APXS is the abbreviation for Apache Extensions.
  • --enable-maintainer-zts is used if compiling PHP 7 and --enable-zts is used if compiling PHP 8 - this option ensure that PHP is compiled as threadsafe so that it will work with the Multi-Processing Module (MPM) module.
./configure \
--prefix=/tmp/php_make_install \
--with-apxs2=/path/to/bin/apxs \
--with-libdir=lib64 \
--with-ldap \
--with-openssl \
--with-mysqli \
--with-curl \
--with-pdo-odbc=unixODBC,/usr/ \
--enable-zts (for PHP 8 or --enable-maintainer-ztz for PHP 7)

 

If configure was successful, the following should be displayed at the end of the output.

Thank you for using PHP.

 

Run make.

sudo make

 

If make was successful, something like this should be displayed.

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

 

Run make install.

sudo make install

 

If make install was successful, something like this should be displayed.

Installing shared extensions:     /tmp/makeinstall/lib/php/extensions/no-debug-non-zts-20220829/
Installing PHP CLI binary:        /tmp/makeinstall/bin/
Installing PHP CLI man page:      /tmp/makeinstall/php/man/man1/
Installing phpdbg binary:         /tmp/makeinstall/bin/
Installing phpdbg man page:       /tmp/makeinstall/php/man/man1/
Installing PHP CGI binary:        /tmp/makeinstall/bin/
Installing PHP CGI man page:      /tmp/makeinstall/php/man/man1/
Installing build environment:     /tmp/makeinstall/lib/php/build/
Installing header files:          /tmp/makeinstall/include/php/
Installing helper programs:       /tmp/makeinstall/bin/
  program: phpize
  program: php-config
Installing man pages:             /tmp/makeinstall/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PDO headers:           /tmp/makeinstall/include/php/ext/pdo/

 

And the libphp.so module should exist in the Apache HTTPD modules directory.

~]$ ls -long /path/to/modules/
-rwxr-xr-x. 1 admin admin 49442472 Jul 26 00:47 libphp.so

 

The php --version command show show the version of PHP you installed.

]$ /tmp/makeinstall/bin/php --version
PHP 8.2.8 (cli) (built: Jul 21 2023 10:57:04) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.8, Copyright (c) Zend Technologies

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e51888 in the box below so that we can be sure you are a human.