Tuesday, 29 April 2014

Running python along with PHP on Windows with Apache

Short howto:

1. Install Python on the server or your computer (adding Python.exe to the path)

2. download pre-compiled mod_wsgi.so from:
You should download right file for your Apache version and for your Python wersion:
for example mod_wsgi‑3.4.ap24.win32‑py3.4.zip is mod_wsgi.so for Apache 2.4 compiled as 32bit app and Python 3.4. This file works fine for me on XAMPP 1.8.0

3. Extract mod_wsgi.so into your /apache/modules/ directory (would be C:\xampp\apache\modules for XAMPP)

4. Add the following string into your /apache/conf/httpd.conf (somewhere among others LoadModule strings):
LoadModule wsgi_module modules/mod_wsgi.so

5. Say you want your Python stuff to work on URL http://localhost/my-python-stuff/:
Create /my-python-stuff/ directory inside of your localhost's public_html dir (/htdocs/localhost/www/ for XAMPP);
Create .htaccess and index.wsgi files in this directory;

Options +ExecCGI AddDefaultCharset utf-8  #add wsgi handler AddHandler wsgi-script .wsgi  #redirect all requests to index.wsgi RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.wsgi/$1
def application(environ, start_response): status = '200 OK' output = 'Hello World!' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
Now if you re-start/run your apache/XAMPP and open http://localhost/my-python-stuff/ , or any other URL deeper than /my-python-stuff/ (/my-python-stuff/test/ and so on), you will see Hello World message in your browser.