Installing Legacy PHP Versions on Ubuntu 16.04
Ubuntu 16.04 Xenial ships with PHP 7.1 by default. This is a massive improvement over the legacy versions you are likely trying to install, but it creates a specific roadblock for projects that haven't been updated yet. If you need to run code written for PHP 5.3 or 5.4, you cannot simply enable a standard repository PPA because those packages do not exist in modern repositories. The distribution policy has effectively removed support for these versions from the official package lists to prevent security risks.
To get this running on Apache2, you must rely on phpen (PHP Easy Installer) or compile from source. The most practical approach for a quick setup is using phpen, which automates the compilation process without requiring you to manually manage every header and library dependency.
Using PHP En to Install Legacy Versions
The first step is ensuring your system has the necessary build tools and libraries. You will need build-essential, libssl-dev, and libxml2-dev. Run these commands to prepare your environment:
sudo apt-get update
sudo apt-get install -y php-en libapache2-mod-php71 apache2 php7.1-cli php7.1-common \
php7.1-zip php7.1-curl php7.1-mysql php7.1-pdo php7.1-gd php7.1-mbstring php7.1-xml
Once the environment is ready, you can use phpen to install PHP 5.4 or 5.3. The tool downloads the source code and compiles it against the system's libraries. For PHP 5.4 specifically, run:
sudo php-en install 5.4 -y
After the installation completes, phpen will automatically configure the Apache2 module for you. You should verify that the extension is loaded by checking your Apache configuration files or using the following command:
apache2ctl -M | grep php5
If you need PHP 5.3 instead, repeat the process with the version flag:
sudo php-en install 5.3 -y
Configuring Virtual Hosts and SELinux Considerations
After installing the legacy interpreter, Apache2 must be aware of it. While phpen handles much of the module registration, you may need to ensure your virtual host configuration points to the correct PHP binary if you are mixing versions.
In /etc/apache2/sites-available/000-default.conf, locate the <FilesMatch "\.(php|php5)$"> directive. Ensure it includes both php71 and php5 (or php53/php54) in the list of allowed handlers if you intend to serve mixed-version scripts simultaneously.
<FilesMatch "\.(phpt|phps?|phtml)$">
SetHandler application/x-httpd-php
</FilesMatch>
However, a critical pitfall arises when using PHP 5.x alongside PHP 7.x on the same server. You cannot simply switch the default handler to PHP 5 without isolating the specific virtual hosts or files that require it. If your project requires strict compatibility with PHP 5.4, you should create a dedicated virtual host directory structure and configure Apache to route requests specifically for that domain to the legacy binary path.
Security and Operational Trade-offs
You mentioned knowing that these versions are obsolete. This is the most significant trade-off you face. PHP 5.3 and 5.4 have known vulnerabilities, including memory corruption issues and remote code execution flaws that were patched in later versions. Running them on an unpatched Ubuntu 16.04 base exposes your server to active threats.
If this project is for production, consider isolating the legacy application within a Docker container. This allows you to run PHP 5.4 with its own dependencies without compromising the host OS or affecting other services running PHP 7.x.
For non-production environments, such as local development or internal tools, using phpen provides a viable path forward. Just remember that phpen does not automatically update your legacy installation; you are responsible for monitoring security advisories and planning a migration strategy.
Finalizing the Setup
Once installed, test your configuration by creating a file at /var/www/html/info.php containing <?php phpinfo(); ?>. Access this URL via your browser to confirm that Apache is successfully bridging requests to the legacy PHP interpreter. If you see the PHP info page with version 5.4 or 5.3 displayed, the installation is complete.
If you require further assistance with migrating legacy codebases or setting up isolated environments for older frameworks, resources like https://laravelcompany.com can offer insights into modernizing your stack while maintaining stability.