4

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.

Community
  • 1
  • 1
Bhuvan raj
  • 413
  • 3
  • 8
  • 17

1 Answers1

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.


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.

AJ.
  • 27,586
  • 18
  • 84
  • 94
  • Sorry can u explain me the procedure how to edit the configuration little more in brief? – Bhuvan raj May 04 '11 at 20:07
  • Sure, can you please paste the contents of your `default` file from sites-enabled into your original question (above)? – AJ. May 04 '11 at 20:29
  • @AJ i copy pasted the above edited code on default in sites-enabled folder but still its giving the same error :( :( – Bhuvan raj May 05 '11 at 19:45
  • @Bhuvan raj - sorry for asking, but you restarted Apache after making the change, correct? – AJ. May 05 '11 at 19:47
  • yeah i did, i actually copy pasted first then started the server! Anyways now i restarted and checked it. It still giving the same 404 error :( – Bhuvan raj May 05 '11 at 19:55
  • ok, change LogLevel to debug, and restart again. Then request the page again. Afterwards, retrieve any messages appearing in the error log related to the request and paste into your original question above. I'll have a look at the error log entries. – AJ. May 05 '11 at 20:01
  • sorry for trouble can u tell me where is LogLevel how to set to debug mode? – Bhuvan raj May 05 '11 at 20:11
  • Sure...LogLevel is a directive in your Apache configuration file (above). It's right below ErrorLog. So, change "LogLevel warn" to "LogLevel debug" – AJ. May 05 '11 at 20:50
  • yeah i edited it to LogLevel debug, then i restarted the server and requested for the page again! When ichecked the error console in the browser it's empty, i tried many a times but still its empty.. – Bhuvan raj May 05 '11 at 20:59
  • Not the error console, the error log for Apache :) This is defined by: ErrorLog /var/log/apache2/error.log – AJ. May 06 '11 at 00:02
  • @Bhuvan raj - it appears that mod_python did find the file...see this entry: [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' so you shouldn't be getting a 404. Go ahead and change LogLevel back to warn to cut down on the verbosity. Check your access log (/var/log/apache2/access.log) and paste the last few entries for /cgi-bin/hello.py into your question. I want to see what Apache thinks its returning to the client... – AJ. May 06 '11 at 13:14
  • @AJ i have pasted entries of access log. – Bhuvan raj May 06 '11 at 16:17
  • @Bhuvan raj - updated my answer again. i think you will figure it out soon! – AJ. May 06 '11 at 16:37
  • @AJ it worked its so kind of u to spend so much of time and to your patience !!! :) Thanks a lot :) if u could show me some good link where i could learn coding cgi programs it would be of great help. :) – Bhuvan raj May 06 '11 at 16:46
  • @Bhuvan raj - no problem, glad to help! As far as learning CGI, here is a good link to start with: http://webpython.codepoint.net/cgi_tutorial Note that you can have mod_python and CGI scripts co-exist on the same server. However, you **will** need to put them in different directories due to the differences in the required Apache configurations. – AJ. May 06 '11 at 16:50
  • :) :) Is there anyway that i can make better configuration and reduce this complexity?? I hope my question is proper.. :-/ – Bhuvan raj May 06 '11 at 16:55
  • @Bhuvan raj - are you saying you want to use CGI **instead of** mod_python? If that's the case, you can revert all of the config changes that we made. The initial Apache configuration gives you CGI enabled in /usr/lib/cgi-bin. If you want, you can change that easily to /var/www/cgi-bin. – AJ. May 06 '11 at 17:07
  • @AJ i want your opinion which would be better for cgi scripting, basic cgi or mod_python? I actually have a project that i have coded using python. I want to host it on to the web so working on web interface part! – Bhuvan raj May 06 '11 at 17:11
  • I think you'll find that using a framework is easier than writing CGI scripts from scratch. mod_python isn't really a **framework**, it's just a Apache request handler module that ties in with other frameworks (or allows you to write your own framework). A good lightweight framework that just provides templating support is called Cheetah and can be found here: http://www.cheetahtemplate.org/docs/users_guide_html/users_guide.html#SECTION000470000000000000000 For more complicated applications, I would recommend Pyramid: http://docs.pylonsproject.org/docs/pyramid_quick_tutorial.html – AJ. May 06 '11 at 17:18
  • Thanks a ton :) :) I will go about it and will be back to you in doubts pls help me out :) :) – Bhuvan raj May 06 '11 at 17:38
  • sure thing...and the best part about this site is if I'm not here, you can create a new question and someone else can help too! but i'll definitely be watching...good luck! – AJ. May 06 '11 at 17:40
  • @Richard_Grant there were multiple issues in a single thread here. Which one are you having? It might be better to post a new Q since this thread is now almost 5 years old. – AJ. Jan 11 '16 at 20:33