How to Install PHP 5.6, PHP 7.0, and PHP 7.2 on Ubuntu

23 Apr

Are you looking to install multiple PHP versions on your Ubuntu system? Managing different PHP versions can be crucial for various development environments. This guide will walk you through the process of installing PHP 5.6, PHP 7.0, and PHP 7.2 on an Ubuntu system. We will also cover how to switch between these versions seamlessly.

Step-by-Step Guide to Install PHP 5.6, PHP 7.0, and PHP 7.2 on Ubuntu

Prerequisites

Before we begin, ensure your system meets the following prerequisites:

  • A running instance of Ubuntu (preferably a LTS version for stability).
  • Root or sudo access to perform administrative tasks.

Step 1: Disable Unnecessary Apache Modules

To avoid conflicts, it’s essential to disable certain Apache modules if they are enabled. Run the following command:

sudo a2dismod proxy_fcgi proxy
sudo service apache2 restart

Step 2: Add the Ondřej Surý PPA

Ondřej Surý maintains a widely-used PPA (Personal Package Archive) with various PHP versions. Add this repository to your system:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

Step 3: Install PHP Versions

Install PHP 5.6 and PHP 7.0 along with their common extensions. This setup also includes PHP 7.2:

sudo apt-get install php5.6 php7.0 php7.2 php5.6-mysql php-gettext php5.6-mbstring php-mbstring php7.0-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0 libapache2-mod-php7.2

Step 4: Switching PHP Versions

To switch between PHP versions, you need to configure both Apache and the Command Line Interface (CLI).

Switching from PHP 5.6 to PHP 7.0

Apache:

sudo a2dismod php5.6
sudo a2enmod php7.0
sudo service apache2 restart

CLI:

sudo update-alternatives --set php /usr/bin/php7.0

Switching from PHP 7.0 to PHP 5.6

Apache:

sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart

CLI:

sudo update-alternatives –set php /usr/bin/php5.6

Switching from PHP 7.2 to PHP 5.6

Apache:

sudo a2dismod php7.2
sudo a2enmod php5.6
sudo service apache2 restart

CLI:

sudo update-alternatives --set php /usr/bin/php5.6

Ensuring a Smooth PHP Environment

After switching PHP versions, it’s crucial to ensure that your web applications and scripts are compatible with the version in use. Here are some tips to maintain a smooth PHP environment:

Testing Your PHP Installation

Create a simple PHP info file to test which PHP version is currently active:

  • Navigate to your web server’s root directory (e.g., /var/www/html).
  • Create a file named info.php with the following content:
<?php
phpinfo();
?>
  • Open your web browser and navigate to http://your-server-ip/info.php. This page will display detailed information about the PHP version and modules currently active.

Managing PHP Extensions

Each PHP version may require different extensions for your applications to run correctly. Use the following commands to install or enable extensions:

For PHP 5.6:

sudo apt-get install php5.6-<extension>

For PHP 7.0:

sudo apt-get install php7.0-<extension>

For PHP 7.2:

sudo apt-get install php7.2-<extension>

Replace <extension> with the desired extension name (e.g., curl, gd, intl).

Troubleshooting Common Issues

Issue: PHP Version Not Switching Correctly

  • Ensure you restart Apache after switching versions.
  • Verify the CLI version by running php -v.

Issue: Missing Extensions

  • Confirm the extension is installed and enabled for the active PHP version.
  • Check the PHP error logs for more detailed error messages.

Automating PHP Version Switching

For developers frequently switching PHP versions, automating the process can save time. Consider creating bash scripts to handle the commands:

Example Script to Switch to PHP 5.6:

#!/bin/bash
sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart
sudo update-alternatives --set php /usr/bin/php5.6

Example Script to Switch to PHP 7.0:

#!/bin/bash
sudo a2dismod php5.6
sudo a2enmod php7.0
sudo service apache2 restart
sudo update-alternatives --set php /usr/bin/php7.0

Save these scripts in a convenient location and grant execute permissions:

chmod +x switch-to-php5.6.sh
chmod +x switch-to-php7.0.sh

Run the script whenever you need to switch PHP versions.

Conclusion

Installing and managing multiple PHP versions on Ubuntu is straightforward with the right steps. By using the Ondřej Surý PPA, you can easily install PHP 5.6, PHP 7.0, and PHP 7.2. Switching between these versions for both Apache and CLI is essential for maintaining different development environments. Always test your applications after switching versions to ensure compatibility and smooth functionality. Follow this guide, and you’ll have a flexible PHP environment up and running on your Ubuntu system.