Tomonori Takanawa's Blog
Writing about Tech, Software Development and Gadgets.

How to build old PHP

How to build old PHP
photo by Laura Kay

TL;DR You can install and configure the PHP which is old, outdated, unsupported and archived on your linux

This post is the steps I’ve taken in my previous I built PHP post.

My environment

  • OS: Ubuntu 20.04.1 LTS (WSL2)
  • Japan

OK, let’s get started it.

First, Install C compiler and Make beforehand.

sudo apt install -y gcc make

Download your target version from Unsupported Historical Releases.

I will use PHP 7.2.32 (tar.gz) as an example.

curl -Lso php-7.2.32.tar.gz https://www.php.net/distributions/php-7.2.32.tar.gz

Unzip the source code to /usr/local/src.

tar xfz php-7.2.32.tar.gz -C /usr/local/src/

Go to the unzipped directory.

cd /usr/local/src/php-7.2.32

Generate Makefile. PHP has so many options, so you should look up and optimize your settings, but you could try this options at first. Don’t worry. the options you used will be recorded on the Makefile, so you can check them later. That’s very useful, isn’t it?

sudo ./configure \
    --prefix=/usr/local/php-7.2.32 \
    --with-apxs2=/usr/bin/apxs \
    --with-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --enable-mbstring \
    --with-openssl

I think you would encounter some errors, but you could manage those.

I will describe the errors I faced and the steps I took to resolve them.

the errors

1. Perl is not installed
2. apxs was not found. Try to pass the path using --with-apxs2=/path/to/apxs
3. Apache was not built using --enable-so (the apxs usage page is displayed)

fixed them

sudo apt install -y perl
which apxs
=> Replace --with-apxs2 option to the output. (/usr/bin/apxs)
sudo apt install -y apache2-dev

the error

configure: error: libxml2 not found. Please check your libxml2 installation.

fixed it

sudo apt install -y libxml2-dev

the error

configure: error: Cannot find OpenSSL's <evp.h>

fixed it

sudo apt install -y libssl-dev

the error

configure: error: Cannot find OpenSSL's libraries

fixed it

sudo apt install -y pkg-config

Build PHP using the Makefile you generated above.

make

Test the PHP just in case.

make test

Install PHP.

make install

If you encounter an error when you install PHP according to the above, install apache and install PHP again.

apxs:Error: Config file /etc/apache2/mods-available not found.
make: *** [Makefile:153: install-sapi] Error 1
sudo apt upgrade -y apache2

I want to use several versions of PHP depending on the situation, so set up the symbolic link.

First, check where the PHP. It is in the location you specified when you ran the configure script. The following is /usr/local/php-7.2.32.

sudo ./configure --prefix=/usr/local/php-7.2.32

And check the PHP version just in case.

/usr/local/php-7.2.32/bin/php -v

Generate the symbolic link.

sudo update-alternatives --install /usr/bin/php php /usr/local/php-7.2.32/bin/php 72

Switch your PHP

update-alternatives --config php

That’s all. From now on, you can install another version following the above instructions.

If you have found some mistakes and unknown points, I’d be happy to me know tweeting at me.