I believe you should be able to configure the server to use different version of PHP for each of the apps.
Note: these instructions from ChatGT mention PHP 8.1, you’ll need >= 8.2.
If you have two Laravel apps on the same server and need them to use different PHP versions, you can achieve this by configuring your web server (Nginx or Apache) and using the correct PHP-FPM version for each app.
1. Check Installed PHP Versions
Run the following command to see which PHP versions are installed on your server:
ls /usr/bin/php*
or
php -v
php7.4 -v
php8.1 -v
If you need to install another PHP version, you can do so using:
sudo apt install php7.4 php7.4-fpm
sudo apt install php8.1 php8.1-fpm
2. Configure Nginx (Recommended for Laravel)
If you’re using Nginx, modify the server blocks for each app.
Example for App 1 (Using PHP 7.4)
server {
listen 80;
server_name app1.example.com;
root /var/www/app1/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
Example for App 2 (Using PHP 8.1)
server {
listen 80;
server_name app2.example.com;
root /var/www/app2/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
After making these changes, restart Nginx:
sudo systemctl restart nginx
3. Configure Apache (If Using)
If you’re using Apache, modify the virtual hosts.
Enable Multiple PHP Versions
First, enable the PHP versions using:
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.4-fpm
sudo a2enconf php8.1-fpm
sudo systemctl restart apache2
Set Different PHP Versions for Each App
Edit the Apache virtual host files for each app.
App 1 (Using PHP 7.4)
<VirtualHost *:80>
ServerName app1.example.com
DocumentRoot /var/www/app1/public
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
<Directory /var/www/app1>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
App 2 (Using PHP 8.1)
<VirtualHost *:80>
ServerName app2.example.com
DocumentRoot /var/www/app2/public
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
</FilesMatch>
<Directory /var/www/app2>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
After editing the configuration, restart Apache:
sudo systemctl restart apache2
4. Set CLI PHP Version Per Project
By default, the command-line PHP version may not match the web server’s PHP version. You can specify which version to use per project.
Inside App 1 (which uses PHP 7.4):
alias php="php7.4"
Inside App 2 (which uses PHP 8.1):
alias php="php8.1"
Alternatively, use update-alternatives
:
sudo update-alternatives --set php /usr/bin/php7.4
or for PHP 8.1:
sudo update-alternatives --set php /usr/bin/php8.1
For project-specific CLI PHP version, you can use Laravel’s php
binary path in commands:
/usr/bin/php7.4 artisan migrate
Conclusion
- Use different PHP-FPM versions in your Nginx or Apache configurations.
- Restart the web server after making changes.
- Set the correct PHP version for CLI operations if needed.
Would you like help setting up Supervisor queues or cron jobs for each app with the correct PHP version? 