Installing PHP 5.3 on Modern Ubuntu (e.g., 22.04)

November 15, 2024
Installing PHP 5.3 on Modern Ubuntu (e.g., 22.04)

If you support legacy applications like I do, you'll sometimes need to support older version of php. PHP 5.3 is an outdated and unsupported version. Do this for legacy applications only in controlled environments.

You have two options: 

  • Using Docker (recommended)
  • Installing it natively (not recommended)

Using Docker (recommended)

Install Docker

sudo apt update
sudo apt install docker.io -y

Pull a PHP 5.3 Docker Image

docker pull php:5.3-apache


Test

Create a file index.php in your desired application directory (e.g., /path/to/your/app) with the following content:

<?php
echo '<h1>PHP Version: ' . phpversion() . '</h1>';
?>

Run the Docker Container

docker run -d -p 8080:80 -v /path/to/your/app:/var/www/html --name php53-container php:5.3-apache

In the previous command, --name php53-container: Assigns the name php53-container to the container

Be sure to replace /path/to/your/app with your application directory

Access your app by visiting http://localhost:8080 in your browser, if you used the example index.php file I provided, you should see "PHP Version: 5.3.29"

Native Installation (not recommended)

Prepare the system

Switch to Root

sudo su

Generate Locale

locale-gen en_US.UTF-8

Remove Existing PHP and Apache

Backup Configuration Files

cp -R /etc/php /etc/php_backup
cp -R /etc/apache2 /etc/apache2_backup

List Existing PHP Packages

dpkg -l | grep php

Remove PHP and Apache (Replace <php-packages> with the actual package names from the previous step):

apt purge <php-packages> apache2 -y

Add Legacy Repositories

Since PHP 5.3 isn't in modern repositories you'll need to manually download .deb files for PHP 5.3 from an archive or PPA like ppa:ondrej/php

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php5.3 apache2

Test by creating an index phage to display the PHP version

Navigate to the default web root:

cd /var/www/html

Remove any default index.html:

rm index.html

Create a new index.php file

echo "<?php echo '<h1>PHP Version: ' . phpversion() . '</h1>'; ?>" > index.php

Restart Apache and Verify

sudo systemctl restart apache2

Open your browser and navigate to http://localhost or the server's IP address.
You should see a page displaying the PHP version.