404 error while executing .py file in cgi-bin using apache2

Bhuvan raj picture Bhuvan raj · May 4, 2011 · Viewed 15.8k times · Source

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

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

#!/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

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:

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

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.

Answer

AJ. picture AJ. · May 4, 2011

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.


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

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.

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

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.

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

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:

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.