Running Python as CGI in Apache in Windows:
Friday, 17. August 2007, 22:02:59
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.
Prerequisites:
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 "<html><head>"
print "
print "</head><body>"
print "Thanks NoteMe for teaching me this."
print "</body></html>"
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:
http://localhost/test.py
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:
http://localhost:8080/test.py
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 -
By zomg, # 30. August 2007, 05:43:43
- ØØ -
By NoteMe, # 30. August 2007, 07:07:54
By iGod, # 8. September 2007, 14:25:58
- ØØ -
By NoteMe, # 8. September 2007, 15:11:51
But really PHP is always the best for web applications, until we write our own language of course Øyvind!
By iGod, # 8. September 2007, 17:16:00
Thanks for putting together this page. It really helped me get Apache running for local Python CGI scripting.
By anonymous user, # 12. November 2007, 03:50:02
Say I had a program written in Python that needs to interface with a MySQL database on a shared hosting system (netfirms).
They "do not have plans" to add MySQLpython to their service so I can connect to the Db.
Is there:
A) a program to translate my python program elements to cgi; or
B) install a script at my site level that will enable connection to the MySQL database?
Thanks - sincerely - for any help anyone out there can provide.
By anonymous user, # 20. November 2007, 00:00:58
You could also access MySQL directly with Python, but it's not meant to be used like that, so it's hard to find any information at all on how to do that.
- ØØ -
By NoteMe, # 20. November 2007, 00:18:14
I do indeed, but sadly I have no idea how to do what you are suggesting.
It ain't easy being a knucklehead.
Would I just SSH into my account and upload the MySQLpython into my cgi-bin or something?
I appreciate your time and guidance.
Bill
By anonymous user, # 21. November 2007, 14:10:12
- Log on to your server and go to your home directory.
- Then set up your environment. Write:
export LD_LIBRARY_PATH=$HOME/lib/
export PATH="$HOME/bin:$PATH"
source ~/.bash_profile
- Then to download and install. Write:
wget http://downloads.sourceforge.net/mysql-python/MySQL-python-1.2.2.tar.gz
tar -xzvf MySQL-python-1.2.2.tar.gz
cd MySQL-python-1.2.2
python setup.py build
python setup.py install
Hope it works. If not you could always try to ask again, but my best bet is your support at your hosting company.
- ØØ -
By NoteMe, # 23. November 2007, 19:44:34
Thank you for this wonderful article.
It would be nice to remind the readers to replace any "./Script.py"
to just "Script.py" in windows.
Correct me if I'm wrong
By anonymous user, # 15. December 2007, 03:02:30
- ØØ -
By NoteMe, # 15. December 2007, 17:04:19
By fanna4u_sumit, # 21. December 2007, 02:53:10