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

爷,独闯天下 提交于 2019-12-02 05:32:50

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

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.

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

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:

... 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!