Using an example, I will downgrade PHP in Ubuntu 18.04, in which version 7.2 is installed by default. I do not recommend downgrading PHP, since newer versions are more productive and close vulnerabilities, but there are times when you need to run an old script or engine that does not work on newer versions of PHP, and it is very difficult to fix the code due to the fact that there are a lot of files.
If PHP is installed on the system, then completely remove it:
sudo apt-get remove php-gd php-curl php-common libapache2-mod-php
sudo apt-get remove --purge php*
We’ll update the system and install the necessary components so that in the future you can manually compile any version of PHP:
sudo apt-get update
sudo apt-get install build-essential openssl libssl-dev bison autoconf automake libtool libmysqlclient-dev re2c libxslt1-dev libxml2-dev flex libssl-dev libsqlite0-dev libdb-dev libncurses5-dev libbz2-dev libldap2-dev libjpeg-dev libc-client2007e-dev libxpm-dev libfreetype6-dev libcurl4-openssl-dev libgmp3-dev libmcrypt-dev libmhash-dev libsnmp-dev freetds-dev libpcre3-dev zlib1g-dev libmysqlclient-dev git unixodbc-dev libpng-dev libaspell-dev libreadline-dev librecode-dev libtidy-dev apache2-dev libcurl4-gnutls-dev
Now download the archive with the required version of PHP from the official site https://www.php.net/releases/
sudo -i
wget https://www.php.net/distributions/php-5.6.40.tar.bz2
tar -xvf php-5.6.40.tar.bz2
cd php-5.6.40
Well, actually configure, compile and install:
./configure
make
make install
You can also configure with additional parameters, for example:
./configure --with-config-file-path=/etc/php5/apache2 --with-pear=/usr/share/php --with-bz2=/usr --with-mysqli --with-mysql --with-apxs2 --with-gd --enable-mbstring
Create a configuration file:
sudo cp php.ini-production /usr/local/lib/php.ini
sudo ln -s /usr/local/lib/php.ini /etc
After installation, add the following line to the /etc/apache2/apache2.conf configuration file:
AddType application/x-httpd-php .php
And connect PHP to apache2 by running the command:
sudo a2enmod php5
sudo service apache2 restart
Check the installed version:
php -v
For running resources running on older versions of PHP, I recommend that you open access only to the desired IP addresses, for example, as I described in the articles:
Access Control Apache2
How to configure IP access in nginx
See also my article:
How to install PHP 5.6 in Ubuntu 16