How to configure an Apache config on Windows with different php Version for different virtual hosts

241 Views Asked by At

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?

1

There are 1 best solutions below

0
Álvaro González On

mod_rewrite is 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 the LoadModule directive 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:

  1. Download as many PHP versions as you need and install them in different directories.

  2. Get the module if you don't have it already. Apache Lounge provides Windows binaries.

  3. Load the module with whatever system-wide defaults you see fit:

    LoadModule fcgid_module C:/path/to/mod_fcgid.so
    FcgidInitialEnv PHPRC "C:/PHP74"
    FcgidWrapper "C:/PHP74/php-cgi.exe" .php
    
  4. For each virtual host where you want a different PHP version, replace mod_php with mod_fcgid:

    <VirtualHost *:443>
        ServerName legacy.example.com
        # ...
    
        Options +ExecCGI
        <FilesMatch \.php$>
            SetHandler fcgid-script
        </FilesMatch>
    </VirtualHost>
    

    If you dropped mod_php entirely you can also do this:

    Options +ExecCGI
    <FilesMatch \.php$>
        AddHandler fcgid-script .php
    </FilesMatch>
    
  5. If you need yet another PHP version, set a different interpreter inside the required virtual host:

    <VirtualHost *:443>
        ServerName latest.example.com
        # ...
    
        Options +ExecCGI
        <FilesMatch \.php$>
            SetHandler fcgid-script
        </FilesMatch>
        FcgidInitialEnv PHPRC "C:/PHP83"
        FcgidWrapper "C:/PHP83/php-cgi.exe" .php
    </VirtualHost>
    
  6. If you're setting PHP directives in .htaccess or other Apache settings files (php_flag, php_value, etc.), you'll need to migrate these into into .user.ini files (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.