[Tutor] cgi not executed when accessed through browser

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 20 Jul 2002 17:21:03 -0700 (PDT)


On Fri, 19 Jul 2002, runsun wrote:

> I have a simple python-based cgi :
>
[script contents cut]
>
> It was executed as expected when called from command line in my unix
> account. But when it was called through a browser, it didn't print out
> the listdir that I expected, but displayed the entire code content as
> text on my browser.

Hi runsun,


If this happens, there are a few things you'll want to check.  One of them
is to see if the CGI program is "executable".  Another is to check that
the web server knows that '.py' and '.cgi' files are meant to be CGI
programs.

Since you're on a Unix server, your web server is probably Apache.  Check
with your system administrator to see if '.py' and '.cgi' files are
treated as CGI scripts.  Hopefully, they should know enough to fix this.


... but if you ARE the system administrator, you'll want to look at:

    http://httpd.apache.org/docs/howto/cgi.html


In particular, you may need to modify 'httpd.conf', and on the line that
mentions which files are cgi-able:

###
    AddHandler cgi-script .cgi
###



you'll want to add additional file extensions:

###
    AddHandler cgi-script .cgi .py .pl
###

You'll also want to all ExecCGI in the directory where the CGI files are
living; if your Apache server is configured properly, it should exclude
CGIs from running outside directories that you specify, for security
reasons.


This is, admittedly, a little involved, so we recommend a read through
that Apache CGI FAQ.  Thankfully, this is a one-time thing; once you have
your web server configured properly, it should be smooth sailing.


If you have more questions or run into problems, please feel free to ask
us on Tutor.  Best of wishes to you!