[Tutor] Simple CGI script and Apache configuration
mwalsh at mwalsh.org
Tue Feb 24 06:33:35 CET 2009
> Hello all,
> 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
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
You do *not* need mod python to run python cgi scripts.
> In /var/www/cgi-bin, I have hello.py <http://hello.py>:
> import cgitb
> print "Content-type: text/html"
> 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):
# or, Options +ExecCGI to merge
# with options from parent dir(s)
Allow from all
> 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.
More information about the Tutor