How to Install PHP 5.6 on Ubuntu 24.04

PHP 5.6 is an older version of PHP and is not supported by the latest Ubuntu repositories by default. However, it can still be installed on Ubuntu 24.04 using a third-party repository. This guide will walk you through the process.

Step 1: Update the Package List

Before installing any new software, it’s good practice to update the package list to ensure you’re working with the latest information.

Open your terminal and run:

sudo apt update

Step 2: Install Software Properties Common

You need to install software-properties-common to manage your PPAs (Personal Package Archives).

sudo apt install software-properties-common

Step 3: Add the PHP 5.6 PPA

Since PHP 5.6 is not available in the default Ubuntu repositories, you’ll need to add a PPA (Personal Package Archive) that contains older PHP versions. The most commonly used PPA for this is maintained by Ondřej Surý.

Run the following command to add the PPA:

sudo add-apt-repository ppa:ondrej/php

When prompted, press Enter to confirm adding the PPA.

Step 4: Update the Package List Again

After adding the new PPA, update the package list again to include packages from the newly added repository.

sudo apt update

Step 5: Install PHP 5.6

Now that the PPA is added and the package list is updated, you can install PHP 5.6.

Run the following command:

sudo apt install php5.6

Step 6: Install PHP 5.6 Modules (Optional)

Depending on your needs, you may need additional PHP modules. Here are some common PHP modules you might want to install:

sudo apt install php5.6-mysql php5.6-curl php5.6-json php5.6-cgi php5.6-xml php5.6-mbstring

You can adjust the list of modules based on the requirements of your applications.

Step 7: Verify PHP 5.6 Installation

Once PHP 5.6 is installed, you should verify that it was installed correctly.

Run:

php -v

This command should output something like:

PHP 5.6.x (cli) (built: ...)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.x, Copyright (c) 1998-2016 Zend Technologies

Step 8: Configure Apache or Nginx to Use PHP 5.6 (If Applicable)

If you are using Apache or Nginx as your web server, you will need to configure it to use PHP 5.6. Below are the instructions for Apache; if you’re using Nginx, the process is slightly different.

For Apache:

  1. Disable the current PHP module:
    sudo a2dismod php8.3
  2. Enable the PHP 5.6 module:
    sudo a2enmod php5.6
  3. Restart Apache to apply the changes:
    sudo systemctl restart apache2

For Nginx:

For Nginx, you will need to update the fastcgi_pass directive in your site’s configuration file to point to the PHP 5.6 FPM socket.

  1. Install PHP 5.6 FPM:
    sudo apt install php5.6-fpm
  2. Edit your Nginx server block (e.g., /etc/nginx/sites-available/default) and change the fastcgi_pass line:
    fastcgi_pass unix:/run/php/php5.6-fpm.sock;
  3. Restart Nginx:
    sudo systemctl restart nginx

Step 9: Test PHP 5.6 on Your Server

To ensure everything is working correctly, create a simple PHP file in your web server’s root directory.

sudo nano /var/www/html/info.php

Add the following line to this file:

<?php phpinfo(); ?>

Save and close the file, then open your web browser and navigate to http://your_server_ip/info.php.

You should see a page displaying your PHP configuration details, confirming that PHP 5.6 is installed and functioning correctly.

Step 10: Remove the PHP Info File (For Security)

Once you’ve verified that PHP is working, it’s best to delete the info.php file, as it can expose sensitive information about your server’s configuration.

sudo rm /var/www/html/info.php

Conclusion

You have now successfully installed PHP 5.6 on Ubuntu 24.04. Remember that PHP 5.6 is no longer supported, so it’s recommended to use it only if absolutely necessary, such as for legacy applications. Where possible, consider updating your codebase to work with a supported PHP version.