I am testing different syntax in the httpd.config of Apache 2.4 / Windows 10 to have different php Versions for different virtual hosts. Domain1 should have PHP 8.1 and Domain2 should run with PHP 7.4. This is one of my tests: Sometimes with the line
LoadModule php7_module "C:/amp/php74/php7apache2_4.dll"
sometimes without it. I get different errors. This version works for Domain 1 but not for Domain 2 which needs 7.4.
#LoadModule php7_module "C:/amp/php74/php7apache2_4.dll"
LoadModule php_module "C:/amp/php8/php8apache2_4.dll"
<IfModule php_module>
AddType application/x-httpd-php .php
DirectoryIndex index.php index.html
PHPIniDir "C:/amp/php8"
</IfModule>
<VirtualHost *:80>
ServerName localhost
DocumentRoot "D:/wwwroot"
<Directory "D:/wwwroot">
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName domain1.localhost
DocumentRoot "D:/wwwroot/domain1/web"
ErrorLog "logs/domain1-error.log"
<Directory "D:/wwwroot/domain1/web">
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.localhost
DocumentRoot "D:/wwwroot/domain2"
ErrorLog "logs/domain2-error.log"
<Directory "D:/wwwroot/domain2">
Require all granted
AllowOverride All
</Directory>
<FilesMatch \.php$>
SetHandler application/x-httpd-php
Action application/x-httpd-php "C:/amp/php74/php-cgi.exe"
</FilesMatch>
</VirtualHost>
What would be the right syntax?
mod_rewriteis bound to one specific PHP version and only one instance can be loaded, so there's no way to have different PHP versions at the same time. You can switch between versions if you change theLoadModuledirective and restart Apache (there're third-party bundles that script this) or you can install another copy of Apache listening to a different IP+port combination.Another alternative it to install a different SAPI module side to side (or replacing)
mod_php, such as mod_fcgid. For that:Download as many PHP versions as you need and install them in different directories.
Get the module if you don't have it already. Apache Lounge provides Windows binaries.
Load the module with whatever system-wide defaults you see fit:
For each virtual host where you want a different PHP version, replace
mod_phpwithmod_fcgid:If you dropped
mod_phpentirely you can also do this:If you need yet another PHP version, set a different interpreter inside the required virtual host:
If you're setting PHP directives in
.htaccessor other Apache settings files (php_flag,php_value, etc.), you'll need to migrate these into into.user.inifiles (with the appropriate syntax).The benefit over other solutions is that you don't need to switch, all projects will use the PHP version they need, simultaneously.
Note: this is written with Windows in mind. On Linux, you'll probably prefer php-fpm.