1

Due to various environmental complexities which are not relevant to the question, I need to launch PHP via a batch file (Windows) and so have set up a ScriptAlias, which looks something like this:

ScriptAlias /php "${path}/php_cgi_wrapper.bat"
Action php-script "/php"

This is fine, things work well for a single PHP version. However, I need to set this up to have a large range of PHP versions available for testing. I can get this working by having duplicate copies of the batch file, whose code is tweaked to point to the correct PHP version:

ScriptAlias /php85 "${path}/php85_cgi_wrapper.bat"
Action php85-script "/php85"
ScriptAlias /php84 "${path}/php84_cgi_wrapper.bat"
Action php84-script "/php84"
ScriptAlias /php83 "${path}/php83_cgi_wrapper.bat"
Action php83-script "/php83"
...etc...

However, that means we've got lots of batch files kicking around and maintenance is fiddly.

I would much rather have a single batch file that serves all PHP versions, and I was hoping I could do something like this, instead:

ScriptAlias /php85 "${path}/php_cgi_wrapper.bat 85"
Action php85-script "/php85"
ScriptAlias /php84 "${path}/php_cgi_wrapper.bat 84"
Action php84-script "/php84"
ScriptAlias /php83 "${path}/php_cgi_wrapper.bat 83"
Action php83-script "/php83"
...etc...

However, this results in an Apache error (The requested URL /php85/path/to/file.php was not found on this server).

Is there a way to achieve what I want; either via command arguments or some other means? Or is the only solution to maintain scripts for each version we want to support? Note that we currently need to support at least 18 versions, but this will grow over time!

The solution mustn't involve parsing the request data sent to STDIN as that would introduce a whole bunch of additional complexity that I'm not sure I could cope with. I'm also not sure that environment variables would work, as they would need to be set differently depending on which handler is called, which I don't think is possible (though correct me if I'm wrong).

Creative ideas welcome, but equally "there is no way to achieve this" would be a valid answer, if that is the case.

Ideally this would work on Apache1 and Apache2 as we would ideally want to test on both, but an Apache2-only solution would be acceptable. (Note that I wasn't allowed to simply tag this as apache, so I've arbitrarily selected a couple of specific versions.)

1
  • 1
    Are you sure this question is about apache-1.3 and apache-2.2 as you've tagged? Apache 1.3 reached EOL in 2010 and 2.2 in 2017. Only 2.4 is still supported. Commented 17 hours ago

1 Answer 1

1

It is not possible to pass command-line arguments to a ScriptAlias target in Apache. ScriptAlias only performs a URL prefixfilesystem path mapping. The right-hand side is treated strictly as an executable path, not a command line.

To achieve version-based dispatch with a single wrapper script, you need to:

  1. Match multiple URL prefixes using a regex-based alias.
  2. Set an environment variable based on the requested prefix.
  3. Use that variable inside a single CGI wrapper.

An example Apache configuration:

ScriptAliasMatch ^/php[0-9]+/ "${path}/php_cgi_wrapper.bat"

SetEnvIf Request_URI "^/php85/" PHP_VERSION=85
SetEnvIf Request_URI "^/php84/" PHP_VERSION=84
SetEnvIf Request_URI "^/php83/" PHP_VERSION=83

Action php-script "/php"
AddHandler php-script .php

With this configuration, the environment variable can be used in the batch script:

@echo off

if "%PHP_VERSION%"=="" set PHP_VERSION=85

set "PHP=C:\php\%PHP_VERSION%\php-cgi.exe"
"%PHP%" -f "%SCRIPT_FILENAME%"

If your directory structure uses dotted version numbers, set the environment variable accordingly (e.g. PHP_VERSION=8.5 for C:\php\8.5\). This is purely a naming convention and can be adjusted to match the filesystem layout.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.