So you want to get Python working with XAMPP eh? Me too! So, apparently, do a lot of people; and pretty much no one who knows how is saying how from what I've found online in my cursory 5 minute search. Let's just jump right in, shall we?
Step 0: Install XAMPP & Python
You should have installed XAMPP already, but if you are a brand new "b" to the Server Side Dev ring, and you have stumbled upon this post looking for the catch all solution to getting your feet wet with Server Side Programming and have picked Python as your poison, go get XAMPP! It basically installs and configures itself, and there is an excellent tutorial on the apachefriends website to get you through this process:
You might not have installed Python already (although, if not, why did you choose Python over PHP or Ruby?). If you haven't installed it already, go download and install it now:
Step 1: Make a Backup
If you are like me, and have a few years worth of stuff in your XAMPP directory that you don't want to 'splode when you screw up the first time you try to get Python working with XAMPP, go ahead and just zip c:\xampp to x:\xampp-backup.zip now...
Step 2: Download mod_python
Download mod_python now...
Wait, which one do I download?
Good question! Check your version of Apache and Python to determine which one you need
- Enter the text (leave out the space between phpinfo and ()):
<? phpinfo (); ?>
into a new text document in notepad.
- Save the document as "test.php" (or something) in the htdocs directory of your xampp install.
- Start Apache (open the xampp control panel, c:\xampp\xampp-control.exe, and click "Start" next to Apache).
NOTE: If you have IIS running, stop it. (Control Panel, Administrative Tools, Services, IIS Admin, Stop (say yes to stopping those other services too). If you scroll up to "Apache" at this point and click on it, you will see which version you are running and can skip to the next step in the mod_python install).
- Browse to the document you saved in step 2 through your web browser (localhost\test.php). You can NOT just double click the file and expect it to be parsed by apache and run...
- Scroll down and view the output in the "apache2handler" section. The first box in the table should be "Apache Version" and should tell you which version of Apache you are running (mine is 2.2.4, so I downloaded the very last ".exe" file so that I would have Python 2.5 w/ Apache 2.2)
Step 3: install mod_python
- Double click the exe file
- Click "Next"
- It should find your Python install. If you have more than one version, select the one you want to use. Click "Next"
- Click "Next"
- There should be a pop-up asking where Apache is installed. If you installed xampp to c:\, then this will be c:\xampp\apache
Step 4: Configure Apache
- Open c:\xampp\apache\conf\httpd.conf
- Scroll down to the section with all the "LoadModule" lines (about line #67) and add this line:
LoadModule python_module modules/mod_python.so
Note: If you are unable to start Apache after this, go back and type the line in the conf file by hand instead of copying/pasting.
- Scroll down to the section with the <Directory "/xampp/htdocs"> tag ends (about line #232) and add these lines following the closing </Directory> tag:
<Directory "/xampp/htdocs/test"> AddHandler mod_python .py PythonHandler mptest PythonDebug On </Directory>
- Save and close the file.
- Restart Apache
- Create a new file (and folder) c:\xampp\htdocs\test\mptest.py
- Place the following text in the file, and save it (watch your formatting, Python is whitespace sensitive!):
from mod_python import apache def handler(req): req.content_type = 'text/plain' req.write("Hello World!") return apache.OK
Step 5: Testing Py!
Point your browser to http://localhost/test/mptest.py
You should see "Hello World!". If you don't, check this page out (and this one and this one). Also, make sure you got the path correct in the <Directory /> tag, and saved your mptest.py file to the right place.
What's next? Why not get a Python book? This one is really good:
Game Programming: The L Line, The Express Line to Learning (The L Line: The Express Line To Learning). That's right! Game programming in Python.
Some user comments:
<Directory /test> ... </Directory> <-- This is wrong!
But, the following works:
<Directory "C:/xampp/htdocs/test"> ... </Directory>
instead on adding the directory block, which turns on mod-python for a single folder, add the following at the end of the file (assumes you want to use the Publisher handler):
AddHandler mod_python .py
If you have any other comments that require code. Just send me an e-mail (cmcculloh-at-gmail-dot-com) and I'll post the code (so you don't have to worry about it getting mangled).