Sunday, April 3, 2016

How to Run Python on XAMPP web server

Today, am going to talk abut "Running Python Scripts on Windows with Apache and Xampp web server".

One of the easiest ways to get Apache web server on a local windows PC is through an easy to use tool called XAMPP. Other tool that provide Apache web server on PC are WAMP or AMPPS.

While PHP and Perl are automatically installed with the XAMPP server as at the time of writing, XAMPP installation doesn't come with Python by default, hence we have to we have to add python support manually. On the other hand, the AMPPS web server includes Python if you decided to use it.

XAMPP stands for Cross-Platform (X), Apache (A), MySQL (M), PHP (P) and Perl (P). It is a simple, lightweight Apache distribution that makes it extremely easy for developers to create a local web server for testing purposes.

Now download and install latest version of XAMPP if don't have it.

Next download and install Python, for this purpose am going to use Python2.7.10. Make sure you select "add python.exe to Path" to add your python set to the system PATH.

I assume you have installed both xampp and python on the C Drive:
c:/python27 or c:/python35

In order to deliver dynamic contents on the web using python with the XAMPP server, we need a mechanism to do this? This mechanism could be either of WSGI or CGI or FastCGI or sCGI etc.

To run python on a web server like XAMPP, we can use module implementation of Web Server Gateway Interface (WSGI)  or Common Gateway Interface (CGI). Other gateway interfaces to consider are FastCGI and sCGI, however WSGI is better maintained.

WSGI or CGI provides a mechanism to deliver dynamic contents on the web server.

As you already know, static web pages are served with files that has a .htm or .html extension e.g. If we wish to serve a dynamic page the extension would be different, for example .cgi or .py or .php or .asp.

Running Python Scripts via CGI

You should not have to do anything to get CGI working on your server, but you do need to set up Python itself:
Lets modify http.conf, make a backup copy just in case you messed-up with the file you can easily restore it from the backup copy.

~ Locate the configuration file http.conf in c:\xampp\apache\conf\httpd.conf.
~ In http.conf look for this line: AddHandler cgi-script .cgi .pl .asp. Modify it so it looks like this: AddHandler cgi-script .cgi .pl .asp .py
~ You probably won't need to do this, but just to be sure, look for this line: Options Indexes FollowSymLinks. Ensure that it looks like this: Options Indexes FollowSymLinks Includes ExecCGI
~ At the top of each Python script you create, set the path to your version of Python. For instance, ours is in C:\Python27 so we write: #!/Python27/python

Example script, create folder at C:\xampp\htdocs and create script file with .cgi extension containing the following code within the folder:-

print """ Content-type: text/html\n\n
<!DOCTYPE html>
<html lang="en">
    <title>Server-side scripting</title>
    <h1>This is my first Server-side scripting in Python!</h1>

The code above is a minimum html document executed using the python print function.
Note precence of "\n\n" in second line of the above code, it is important for the script to work or you will get a "malformed header" error.

My script file is at: C:\xampp\htdocs\project\index.cgi
With your XAMPP server is running and you access http://localhost/project/ on the browser, you should see the body content of the code above as seen below;-

Running Python Scripts via WSGI

Using the WSGI approach, we need a module called "mod_wsgi". This module is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python 2.3+ based web applications under Apache.

Download mod_wsgi from this link. This is a small ".so" file with a long name identifying which combination of Apache and Python it supports. Rename it to and put it in your Apache Modules folder presumably located at C:\xampp\apache\modules if you did a default installation.

Next we modify http.conf as we deed in CGI section above.
~ Locate the configuration file http.conf in c:\xampp\apache\conf\httpd.conf.
~ In http.conf look for this line: AddHandler cgi-script .cgi .pl .asp. Modify it so it looks like this: AddHandler cgi-script .cgi .pl .asp .py
~ Add LoadModule wsgi_module modules/ to the end of the end of page of http.conf file.
~ Restart Apache: From the XAMPP control panel, click the stop button and wait for Apache to shut down. Then click the start button to bring it back up. If Apache fails to restart, you almost certainly have a problem with your httpd.conf file edits. Revert the changes you made and try to restart Apache again. If it works, you probably did something wrong with the httpd.conf edits or forgot to rename the file or named it incorrectly or you are using incompatible versions of python and
~ If it doesn't work, using the apache_start.bat file may give you a more informative error message to work with. If Apache fails to restart even after you fixed the httpd.conf file, try uninstalling Python and removing the file from the modules folder. If Apache still doesn't restart, even after a fresh reboot of the computer, I am very sorry you broke your web server and I hope you made that copy of htdocs I suggested above.

An alternative Apache server that comes with by defualt is the AMPPS server, download it and install to deliver dynamic contents on the web using python. AMPPS is a WAMP, MAMP and LAMP stack of Apache, MySQL, MongoDB, PHP, Perl & Python.

Finally, your file in .cgi or .py extension should load properly on your web browser;-

List of other web Servers that support WSGI can be found at this link.

That is it! Lets learn how to process html form data with Python


  1. Terimakasih ini sangat membantu

  2. Thanks man. This was very helpful. I was having the "malformed header" error. Searched everywhere online, even downloaded loads of videos. Only to get the simple solution to the problem here. Thanks for your clear and precise information.

  3. how can i add pandas library in python web application

    1. You will need to install it on your server if it isn't installed already. Usually, this will be done via your server bash console.