Running PHP file outside of documentroot (cgi-bin

2019-02-27 19:26发布

问题:

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

回答1:

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


回答2:

There is (very) rarely a need to run PHP scripts as CGIs given that the PHP module for Apache can execute them directly. Try adding this to your Apache config:

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Afterwards simply place the PHP scripts into the document root for the site and see if they work. You'll want to remove the /cgi-bin/ part of the URL.

You say you're setting XAMMP on a Mac, but you have a drive letter (E:) prefixing your paths. OS X does not have drive letters like Windows, and this may also be causing (part of) your issue.



回答3:

I don't know much about settings used. But I think you should go through following links. Might get some help.

  1. http://www.sean-barton.co.uk/2009/02/setting-up-a-phpmysql-local-development-environment-on-a-mac-doing-it-properly/
  2. http://docs.joomlabamboo.com/using-joomla/setting-up-a-quick-start-package-on-your-local-server-xampp-pc
  3. http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html
  4. How to create Mac OS X dev environment for legacy PHP app?


回答4:

Assuming that PHP is running in Safe Mode you may need to "open" your cgi-bin directory, as the execution of user (PHP) scripts is limited to the DocumentRoot and it's subfolders.

For all I know you could do that in two ways

1. Edit your php.ini

Locate the line containing open_basedir. If there's a comment at the beginning of the line - a semicolon - remove it. Then add your cgi-bin directory.

open_basedir = "E:\home\www\www.domain.co.uk\cgi-bin\"

If you need to open more than one directories you can use semicolon ; as a separator. On Linux based server, use a colon :

open_basedir = "E:\home\www\www.domain.co.uk\cgi-bin\;E:\home\www\www.domain.co.uk\another_dir\"

In cases like mine, where your server is hosted by third party, you'd need the second option (well sort of)

2. Edit your VirtualHost

Add the following to your VirtualHost, i.e. after DocumentRoot:

php_admin_value open_basedir "E:\home\www\www.domain.co.uk\cgi-bin\"

Same rules apply here for multiple directories and difference between Linux and Windows systems as above.

Hope that helps



回答5:

Do you know whether PHP is running as a CGI program or as a webserver module? You should be able to find this out if you can get a phpinfo() page working (maybe from a regular folder inside the website root). If you're running as a webserver module then you should have a section near the top with a heading of Server API which says Apache 2.0 Handler (or equivalent).

From these pages:

  • https://bugs.php.net/bug.php?id=13316
  • http://php.net/manual/en/install.unix.commandline.php
  • http://gallery.menalto.com/node/8955

... it seems that it may be either due to PHP running as a CGI script, or else a conflict between PHP and another CGI handler.

One of the posters on the third linked page found that their similar-sounding end of script headers issue was resolved by removing / commenting out the Options +ExecCGI line in their .htconfig / vhosts file.

Might be worth having a read through the above links to see if your problem is related.