使用apache2在cgi-bin中执行.py文件时出错。

时间:2021-08-01 07:09:01

previously i had error in installing apache2 for python, this is the link

以前我在为python安装apache2时犯了错误,这是链接。

I thought i should continue there but right now i'm getting a new error so posting it as a new one this is what the problem is

我想我应该继续,但是现在我有了一个新的错误,把它作为一个新的错误,这就是问题所在。

I have placed a file hello.py having code

我已经放了一个文件。py有代码

#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

in a folder /var/www/cgi-bin when I run this on apache2 using

在一个文件夹/var/www/cgi-bin中,当我使用apache2运行这个文件时。

http://localhost/cgi-bin/hello.py

http://localhost/cgi-bin/hello.py

i'm getting this error

我得到这个错误

The requested URL /cgi-bin/hello.py was not found on this server. Apache/2.2.14(Ubuntu)Server at localhost Port 80

This is the code that i found in the default page in sites-available folder:

这是我在sites-available文件夹的默认页面中找到的代码:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

This is what i could find in the error log:

这是我在错误日志中可以找到的:

[Fri May 06 13:41:22 2011] [notice] mod_python: using mutex_directory /tmp 
[Fri May 06 13:41:22 2011] [notice] Apache/2.2.14 (Ubuntu) mod_python/3.3.1 Python/2.6.5 mod_wsgi/2.8 configured -- resuming normal operations
[Fri May 06 14:23:03 2011] [error] [client 127.0.0.1] File does not exist: /var/www/favicon.ico
[Fri May 06 14:23:03 2011] [debug] mod_deflate.c(615): [client 127.0.0.1] Zlib: Compressed 284 to 218 : URL /favicon.ico
[Fri May 06 14:23:11 2011] [notice] mod_python (pid=2038, interpreter='127.0.1.1'): Importing module '/var/www/cgi-bin/hello.py'
[Fri May 06 14:23:11 2011] [debug] mod_deflate.c(615): [client 127.0.0.1] Zlib: Compressed 289 to 222 : URL /cgi-bin/hello.py
[Fri May 06 14:23:11 2011] [error] /usr/lib/python2.6/dist-packages/mod_python/importer.py:32: DeprecationWarning: the md5 module is deprecated; use hashlib instead
[Fri May 06 14:23:11 2011] [error]   import md5
[Fri May 06 14:23:25 2011] [debug] mod_deflate.c(615): [client 127.0.0.1] Zlib: Compressed 289 to 222 : URL /cgi-bin/hello.py

This is what i could find in access.log:

这是我在access.log中可以找到的。

172.16.0.73 - - [06/May/2011:02:29:02 +0530] "GET / HTTP/1.1" 200 492 "-" "EZI_WIN_HTTP_AGENT"
172.16.0.73 - - [06/May/2011:02:46:47 +0530] "GET / HTTP/1.1" 200 492 "-" "EZI_WIN_HTTP_AGENT"
127.0.0.1 - - [06/May/2011:14:23:03 +0530] "GET /favicon.ico HTTP/1.1" 404 501 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3"
127.0.0.1 - - [06/May/2011:14:23:10 +0530] "GET /cgi-bin/hello.py HTTP/1.1" 404 504 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3"
127.0.0.1 - - [06/May/2011:14:23:25 +0530] "GET /cgi-bin/hello.py HTTP/1.1" 404 504 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3"
127.0.0.1 - - [06/May/2011:15:10:33 +0530] "GET /cgi-bin/hello.py HTTP/1.1" 404 505 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3"
125.224.195.218 - - [06/May/2011:15:15:10 +0530] "CONNECT 203.188.201.253:25 HTTP/1.1" 405 539 "-" "-"
172.16.0.25 - - [06/May/2011:15:45:53 +0530] "HEAD / HTTP/1.0" 200 277 "-" "-"
127.0.0.1 - - [06/May/2011:21:36:32 +0530] "GET /cgi-bin/hello.py HTTP/1.1" 404 505 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3"
127.0.0.1 - - [06/May/2011:21:36:35 +0530] "GET /favicon.ico HTTP/1.1" 404 500 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3"

Please help me through this.

请帮我度过这个难关。

1 个解决方案

#1


7  

In your Apache configuration, you need to change the Directory directive to: <Directory /var/www/cgi-bin> if this is where you intend to host your mod_python scripts.

在Apache配置中,需要将目录指令更改为: ,如果这是您打算托管mod_python脚本的地方。


UPDATE:

更新:

You're placing your python script in /var/www/cgi-bin but /cgi-bin/ is defined to point to /usr/lib/cgi-bin. You need to standardize on one location for /cgi-bin/.

您将python脚本放置在/var/www/cgi-bin中,但是/cgi-bin/被定义为指向/ usr/lib/cgi-bin。您需要在一个位置上标准化/cgi-bin/。

Here's what I would propose as your corrected Apache configuration. Note that I didn't remove any lines, I only commented out the lines that are not needed or have been replaced.

以下是我建议的修改后的Apache配置。注意,我没有删除任何行,我只注释掉不需要的行或者已经被替换的行。

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    #<Directory /var/www>
    <Directory /var/www/cgi-bin>
        Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
    </Directory>

    #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    #<Directory "/usr/lib/cgi-bin">
    #    AllowOverride None
    #    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    #    Order allow,deny
    #    Allow from all
    #</Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

UPDATE 2:

更新2:

Now that your Apache configuration is correct, and we've verified that your module is importing correctly, you're still getting a 404. This is because you are using the mod_python publisher handler, which expects a different usage than what you have implemented. Basically, you have implemented a simple CGI script that just prints output. mod_python has a specific API for executing your module. You should try this example to get an understanding of how it works.

既然您的Apache配置是正确的,并且我们已经验证了您的模块是正确导入的,那么您仍然可以得到404。这是因为您使用的是mod_python发布者处理程序,它期望的使用与您所实现的不同。基本上,您已经实现了一个简单的CGI脚本,它只打印输出。mod_python有一个用于执行模块的特定API。您应该尝试这个示例来了解它是如何工作的。

To implement your test module as a valid mod_python publisher, you could change hello.py to this:

要实现您的测试模块作为一个有效的mod_python发布者,您可以更改hello。py:

def index(req):
    return """<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body>
<h2>Hello Word! This is my first CGI program</h2>
</body>
</html>"""

Note that the reason you were getting a 404 error (even though the module was imported) is explained on this page:

请注意,您获得404错误的原因(尽管模块是导入的)在此页面上有说明:

The traversal will stop and HTTP_NOT_FOUND will be returned to the client if:

遍历将停止,HTTP_NOT_FOUND将返回给客户端:

Any of the traversed object's names begin with an underscore ("_"). Use underscores to protect objects that should not be accessible from the web.

任何遍历对象的名称都以下划线(“_”)开头。使用下划线来保护不应该从web访问的对象。

A module is encountered. Published objects cannot be modules for security reasons.

遇到一个模块。出于安全原因,发布的对象不能是模块。

If an object in the path could not be found, HTTP_NOT_FOUND is returned to the client.

如果无法找到路径中的对象,则将HTTP_NOT_FOUND返回给客户端。

#1


7  

In your Apache configuration, you need to change the Directory directive to: <Directory /var/www/cgi-bin> if this is where you intend to host your mod_python scripts.

在Apache配置中,需要将目录指令更改为: ,如果这是您打算托管mod_python脚本的地方。


UPDATE:

更新:

You're placing your python script in /var/www/cgi-bin but /cgi-bin/ is defined to point to /usr/lib/cgi-bin. You need to standardize on one location for /cgi-bin/.

您将python脚本放置在/var/www/cgi-bin中,但是/cgi-bin/被定义为指向/ usr/lib/cgi-bin。您需要在一个位置上标准化/cgi-bin/。

Here's what I would propose as your corrected Apache configuration. Note that I didn't remove any lines, I only commented out the lines that are not needed or have been replaced.

以下是我建议的修改后的Apache配置。注意,我没有删除任何行,我只注释掉不需要的行或者已经被替换的行。

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    #<Directory /var/www>
    <Directory /var/www/cgi-bin>
        Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
    </Directory>

    #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    #<Directory "/usr/lib/cgi-bin">
    #    AllowOverride None
    #    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    #    Order allow,deny
    #    Allow from all
    #</Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

UPDATE 2:

更新2:

Now that your Apache configuration is correct, and we've verified that your module is importing correctly, you're still getting a 404. This is because you are using the mod_python publisher handler, which expects a different usage than what you have implemented. Basically, you have implemented a simple CGI script that just prints output. mod_python has a specific API for executing your module. You should try this example to get an understanding of how it works.

既然您的Apache配置是正确的,并且我们已经验证了您的模块是正确导入的,那么您仍然可以得到404。这是因为您使用的是mod_python发布者处理程序,它期望的使用与您所实现的不同。基本上,您已经实现了一个简单的CGI脚本,它只打印输出。mod_python有一个用于执行模块的特定API。您应该尝试这个示例来了解它是如何工作的。

To implement your test module as a valid mod_python publisher, you could change hello.py to this:

要实现您的测试模块作为一个有效的mod_python发布者,您可以更改hello。py:

def index(req):
    return """<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body>
<h2>Hello Word! This is my first CGI program</h2>
</body>
</html>"""

Note that the reason you were getting a 404 error (even though the module was imported) is explained on this page:

请注意,您获得404错误的原因(尽管模块是导入的)在此页面上有说明:

The traversal will stop and HTTP_NOT_FOUND will be returned to the client if:

遍历将停止,HTTP_NOT_FOUND将返回给客户端:

Any of the traversed object's names begin with an underscore ("_"). Use underscores to protect objects that should not be accessible from the web.

任何遍历对象的名称都以下划线(“_”)开头。使用下划线来保护不应该从web访问的对象。

A module is encountered. Published objects cannot be modules for security reasons.

遇到一个模块。出于安全原因,发布的对象不能是模块。

If an object in the path could not be found, HTTP_NOT_FOUND is returned to the client.

如果无法找到路径中的对象,则将HTTP_NOT_FOUND返回给客户端。