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

 

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 -y pkg-config build-essential autoconf bison re2c libxml2-dev libsqlite3-dev

 

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

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

 

Create a diretory 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/makeinstall --with-apxs2=/path/to/bin/apxs --with-libdir=lib64

 

Optionally, you can configure with additional features, such as OpenSSL and curl. 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.

./configure --prefix=/tmp/makeinstall --with-apxs2=/path/to/bin/apxs --with-libdir=lib64 --with-openssl --with-curl --with-mysqli

 

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 67920a in the box below so that we can be sure you are a human.