[Tutor] Simple CGI script and Apache configuration

wormwood_3 wormwood_3 at yahoo.com
Tue Feb 24 07:14:24 CET 2009


Thanks for all the suggestions! I tried to go through them, and will relate what results I encountered. I changed my Apache config to:

         <Directory /var/www/samuelhuckins.com/cgi-bin/>
                 AllowOverride None
                 Options ExecCGI
                 Order allow,deny
                 Allow from all
         </Directory>

I in fact did not have the cgi module enabled, so I did that. Then I ran "sudo /etc/init.d/apache2 reload", and hit http://samuelhuckins.com/cgi-bin/hello.py, which contains simply:

#!/usr/bin/python
print "Content-type: text/html"
print
print "<html>"
print "<center>Hello!</center>"
print "</html>"

I get prompted to download the file, but it does not execute or appear in plain text. The logs just show the request being made. What is the missing element to get this script to execute?

Thanks,
Sam



________________________________
From: Martin Walsh <mwalsh at mwalsh.org>
To: Python Tutorlist <tutor at python.org>
Sent: Tuesday, February 24, 2009 12:33:35 AM
Subject: Re: [Tutor] Simple CGI script and Apache configuration

wormwood_3 wrote:
> Hello all,

Hi Sam,

> I'll try to give as much detail as I can, but this is a somewhat vague
> problem. I have a very simple script that I would like to implement as a
> CGI script, just so I can hit a URL and get some output. However, after
> following a number of tutorials, I am still seeing some very odd
> results. I am almost sure it's in my Apache configuration, but I figured
> a few people on this list would likely know what the minimal related
> Apache config should be. (The Apache docs are pretty daunting...)
> 
> Local version wise, I am on Ubuntu 8.10, with Apache 2.2.9 and Python
> 2.5.2. I have libapache2-mod-python installed. Apache config is out of
> the box, along with:
> 
> ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

You need more than this to make apache cgi work, I think.

Firstly, mod_cgi should be loaded -- look for a symlink named cgi.load
or some such, in /etc/apache2/mods-enabled. Run 'a2enmod cgi' if you
don't have one. Secondly, ExecCGI is usually enabled using the Options
directive within a Directory definition -- but, less commonly, you might
see something like 'AddHandler cgi-program .py' in an apache or site
config.

Of course, the script needs to be executable by the apache user (which
would be 'www-data' on ubuntu, IIRC), and contain an appropriate shebang
(#!) on the first line -- but it sounds like you have that covered.

Both 'Options ExecCGI' and 'Addhandler cgi-program .py' are allowed in
.htaccess also, given an appropriate AllowOverride directive for the
path in question. Something to look for on the working system, if all
else fails.

You do *not* need mod python to run python cgi scripts.

> In /var/www/cgi-bin, I have hello.py <http://hello.py>:
> 
> #!/usr/bin/python
> import cgitb
> cgitb.enable()
> 
> print "Content-type: text/html"
> print
> print "<html>"
> print "<center>Hello!</center>"
> print "</html>"
> 
> Reload, hit http://localhost/cgi-bin/hello.py in a browser, I get
> centered text just fine. Now I want to do this same process on my remote
> webserver. On there, I am on Ubuntu 7.10, with Apache 2.2.4 and Python
> 2.5.1. I add:
> 
> ScriptAlias /python/ "/var/www/samuelhuckins.com/python"

You can try appending something like this (untested):

<Directory "/var/www/samuelhuckins.com/python/">
  AllowOverride None
  Options ExecCGI
  # or, Options +ExecCGI to merge
  # with options from parent dir(s)
  Order allow,deny
  Allow from all
</Directory>

> 
> Reload, hit http://samuelhuckins.com/python/hello.py, and I get a 404?
> The perms and ownership on the file is the same as in other directories.
> Do I need to add some sort of handler, with mod_python.publisher? I
> think I am just missing one of the basics of this whole process.

Hmmm, interesting. It's unlikely that any of my advice will help you
with a 404. With an incomplete apache cgi config, the response I'd
expect would be either a 403 (Forbidden), or the script itself in plain
text. Do the logs provide any additional information?

Re-check your spelling. A 404 with vanilla apache config might just
indicate a typo. When you say 'Reload', I assume you mean the apache
daemon (ie. /etc/init.d/apache2 reload or apache2ctl reload)?

Again, you do *not* need mod python to run python cgi scripts.

HTH,
Marty
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090223/491789b4/attachment-0001.htm>


More information about the Tutor mailing list