We know that one PHP version just isn’t enough. I found this out when I was running more than one website on the same server. One site needed PHP 7.4. Another needed PHP 8.2. At first, I thought I had to pick one and forget the other. But I learned there’s a way to make Apache use more than one PHP version at the same time.
Apache is a web server. It helps your websites load when people visit them. PHP is a language used to build websites, especially ones like WordPress.
Each version of PHP is a little different. Some websites need a newer version to work right. Others break if you try to use the latest one. So if you run many websites, you might need to set things up so each one uses its own PHP version.
Let me show you how I set this up on my server. It’s not as tricky as it sounds.
Why Would You Want Multiple PHP Versions?
Here are a few reasons why I needed more than one PHP version:
- I had one old website that only worked on PHP 7.4.
- I built a new app that needed PHP 8.2 to run.
- I didn’t want to break any working sites by updating PHP.
Maybe you’ve run into something like this too. Or maybe you’re testing software that works with different PHP versions.
What You’ll Need
Before we begin, here’s what you should have ready:
- A server with Apache installed (I used Ubuntu 22.04).
- Two or more PHP versions installed (I used PHP 7.4 and PHP 8.2).
- Root or sudo access (so you can install software and change settings).
If Apache isn’t installed yet, here’s a quick way to install it:
sudo apt update
sudo apt install apache2
And here’s how I installed PHP 7.4 and 8.2:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.4 php7.4-fpm
sudo apt install php8.2 php8.2-fpm
Now both versions are ready to go. But Apache doesn’t yet know how to use them.
What is PHP-FPM?
Let’s pause for a second to explain PHP-FPM.
FPM stands for FastCGI Process Manager. It helps PHP run faster and safer by handling each website with its own PHP process. Apache talks to PHP-FPM instead of loading PHP directly. This is great when you want to run more than one PHP version.
You’ll need to make sure both PHP versions are installed with FPM. If you’re unsure, check like this:
sudo systemctl status php7.4-fpm
sudo systemctl status php8.2-fpm
Both should say active (running).
Enable Apache Modules
Next, let’s enable some Apache features so it can talk to PHP-FPM.
Run these commands:
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.4-fpm
sudo a2enconf php8.2-fpm
Then restart Apache:
sudo systemctl restart apache2
Now Apache is ready to use both versions.
Set Up Virtual Hosts
Here’s where it all comes together. Apache uses virtual hosts to serve different websites. You can tell each one to use a different PHP version.
Let’s say I have two websites:
- site1.example.com (needs PHP 7.4)
- site2.example.com (needs PHP 8.2)
Each will have its own folder in /var/www/
:
sudo mkdir -p /var/www/site1
sudo mkdir -p /var/www/site2
I’ll put a simple index.php
file in each:
<?php phpinfo(); ?>
This helps me check which PHP version is being used.
Virtual Host for site1 (PHP 7.4)
Create a config file:
sudo nano /etc/apache2/sites-available/site1.conf
Paste this:
<VirtualHost *:80>
ServerName site1.example.com
DocumentRoot /var/www/site1
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/site1_error.log
CustomLog ${APACHE_LOG_DIR}/site1_access.log combined
</VirtualHost>
Virtual Host for site2 (PHP 8.2)
Same steps:
sudo nano /etc/apache2/sites-available/site2.conf
Paste this:
<VirtualHost *:80>
ServerName site2.example.com
DocumentRoot /var/www/site2
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/site2_error.log
CustomLog ${APACHE_LOG_DIR}/site2_access.log combined
</VirtualHost>
Enable both sites:
sudo a2ensite site1.conf
sudo a2ensite site2.conf
sudo systemctl reload apache2
Now Apache knows which PHP version to use for each site.
How to Test It
Visit each site in your browser:
http://site1.example.com
→ should show PHP 7.4http://site2.example.com
→ should show PHP 8.2
If you see the PHP info page, it’s working.
If not, check the Apache error logs. They’re usually in /var/log/apache2/
.
Things That Helped Me
When I was setting this up, these tips made things smoother:
- Use real domain names with your
/etc/hosts
file when testing locally. - Double-check the
.sock
file path. PHP might use a different path on some systems. - Make backups of your config files before editing.
Here’s a quick list of common mistakes:
- Forgetting to install PHP-FPM for each version.
- Using the wrong socket path (like
/run/php/php8.2-fpm.sock
). - Not enabling a site with
a2ensite
.
Benefits of Using Apache with Multiple PHP Versions
- Run legacy apps without downgrading your entire server.
- Test new features in PHP without risking old code.
- Make each site use the version it was built for.
You Might Also Consider
If you’re using other tools, there are similar ways to manage PHP versions:
- Nginx + PHP-FPM also supports multiple versions with upstream blocks.
- Docker can run each site in its own container with any PHP version you want.
- PHP Version Manager (phpbrew) for development on a local machine.
But I still like Apache + FPM. It works well and isn’t hard to manage.
Wrapping Up
Running multiple PHP versions with Apache may sound complex, but once I understood how it works, it became second nature. It’s all about setting the right socket path for each site and making sure Apache knows how to talk to PHP-FPM.
If you’re working with different websites or apps that need different PHP versions, this setup can really help.
Have you tried doing this before? Were you stuck with one PHP version like I was?
Give this a try and see if it solves the problem for you like it did for me. If anything goes wrong, remember to check your config files and error logs — they usually hold the answer.