DEV Community

Cover image for How to Install and Manage Multiple PHP Versions (7.4 - 8.4) on Ubuntu with Apache.
M. K. Tanjin Sarker
M. K. Tanjin Sarker

Posted on

How to Install and Manage Multiple PHP Versions (7.4 - 8.4) on Ubuntu with Apache.

πŸš€ PHP Multiple Versions (7.4 β†’ 8.4) with Apache on Ubuntu

1️⃣ Add PHP PPA

sudo apt update
sudo apt install -y software-properties-common ca-certificates lsb-release apt-transport-https
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
Enter fullscreen mode Exit fullscreen mode

2️⃣ Install PHP Versions

# PHP 7.4
sudo apt install -y php7.4 php7.4-cli php7.4-fpm

# PHP 8.0
sudo apt install -y php8.0 php8.0-cli php8.0-fpm

# PHP 8.1
sudo apt install -y php8.1 php8.1-cli php8.1-fpm

# PHP 8.2
sudo apt install -y php8.2 php8.2-cli php8.2-fpm

# PHP 8.3
sudo apt install -y php8.3 php8.3-cli php8.3-fpm

# PHP 8.4
sudo apt install -y php8.4 php8.4-cli php8.4-fpm
Enter fullscreen mode Exit fullscreen mode

3️⃣ Install Important Extensions

# Replace X.X with version (7.4, 8.0, 8.1, 8.2, 8.3, 8.4)
sudo apt install -y phpX.X-cli phpX.X-fpm phpX.X-mysql phpX.X-xml phpX.X-curl phpX.X-mbstring phpX.X-zip phpX.X-bcmath phpX.X-gd phpX.X-intl phpX.X-readline phpX.X-soap phpX.X-opcache phpX.X-dev phpX.X-imagick phpX.X-pgsql phpX.X-sqlite3
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Covers Laravel, Symfony, WordPress, Drupal, Magento, APIs, image processing, DB drivers, and performance.

4️⃣ Check Installed Versions

php -v                              # Current CLI version
update-alternatives --list php      # All installed CLI versions
systemctl list-units | grep php     # Running PHP-FPM services
php -m                              # Installed extensions
Enter fullscreen mode Exit fullscreen mode

5️⃣ Switch PHP (CLI)

Interactive:

sudo update-alternatives --config php
Enter fullscreen mode Exit fullscreen mode

Direct:

sudo update-alternatives --set php /usr/bin/php8.3
Enter fullscreen mode Exit fullscreen mode

Verify:

php -v
Enter fullscreen mode Exit fullscreen mode

6️⃣ Switch PHP (Apache FPM)

Enable/disable PHP-FPM

sudo systemctl stop php7.4-fpm
sudo systemctl enable --now php8.3-fpm
Enter fullscreen mode Exit fullscreen mode

Enable Apache modules/config

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.3-fpm
sudo a2disconf php7.4-fpm
sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

VirtualHost Example

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost/"
    </FilesMatch>
Enter fullscreen mode Exit fullscreen mode

Reload Apache:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

7️⃣ Test PHP

Create /var/www/html/info.php:

<?php phpinfo();
Enter fullscreen mode Exit fullscreen mode

Open in browser:

http://localhost/info.php
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Quick Reference

  • Switch CLI PHP
  sudo update-alternatives --config php
Enter fullscreen mode Exit fullscreen mode
  • Switch Apache PHP-FPM
  sudo systemctl stop php8.0-fpm
  sudo systemctl start php8.2-fpm
  sudo a2enconf php8.2-fpm
  sudo a2disconf php8.0-fpm
  sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

βœ… With this setup, you can:

  • Install PHP 7.4 β†’ 8.4
  • Enable required extensions
  • Switch between versions for CLI & Apache
  • Verify with php -v and phpinfo()

Top comments (0)