Search code examples
phpxamppvirtualhostcgi-bin

Running PHP file outside of documentroot (cgi-bin folder)


I am working with a colleague to set up their local environment on a MAC in XAMPP, on windows my vhost looks like the one below.

When I access a URL like http://domain.local/cgi-bin/handler.php the web server processes the PHP correctly but on his we get a 500 server error and this message...

The server encountered an internal error and was unable to complete your request.

Error message: 
Premature end of script headers:

We tried changing the name of the cgi-bin folder to something else as I noticed there was another alias in httpd.conf but this had no effect...so it seems to me like the issue is permissions related.

We believe the alias is setup ok as accessing http://domain.local/cgi-bin/filenothere.php which doesn't exist throws a 404 as expected, any .html/.pl files fail to execute.

The permissions that exist on the cgi-bin folder are...

rwxrwxrwx dave staff

and is owned by the user and group....

dave staff

Vhost

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName  www.domain.local
    ServerAlias domain.local
ServerAlias api.domain.local

    # Indexes + Directory Root.
    DirectoryIndex index.php
    DocumentRoot E:/home/www/www.domain.co.uk/htdocs/

    # CGI Directory
    ScriptAlias /cgi-bin/ E:/home/www/www.domain.co.uk/cgi-bin/
    <Location /cgi-bin>
            Options +ExecCGI
    </Location>

    # Logfiles
    ErrorLog  E:/home/www/www.domain.co.uk/logs/error.log
    CustomLog E:/home/www/www.domain.co.uk/logs/access.log combined
</VirtualHost>

Any idea what is causing this PHP file to not be executed?

UPDATE Tried adding a header to say that the content is PHP to the start of the PHP file and this now simply outputs the PHP code.

It's as if any path specified in as an Alias is accessible but the server doesn't know how to execute the content, this is for HTML as well as PHP


Solution

  • I think you need a section for your cgi-bin.

    The fact that your server can show you the script sources means the server has at least read permissions on /file/system/path/to/cgi-bin and IIRC that clashes with ScriptAlias b/c ScriptAlias expects /file/system/path/to/cgi-bin to be unaccessible for security reasons. I think your solution should look something along the lines of:

    <Directory /file/system/path/to/cgi-bin>
        Options ExecCGI
        SetHandler cgi-script
    </Directory