Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

Mastering Multiple PHP Versions on Ubuntu: A Comprehensive Guide

linux

Using multiple PHP versions on Ubuntu is straightforward, as Ubuntu allows you to install multiple versions of PHP side by side. You can switch between them using tools like update-alternatives. Here’s a step-by-step guide:


1️⃣ Install Multiple PHP Versions

  1. Update Package List: sudo apt update
  2. Add the Ondřej PPA (if not already added): The Ondřej PPA provides multiple PHP versions. sudo add-apt-repository ppa:ondrej/php sudo apt update
  3. Install the PHP Versions You Need: For example, to install PHP 8.1 and PHP 8.2: sudo apt install php8.1 php8.1-cli php8.1-fpm php8.1-mbstring php8.1-xml php8.1-curl php8.1-zip sudo apt install php8.2 php8.2-cli php8.2-fpm php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip

2️⃣ Configure update-alternatives to Manage PHP Versions

update-alternatives is a tool to manage multiple versions of the same command.

  1. Set Alternatives for PHP: sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.1 81 sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.2 82
  2. Switch PHP Versions Manually: Run the following command and select the version you want to use: sudo update-alternatives --config php You’ll see a list of installed PHP versions, for example: There are 2 choices for the alternative php (providing /usr/bin/php). Selection Path Priority Status ------------------------------------------------------------ 1 /usr/bin/php8.1 81 manual mode 2 /usr/bin/php8.2 82 manual mode Press <enter> to keep the current choice[*], or type selection number: Enter the number corresponding to the PHP version you want to use.
  3. Verify the Active PHP Version: php -v

3️⃣ Configure Different PHP Versions for Apache

If you’re using Apache as your web server and want different sites to use different PHP versions:

  1. Enable the PHP Modules for Apache: sudo a2dismod php8.1 # Disable the current PHP version sudo a2enmod php8.2 # Enable the desired PHP version sudo systemctl restart apache2
  2. Set PHP Version Per Virtual Host: If you have multiple sites and want each to use a different PHP version:
    • Open the virtual host configuration file for your site: sudo nano /etc/apache2/sites-available/your-site.conf
    • Add the following lines inside the <VirtualHost> block for the desired PHP version: <FilesMatch \.php$> SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost/" </FilesMatch>
    • For another site, use: <FilesMatch \.php$> SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost/" </FilesMatch>
  3. Restart Apache: sudo systemctl restart apache2

4️⃣ Configure PHP Versions for Nginx

If you’re using Nginx, configure the desired PHP version per server block.

  1. Edit Your Server Block File: sudo nano /etc/nginx/sites-available/your-site.conf
  2. Update fastcgi_pass to Use the Desired PHP Version:
    • For PHP 8.1: location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; }
    • For PHP 8.2: location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; }
  3. Restart Nginx and PHP-FPM: sudo systemctl restart nginx sudo systemctl restart php8.1-fpm php8.2-fpm

5️⃣ CLI-Level Per-Project PHP Version

To use a specific PHP version in a project without changing the global configuration:

  1. Run the PHP version directly: php8.1 artisan serve php8.2 artisan serve
  2. Use tools like Symfony’s Local PHP Version: Install Symfony CLI: curl -sS https://get.symfony.com/cli/installer | bash export PATH="$HOME/.symfony/bin:$PATH" Set the PHP version per directory: symfony local:php:set 8.1

6️⃣ Verify PHP Versions

  • Check the installed PHP versions: ls /usr/bin/php*
  • Verify active PHP CLI version: php -v

Summary

  1. Install multiple PHP versions (e.g., PHP 8.1, 8.2).
  2. Use update-alternatives to switch between PHP versions for CLI.
  3. Configure Apache/Nginx to use specific PHP versions for individual sites.
  4. Use per-project settings for PHP CLI if required.

Let me know if you need help configuring anything! 🚀

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x