essay on programming languages, computer science, information techonlogies and all.

Monday, July 25, 2016

Install mod_wsgi on Windows

There are number of way to make Apache web server service python code. mod_python, mod_wsgi and FastCGI. mod_pyhton and FastCGI weren't tamed on Windows Server 2008. But mod_wsgi can be configured to work and here is the installation procedure.

First, need to get mod_wsgi pre-built binary from 'Unofficial Windows Binaries for Python Extension Packages'. You need to select compiler, version and arch . I installed Apache from Apache Lounge with VC10 link so chose mod_wsgi-4.4.23+ap24vc10-cp34-cp34m-win32.whl.

Copy the file to c:\download folder and install it using pip.

C:\minute\orange>python -m pip install "c:\download\mod_wsgi-4.4.23+ap24vc10-cp3
4-cp34m-win32.whl"
Processing c:\download\mod_wsgi-4.4.23+ap24vc10-cp34-cp34m-win32.whl
Installing collected packages: mod-wsgi
Successfully installed mod-wsgi-4.4.23+ap24vc10
C:\minute\orange>    
The installed 'mod_wsgi.so' file is located at 'C:\WinPython-32bit-3.4.4.2\python-3.4.4\mod_wsgi.so' and I copied it to 'C:\Apache24\modules'.

Then open 'C:\Apache24\conf\httpd.conf' and edit content as below.

LoadModule wsgi_module modules/mod_wsgi.so
...
WSGIScriptAlias /wsgi "C:/WebPages/wsgi/wsgi_app.py"
<Directory "C:/WebPages/wsgi">
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>

Now restart the Apache service using Windows Service.
If service can't be restarted, then check error at C:\Apache24\logs\error.log. If error is like below talking about encodings, it is because PYTHONPATH is not set.
Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'
Add environmental variable of 'PYTHONPATH' like below.
At this point, you should be able to start Apache service. Then write first Hello-World python code that will be served by Apache server at 'C:\WebPages\wsgi\wsgi_app.py'. This code is originated from Brugbart blog but modified to work by Sumit question at stackoverflow.
def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
Then finally a tiny python step in to the wold wide web.

No comments: