Do not run this on a public facing webserver.
This version of PHP has some rather ugly security problems.
Update your scripts to work with a newer php version. Anything else is plain stupid, and should only be used to verify functionality during porting of scripts.
First we'll have to install Apache development files, a few misc build dependants and build essentials:
sudo apt install apache2 apache2-dev build-essentials libxml2-dev libmariadbclient-dev-compat checkinstall
Now we can continue with the tutorial; Copy paste mostly, with some modifications:
cd /tmp && wget http://in1.php.net/distributions/php-5.3.29.tar.bz2
tar -xvf php-5.3.29.tar.bz2
./configure --with-mysql --with-apxs2=/usr/bin/apxs --with-tsrm-pthreads --enable-maintainer-zts --with-libdir=/usr/lib/x86_64-linux-gnu/ #Note extra options cf. other answer
sudo a2enmod php5
Finally, you'll have to add the line
AddType application/x-httpd-php .php
/etc/apache2/apache2.conf using your favorite editor. Add it near the bottom.
After that, do a
sudo service apache2 restart
And php 5.3.29 should be installed.
The above was tested on 18.04. You probably want other php modules as well, which has to be enabled with
./configure. Which depends on the code you plan to run.