Running Python as CGI in Apache in Windows:
Friday, August 17, 2007 10:02:59 PM
Why CGI? And why, really why Windows?!?
You have basically two different choices when it comes to run Python code to create dynamic pages. You can run it as a mod, which is by far the preferred choice both when it comes to speed and ease of handling. If you have the possibility to install mod_python on your server, or if it is already installed, I suggest you stop reading right away, and do just that.
The reason I write this is not because it is the preferred way, but because at most web hosts you don't have the possibility to install mod_python, and very few web hosts have it pre installed for you. But your problems won't stop there. Most tutorials will only show you how to use mod_python, and leave you to figure out how to do the CGI part. So that is what I will try to do something about now. In Linux, setting up Apache to run Python as CGI is just as difficult as to just find out where Python is installed and you are ready to go, but for Windows users, you can google your self to death, or keep reading.
This is no tutorial for setting up your whole development server. I assume you know how to set up your Apache server on your own in Windows. After all, it is more or less just to press the install button. But if you don't know, and you are willing to learn, here is a link to a tutorial a good friend of mine wrote a while ago. It should get you going up and running in no time, and add some basic security for you as well for your development machine.
The same goes for Python. Just install it. There is no more fuzz about it. After it install, and you have tested that it will work, you are ready to move on to start using it for your server.
Running Python on Apache as CGI:
In Linux you are probably used to the shebang line which tells the script where Python is to be found, so it can run the script for you. There is an equal way of doing it in Windows, but there is a smarter way.
Well, if it is smarter is still discussed, and probably will be forever, but the good part about it, is that it makes it easy to upload the script to a Linux server, without any changes and it will still run. If you leave the Linux shebang line there, you can make Apache ignore it on your Windows server, and it will still be read when you use the script on your Linux server.
The way you do it, is to edit the http.conf file of yours. Go to:
Start Menu -> Programs -> Apache HTTP Server -> Configure Apache Server -> Edit the httpd.conf Configuration File
And locate the line that looks like:
Remove the comment (#) , and there you go. Now windows will use the registry to find out where Python is installed, and you have all read read past the part where I asked you to check if you could run Python script, so that's really it.
But you are not done just yet Mr. Even though Apache now is aware of Python, there is still a missing link between your Python scripts and Apache. You need to tell Apache what file types you are using for Python. The most usual file type is .py, but anything goes. But using .py is a good start. So scroll down on to the bottom of your httpd.conf file, and add the line:
This tells Apache that it should handle all .py files as CGI scripts. And that's what we are after. You can add more space separated file types after the .py, without me seeing the point in doing that here.
So this is it?
Yeah, that's basically it. But there is one more thing we should go through. Safety is a big concern when you are setting up a server. And Apache is usually set up to not allow CGI scripts to run outside the cgi_bin files. If this is ok for you, then you are done. If you are like me, you want a bit more freedom. You can easily give access to run Python scripts in all folders on your server. The easiest way, is again to edit your httpd.conf file. Find a line that looks something like:
There might be a few other words added to it, or less, but it should be between the <Directory> tags pointing to your root folder. At the end add ExecCGI, which basically permits Apache to execute CGI scripts, and if you did write it in your root <Directory>, it will automatically give Apache access to run it in all sub directories as well. This is both great freedom for you, but also for hackers if they find holes in your scripts they are able to take advantage of. So be warned. For this approach, FastCGI might be a safer bet, but again you have the problem with it not being installed on too many servers around the world. But maybe I will find the time to write up a little something about that later on.
Your first dynamic page with Python on Windows:
So you are ready to test it all out. Writing a python cgi script is a little bit different than a normal python script, because this time what you print out has to be a web page. Usually it will be in HTML or XHTML format, but you also have to remember to print out the correct content-type as well as a minimum. So a minimal Python CGI script might look like something like this:
print "Thanks NoteMe for teaching me this."
Even though I left out the DocType with more on purpose, it should give you an idea about what it should look like. Try now to save this to a file called test.py in the root of your Apache server on your machine. On my machine that would be:
C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\
Then point your browser to:
if you chose port 80 when you installed Apache. If you chose something else like 8080, then the port number has to be added after the domain name like this:
If you still have problems, don't hesitate to ask. I usually always answer no matter if I have an answer or not when I have time to do so.
- Øyvind Østlund -