Multiple PHP Versions with Apache on Ubuntu

Want to use multiple PHP versions on Ubuntu? Don’t worry — with Ubuntu you can easily switch between PHP versions.

This special feature only works on Ubuntu because it depends on the PPA repository. And we have Ondřej Surý to thank for creating this repository and maintaining it so well.

For other distros, you need to build from the source code, and that will probably be covered in a different article.

  1. Ondřej Surý’s PPA repository
    sudo add-apt-repository ppa:ondrej/php
    sudo add-apt-repository ppa:ondrej/apache2
    sudo apt update
  2. Install Apache
    sudo apt update
    sudo apt install apache2
  3. Install PHP 8.3
    sudo apt install php8.3 php8.3-cli libapache2-mod-php8.3 php8.3-mysql php8.3-pgsql
  4. Install PHP 7.4
    sudo apt install php7.4 php7.4-cli libapache2-mod-php7.4 php7.4-mysql php7.4-pgsql
  5. Switch the PHP version on the CLI
    sudo update-alternatives --config php

FAQ

Can this be used on WSL 2 Ubuntu?

Yes, and this tutorial uses WSL 2 Ubuntu. But note that it requires the latest version of Windows, because it needs SystemD, which is only available in the newer WSL versions.

References

  1. https://www.digitalocean.com/community/tutorials/how-to-run-multiple-php-versions-on-one-server-using-apache-and-php-fpm-on-ubuntu-20-04
  2. https://launchpad.net/~ondrej/+archive/ubuntu/php
  3. https://ubuntu.com/tutorials/install-and-configure-apache#1-overview
  4. https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu

Leave a comment

Your email address will not be published. Required fields are marked *