Integrating Python and Apache


Today I'm going to write on how to integrate Python with apache web server. All you probably know that Python is also a server side scripting language. So, like PHP and other server side scripting languages, you can use Python in the same way. This is not tough, just a matter of some minutes.

Okay, before you start the tutorial, you must (most probably already you have) have the following installed in your computer/server.

1. Apache web server (Only apache or xampp, wamp etc anything containing apache will do). I'll use xampp as reference but others should behave the same
2. Python (I'm using 2.7)

So, if you don't have these two installed, download and install them first and then continue

As you are here, you have already installed apache/xampp/wamp and Python. Follow these steps

1. Go to your apache folder. In my case, as I'm using xampp it's in location C:\xampp\apache

2. Go to the conf folder and open the httpd.conf using a text editor. I prefer notepad++ as it'll show the lines in a convenient way.

3. Find the line containing AddHandler cgi-script

4. If there is some other types at the end of this line just append a space and .py at the end.
For example in my case the line previously contained
AddHandler cgi-script .cgi .pl .asp
So I added .py at the end of the line so now it's like
AddHandler cgi-script .cgi .pl .asp .py

5. Now go to the cgi-bin folder of your server. In my case, it's in C:\xampp\cgi-bin

6. Create a new python file and write some code there. You can simply copy and paste the following code for testing in a file named test.py

you can also find the source code here http://codepad.org/WB2BZ6vJ

#!C:\Python27\python.exe -u
#!/usr/bin/env python
import cgi
import cgitb; cgitb.enable() # for troubleshooting
print "Content-type: text/html"
print """
<html>
<head>
<title>Sample CGI Script</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
"""

7. Now restart your apache and go to this url http://localhost/cgi-bin/test.py
You should see the web page saying

Hello world

8. Let's look at the code of the test.py file. The first two lines are important

Here the first line is #!C:\Python27\python.exe -u

can you guess what does it mean? This is the command used to run a Python script in my computer. In other words, it is the installation path of python.exe in my computer. In your case, you have to replace the C:\Python27\python.exe with your installation path of python.exe if it's not the same.

The second line #!/usr/bin/env python is also the same thing but for linux OS. As I'm working in windows, I don't need this. So linux users should replace the 
#!/usr/bin/env python line with their own Python installation path or command used to run python

Hope it helped you. If you face any problems, try to google first because I'm very new to Python and just started exploring it. So, in most of the cases, I'll fail to solve your problem :(

One important point: though CGI scripting is fun, it's not a elegant/efficient/beautiful way to build websites in python. Lots of nice frameworks(like django) are available for that purpose.

Comments

  1. Of course we could use CGI to build websites in Python but it is not a very elegant way! :)

    ReplyDelete
    Replies
    1. Thanks a lot for your comment. It's great pleasure that first comment from a tech guru.

      I'm very new in python. When I was able to run python code inside xampp using CGI, it actually increased my interest about python a lot. So I just wrote down my experience and shared with all. At the time of writing I had no idea on django.

      Thanks a lot for this information. I'm attaching it with the post so that no one starts building a website using CGI after reading this blog without further exploring.

      Delete

Post a Comment

Popular posts from this blog

Run tasks in background in Spring

Conditional field inclusion in Jackson and Spring Boot

How to configure Wildfly 10 to use MySQL