Step 1: Installing Python
The current version of Python at the time of writing is 2.6. If you have version 2.6 installed, you will need to uninstall this and install the previous version 2.5.2 (which includes fixes for bugs in version 2.5 of Python) as the mod_python module doesn't currently support version 2.6 of Python. If you have not yet installed Python then just install version 2.5.2. For Windows, the easiest option is to download the Windows Installer (formerly known as Microsoft Installer) package (.msi). You can download this from Python.org's downloads page.
Installing Python 2.5.2 should hopefully be a pain-free process as the installation wizard should essentially do everything for you. All you need do is select a target installation directory. The default is "C:\Python25".
Step 2: Installing Apache HTTP Web Server
With Python installed, the next step is to install the Apache HTTP Web Server. The current version is 2.2.10. I recommend you install this version if you haven't installed the web server already. If you have an older version installed then it shouldn't matter too much. On Windows, you will be asked whether to install Apache as a service or whether you wish to run the web server manually. Your life will probably be made a lot easier if you install it as a service.
Once installed, run the Apache web server in order to test that it works. By default it serves pages on port 80 (the default for serving web pages) on your computer, therefore you should be able to see whether everything is fine by opening your browser and typing:
http://127.0.0.1/
If everything is working then you will see the message:
"It works!"
If you see anything else e.g. HTTP error code 404 (- page not found) then something has gone wrong and this needs to be fixed before proceeding. What you may find that Microsoft's Internet Information Services (IIS) is running on port 80 if you added this service - it is installed by default in Windows Vista. In this case, it is likely Apache willl be serving pages on port 8080, rather than port 80. If this is the case, you may check to see whether Apache is working by navigating to:
http://127.0.0.1:80
in your web browser. If you need to get both Microsoft IIS and the Apache Web Server working at once on the same machine then I found the following article at Monochrome Blog useful. You may also find the official documentation for Using Apache with Microsoft Windows of use.
With the Apache HTTP Web Server working, we can proceed to installation of mod_python.
Step 3: Installing Mod_python
is the Apache module necessary to run the Python interpreter from within the Apache web server so that your Python CGI pages may be executed (and the resulting HTML dispayed to the client). You must have installed both Python and the Apache HTTP Web Server before installing this module. The present version of this software is 3.3.1. Download the Win32 binaries from the Apache HTTP Server Project. The installer will take care of installing the Mod_python module for you. Remember that you need to have an older version of Python than version 2.6 in order for the module to install.
WARNING: When installing the Mod_python module on Windows Vista, ensure that you run the install as an admininstrator (right-click on the installer and select "Run as Administrator"), otherwise the module will not install correctly! This is due to the fact that with User Account Control (UAC) enabled in Windows Vista, only applications running within an administrative context may modify files in the Program Files folder. You might find the following article useful in improving your understand of UAC in Windows Vista: Understanding and Configuring User Account Control in Windows Vista.
This isn't the end of the story for installing Mod_python. We need to make a few changes to the Apache web server configuration files in order to instruct the web server to utilise the new module.
We need to edit the main web server configuration file named "httpd.conf". This can be found in the "conf" directory in your Apache install directory:
e.g. "C:\Program Files\Apache Software Foundation\Apache2.2\conf"
In order to modify this file, you will need to use an application running within an administrative context on Windows Vista. This is again due to the fact that only applciations running in an administrative context may modify files within the "Program Files" folder if UAC is enabled. Windows XP users won't face this dilemma. Windows Vista users may either temporarily disable UAC or use an application such as Notepad running in an administrative context. In order disable UAC, take a look at the article "Disable User Account Control(UAC) the Easy Way on Windows Vista". Alternatively, open the command prompt in an administrative context by opening the start menu, type "cmd" into the search bar to start the command prompt, however instead of hitting the return key, use the key combination:
CTRL + SHIFT + RETURN
This will launch the command prompt in an administration context. Now you may navigate to directory containing httpd.conf and edit it using Notepad. For example:
cd C:\Program Files\Apache Software Foundation\Apache2.2\conf
notepad httpd.conf
This will allow you to edit the httpd.conf file without disabling UAC.
Within the web server configuration file, you will notice many lines beginning with "LoadModule". Add the line:
LoadModule python_module modules/mod_python.so
in order to instruct Apache to utilise the Mod_python module. You will also need to create a directory which will contain your Python CGI scripts (i.e. .py files). For example, I created the directory "python" within the "htdocs" folder of my Apache installation i.e.
C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/python
This can be achieved easily via the Windows Explorer interface. You may alternatively use the "mkdir" command from the command prompt. With this directory, we need to tell Apache to use the Mod_python module to handle files with the extension .py within this directory. We can do this as follows:
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/python">
AddHandler mod_python .py
PythonHandler mptest
PythonDebug On
</Directory>
Step 4 - A First Python CGI Script
Having installed Python, the Apache HTTP Web Server and Mod_python, we should now be able to run our first Python CGI script! To create a first CGI script in Python, type the following into a text editor and save it in a file called "mptest.py":
from mod_python import apache
def handler(req):
req.content_type = 'text/plain'
req.write("Hello World!")
return apache.OK
This is the Python CGI equivalent of the famous "Hello World" example given to anyone learning to program in a new language for the first time.
Warning:If you are going to place your Python (.py) files in any directory within the Program Files directory then may wish to temporarily disable UAC or use a directory outside of Program Files for serving your Python CGI scripts, otherwise you will need to edit your scripts using an application running in an administrative ocntext each time (e.g. as we did to edit the httpd.conf file above).
With our first Python CGI script created, we should now be able to view it! Simply, open your web browser and enter the URL on which your Apache installation is serving pages e.g.
http://127.0.0.1:8080/python/mptest.py
Hopefully, you should see that "Hello World!" is printed out!
Useful Links
- Apache HTTP Server Project - "Download the Apache HTTP Server" - http://httpd.apache.org/download.cgi
- Apache HTTP Server Project - "Download Mod_python" - http://httpd.apache.org/modules/python-download.cgi
- Apache HTTP Server Project - "Using Apache with Microsoft Windows" - http://httpd.apache.org/docs/2.0/platform/windows.html
- Microsoft Technet - "Understanding and Configuring User Account Control in Windows Vista" - http://technet.microsoft.com/en-us/library/cc709628.aspx
- Mod_python - "Apache / Python Integration" - http://www.modpython.org/
- Monochrome Blog - "Running Apache and IIS 6 Together on the Same Server" - http://blog.monochrome.co.uk/2006/06/running-apache-and-iis-6-together-on-the-same-server/
- Python.org - "Download Python Software" - http://www.python.org/download/
- The How-To Geek - "How to Disable User Account Control (UAC) the Easy Way on Windows Vista" - http://www.howtogeek.com/howto/windows-vista/disable-user-account-control-uac-the-easy-way-on-windows-vista/
No comments:
Post a Comment