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
- Update Package List:
sudo apt update
- 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
- 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.
- 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
- 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. - 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:
- 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
- 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>
- Open the virtual host configuration file for your site:
- 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.
- Edit Your Server Block File:
sudo nano /etc/nginx/sites-available/your-site.conf
- 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; }
- For PHP 8.1:
- 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:
- Run the PHP version directly:
php8.1 artisan serve php8.2 artisan serve
- 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
- Install multiple PHP versions (e.g., PHP 8.1, 8.2).
- Use
update-alternatives
to switch between PHP versions for CLI. - Configure Apache/Nginx to use specific PHP versions for individual sites.
- Use per-project settings for PHP CLI if required.
Let me know if you need help configuring anything! 🚀