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

标签 apache2 mod-python cgi-bin

以前我在为 python 安装 apache2 时出错,this is the link

我以为我应该在那里继续,但现在我遇到了一个新错误,所以将它作为一个新错误发布,这就是问题所在

我已经放置了一个包含代码的文件 hello.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>'

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

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



我收到这个错误
The requested URL /cgi-bin/hello.py was not found on this server. Apache/2.2.14(Ubuntu)Server at localhost Port 80

这是我在站点可用文件夹的默认页面中找到的代码:
<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>

这是我在错误日志中可以找到的:
[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

这是我可以在 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"

请帮我解决这个问题。

最佳答案

在您的 Apache 配置中,您需要将 Directory 指令更改为:<Directory /var/www/cgi-bin>如果这是您打算托管 mod_python 脚本的地方。

更新:

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

这是我建议您更正的 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>

更新 2:

现在您的 Apache 配置是正确的,并且我们已经验证了您的模块是否正确导入,您仍然会收到 404。这是因为您使用的是 mod_python 发布者 处理程序,它期望使用与您实现的不同的用法。基本上,您已经实现了一个简单的 CGI 脚本,它只打印输出。 mod_python 有一个特定的 API 来执行你的模块。你应该 try this example了解它的工作原理。

要将您的测试模块实现为有效的 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>"""

请注意,您收到 404 错误的原因(即使模块已导入)在 this page 中进行了解释。 :

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

Any of the traversed object's names begin with an underscore ("_"). Use underscores to protect objects that should not be accessible from the 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.

关于apache2 - 使用 apache2 在 cgi-bin 中执行 .py 文件时出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880094/

相关文章:

python - XP 与 C :\python in path won't run files in C:\python

apache - 为什么在debian和ubuntu中默认安装apache的cgi-bin目录在/usr/lib

linux - Apache 如何访问 root 用户创建的 SSL 证书?

php - fileperms() : stat failed, 但文件存在

apache2 - HTTP/1.1 200 好

python - mod_python 没有名为 _apache 的模块

python - Mod_python 不产生输出

Apache/xampp 命令行启动错误 : AH00436: No installed service named "Apache2.4"

c - 如何通过C编写的cgi-bin程序检索表单 "POST"数据