在documentroot (cgi-bin文件夹)外运行PHP文件

时间:2021-10-04 07:10:36

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.

我正在和一个同事一起在XAMPP的MAC上设置他们的本地环境,在windows上,我的vhost看起来就像下面的那个。

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

当我访问http://domain.local/cgi-bin/handler的URL时。web服务器正确处理php,但在他的服务器上有500个服务器错误和这个消息…

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.

当我注意到httpd中有另一个别名时,我们尝试将cgibin文件夹的名称更改为其他内容。conf但是这没有影响…在我看来,这个问题是与权限相关的。

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.

我们相信别名是设置好的,可以访问http://domain.local/cgi-bin/filenothere。不存在的php会像预期的那样抛出404。html/。pl文件无法执行。

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

在cgibin文件夹中存在的权限是……

rwxrwxrwx dave staff

rwxrwxrwx戴夫员工

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

和所有的用户和组....

dave staff

戴夫的员工

Vhost

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?

知道是什么原因导致PHP文件不被执行吗?

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.

更新尝试添加一个标题,表示内容是PHP文件的开头,而现在只输出PHP代码。

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

这就好像任何指定的路径都是可以访问的,但是服务器不知道如何执行内容,这是HTML和PHP的。

5 个解决方案

#1


3  

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

我认为你需要一段cgibin。

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 *es 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:

您的服务器可以向您显示脚本源,这意味着服务器至少具有读取/文件/系统/路径/ /cgi-bin和IIRC的权限,这与ScriptAlias b/c ScriptAlias所期望的/文件/系统/路径/到/cgi-bin冲突是出于安全原因。我认为你的解决方案应该是:

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

#2


1  

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:

由于Apache的PHP模块可以直接执行它们,因此很少需要将PHP脚本作为CGIs运行。尝试将其添加到Apache配置中:

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

然后,简单地将PHP脚本放置到文档根中,查看它们是否工作。您将需要删除/cgi-bin/部分的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.

你说你在Mac上设置XAMMP,但是你有一个驱动器字母(E:)前缀你的路径。OS X没有像Windows这样的驱动器,这也可能导致(部分)您的问题。

#3


0  

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://www.sean-barton.co.uk/2009/02/setting-up-a-phpmysql-local-development-environment-on-a-mac-doing-it-properly/
  3. http://docs.joomlabamboo.com/using-joomla/setting-up-a-quick-start-package-on-your-local-server-xampp-pc
  4. http://docs.joomlabamboo.com/using-joomla/setting-up-a-quick-start-package-on-your-local-server-xampp-pc
  5. http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html
  6. http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html
  7. How to create Mac OS X dev environment for legacy PHP app?
  8. 如何为遗留的PHP应用程序创建Mac OS X dev环境?

#4


0  

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.

假设PHP运行在安全模式下,您可能需要“打开”您的cgibin目录,因为用户(PHP)脚本的执行仅限于DocumentRoot和它的子文件夹。

For all I know you could do that in two ways

我知道你可以用两种方法来做。

1. Edit your php.ini

1。编辑你的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的行。如果在该行的开头有一个注释——一个分号——删除它。然后添加cgibin目录。

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

open_basedir = " E:\ \ \ www.domain.co.uk \目录\”

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

如果需要打开多个目录,可以使用分号;作为一个分隔符。在基于Linux的服务器上,使用冒号:

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

open_basedir = " E:\ \ \ www.domain.co.uk \目录\;E:\ \ \ 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

2。编辑你的虚拟主机

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

将以下内容添加到您的虚拟主机,即在DocumentRoot之后:

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

php_admin_value open_basedir“E:\ \ \ www.domain.co.uk \目录\”

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

同样的规则适用于多个目录和Linux和Windows系统之间的区别。

Hope that helps

希望这有助于

#5


0  

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

您知道PHP是作为CGI程序运行还是作为webserver模块运行?如果您可以获得一个phpinfo()页面工作(可能来自网站根目录中的一个普通文件夹),您应该能够找到它。如果您是作为一个webserver模块运行的,那么您应该有一个位于顶部的部分,其中有一个服务器API的标题,该API表示Apache 2.0处理程序(或等效的)。

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.

…它看起来可能是由于PHP作为CGI脚本运行,或者是PHP和另一个CGI处理程序之间的冲突。

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.

在第三个链接页面上的一个海报发现,他们的脚本标题问题的相似结尾通过删除/注释掉他们的.htconfig / vhosts文件中的选项+ExecCGI行来解决。

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

通过上面的链接来查看您的问题是否与您的问题有关,可能值得一读。

#1


3  

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

我认为你需要一段cgibin。

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 *es 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:

您的服务器可以向您显示脚本源,这意味着服务器至少具有读取/文件/系统/路径/ /cgi-bin和IIRC的权限,这与ScriptAlias b/c ScriptAlias所期望的/文件/系统/路径/到/cgi-bin冲突是出于安全原因。我认为你的解决方案应该是:

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

#2


1  

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:

由于Apache的PHP模块可以直接执行它们,因此很少需要将PHP脚本作为CGIs运行。尝试将其添加到Apache配置中:

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

然后,简单地将PHP脚本放置到文档根中,查看它们是否工作。您将需要删除/cgi-bin/部分的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.

你说你在Mac上设置XAMMP,但是你有一个驱动器字母(E:)前缀你的路径。OS X没有像Windows这样的驱动器,这也可能导致(部分)您的问题。

#3


0  

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://www.sean-barton.co.uk/2009/02/setting-up-a-phpmysql-local-development-environment-on-a-mac-doing-it-properly/
  3. http://docs.joomlabamboo.com/using-joomla/setting-up-a-quick-start-package-on-your-local-server-xampp-pc
  4. http://docs.joomlabamboo.com/using-joomla/setting-up-a-quick-start-package-on-your-local-server-xampp-pc
  5. http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html
  6. http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html
  7. How to create Mac OS X dev environment for legacy PHP app?
  8. 如何为遗留的PHP应用程序创建Mac OS X dev环境?

#4


0  

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.

假设PHP运行在安全模式下,您可能需要“打开”您的cgibin目录,因为用户(PHP)脚本的执行仅限于DocumentRoot和它的子文件夹。

For all I know you could do that in two ways

我知道你可以用两种方法来做。

1. Edit your php.ini

1。编辑你的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的行。如果在该行的开头有一个注释——一个分号——删除它。然后添加cgibin目录。

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

open_basedir = " E:\ \ \ www.domain.co.uk \目录\”

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

如果需要打开多个目录,可以使用分号;作为一个分隔符。在基于Linux的服务器上,使用冒号:

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

open_basedir = " E:\ \ \ www.domain.co.uk \目录\;E:\ \ \ 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

2。编辑你的虚拟主机

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

将以下内容添加到您的虚拟主机,即在DocumentRoot之后:

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

php_admin_value open_basedir“E:\ \ \ www.domain.co.uk \目录\”

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

同样的规则适用于多个目录和Linux和Windows系统之间的区别。

Hope that helps

希望这有助于

#5


0  

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

您知道PHP是作为CGI程序运行还是作为webserver模块运行?如果您可以获得一个phpinfo()页面工作(可能来自网站根目录中的一个普通文件夹),您应该能够找到它。如果您是作为一个webserver模块运行的,那么您应该有一个位于顶部的部分,其中有一个服务器API的标题,该API表示Apache 2.0处理程序(或等效的)。

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.

…它看起来可能是由于PHP作为CGI脚本运行,或者是PHP和另一个CGI处理程序之间的冲突。

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.

在第三个链接页面上的一个海报发现,他们的脚本标题问题的相似结尾通过删除/注释掉他们的.htconfig / vhosts文件中的选项+ExecCGI行来解决。

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

通过上面的链接来查看您的问题是否与您的问题有关,可能值得一读。