2011-05-04 40 views
3
執行.py文件中的cgi-bin

以前我有錯誤在蟒蛇安裝的Apache2,this is the link404錯誤,而使用的Apache2

我想我應該繼續存在,但現在我得到一個新的錯誤,以便張貼作爲一個新的,這是什麼問題

我已經放置在具有文件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時,我使用

0的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 [email protected] 

    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" 

請幫我通過這個。

回答

7

在你的Apache配置,您需要更改目錄指令:<Directory /var/www/cgi-bin>,如果這是在您打算承載您的mod_python腳本。


UPDATE:

你把你的Python腳本中/var/www/cgi-bin/cgi-bin/被定義爲指向/usr/lib/cgi-bin。您需要在一個位置爲/cgi-bin/標準化

這裏是我建議作爲糾正Apache配置。請注意,我沒有刪除任何行,只是將不需要或已被替換的行註釋掉了。

<VirtualHost *:80> 
    ServerAdmin [email protected] 

    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的出版商,你可以改變打招呼。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解釋說:

遍歷將停止, HTTP_NOT_FOUND將被退回到 客戶端如果:

任何遍歷對象的名稱 都以下劃線(「_」)開頭。使用 下劃線來保護 不應從網絡訪問的對象。

遇到了一個模塊。發佈的 對象不能成爲安全模塊 的原因。

如果找不到路徑中的對象 ,則HTTP_NOT_FOUND返回到 客戶端。

+0

對不起,你能解釋一下如何編輯配置的過程嗎? – 2011-05-04 20:07:01

+0

當然,你能否將你的默認文件的內容從啓用網站的網站粘貼到原始問題中(上圖)? – 2011-05-04 20:29:27

+0

我在問題中粘貼了默認頁面的代碼。 :) – 2011-05-05 06:30:14