https://wiki.archlinux.org/api.php?action=feedcontributions&user=Jbsilva&feedformat=atomArchWiki - User contributions [en]2024-03-28T12:39:25ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Apache_HTTP_Server/mod_wsgi&diff=236327Apache HTTP Server/mod wsgi2012-11-22T00:48:29Z<p>Jbsilva: /* Installation */</p>
<hr />
<div>[[Category:Web Server]]<br />
== Introduction ==<br />
<br />
According to the [http://code.google.com/p/modwsgi/ project's site] :<br />
::The aim of mod_wsgi is to implement a simple to use Apache module which can host any Python application which supports the Python WSGI interface. The module would be suitable for use in hosting high performance production web sites, as well as your average self managed personal sites running on web hosting services. <br />
<br />
mod_wsgi is an [[Apache]] module that embeds a [http://www.python.org Python] application within the server and allow them to communicate through the Python WSGI interface as defined in the [http://www.python.org/dev/peps/pep-0333/ Python PEP 333]. WSGI is one of the Python ways to produce high quality and high performance web applications.<br />
<br />
WSGI provide a standard way to interface different web-apps without hassle. Several well-know python applications or frameworks provide wsgi for easy deployment and embedding. It means that you can embed your Django-powered blog and your project's Trac into a single Pylons application that wraps around them to deals with, say, authentication without modifying the formers.<br />
<br />
Example:<br />
* [http://pylonshq.com/ Pylons]<br />
* [http://www.djangoproject.com/ Django]<br />
* [http://turbogears.org/ Turbo-gear]<br />
* [http://trac.edgewall.org/ Trac]<br />
* [http://moinmo.in/ Moin-moin]<br />
* [http://www.zope.org/ Zope]<br />
<br />
mod_wsgi 3.x supports those Python versions: 2.3, 2.4, 2.5, 2.6, 3.1 (3.0 is '''NOT''' supported as Python's developers have dropped support for Python 3.0).<br />
<br />
== Installation ==<br />
<br />
=== Apache Module : mod_wsgi for Python 2.6 ===<br />
<br />
This document describes how to set up and test the Apache module mod_wsgi compiled against Python 2.6 on an Arch Linux system.<br />
<br />
==== Install Package ====<br />
<br />
The mod_wsgi package lives in extra, install it with pacman (or whatever you want) :<br />
<br />
# pacman -S mod_wsgi2<br />
<br />
==== Configure Apache ====<br />
* As requested by the installer, add the following line to <code>/etc/httpd/conf/httpd.conf</code>:<br />
LoadModule wsgi_module modules/mod_wsgi.so<br />
<br />
* Restart Apache<br />
<br />
# /etc/rc.d/httpd restart<br />
<br />
* Check to make sure that Apache loaded correctly<br />
<br />
==== Test mod_wsgi ====<br />
* Add this block to <code>/etc/httpd/conf/httpd.conf</code> if you are not using the latest apache 2.4:<br />
<pre><br />
WSGIScriptAlias /wsgi_app /home/www/wsgi-scripts/wsgi_app.py<br />
<br />
<Directory /home/www/wsgi-scripts><br />
Order allow,deny<br />
Allow from all<br />
</Directory><br />
</pre><br />
<br />
* If you use apache 2.4.x, add this block to <code>/etc/httpd/conf/httpd.conf</code><br />
<pre><br />
WSGIScriptAlias /wsgi_app /home/www/wsgi-scripts/wsgi_app.py<br />
<br />
<Directory /home/www/wsgi-scripts><br />
Order allow,deny<br />
Require all granted<br />
</Directory><br />
</pre><br />
<br />
* Create a file in <code>/home/www/wsgi-scripts</code> called <code>wsgi_app.py</code> with this code :<br />
<pre>#-*- coding: utf-8 -*-<br />
import pprint<br />
def wsgi_app(environ, start_response):<br />
""" Display the contents of the environ dictionary."""<br />
# produce some content<br />
output = pprint.pformat(environ)<br />
<br />
# send first header and status<br />
status = '200 OK'<br />
headers = [('Content-type', 'text/plain'),<br />
('Content-Length', str(len(output)))]<br />
start_response(status, headers)<br />
<br />
# wsgi apps should return and iterable, the following is acceptable too :<br />
# return [output]<br />
yield output<br />
<br />
# mod_wsgi need the *application* variable to serve our small app<br />
application = wsgi_app<br />
</pre><br />
<br />
* Restart Apache<br />
<br />
# /etc/rc.d/httpd restart<br />
<br />
* Check to make sure that Apache loaded correctly<br />
<br />
* Navigate to <code>http://localhost/wsgi_app</code> and discover the ugly <code>environ</code> dictionary..<br />
<br />
{{Note|While [[mod_python]] will execute any Python script that ends with .py, mod_wsgi only executes python ''applications''.}}<br />
<br />
=== Customization ===<br />
<br />
==== Use mod_wsgi and Python 3.1 ====<br />
<br />
This document explain how run mod_wsgi along Python 3.1 t instead of Python 2.6 that the Arch Linux binary package from repository offer. It will be assumed you have already installed the necessary packages (as described in [[ABS]] plus Python 3.1 : <code>pacman -S python3</code>).<br />
<br />
==== Compilation using ABS ====<br />
<br />
* Update your [[ABS]] to ensure you have the very last PKGBUILD version.<br />
<br />
* Prepare the mod_wsgi folder into your home:<br />
<br />
$ mkdir -p ~/abs<br />
$ cp -r /var/abs/extra/mod_wsgi ~/abs<br />
<br />
* Edit the PKGBUILD: replace the <code>--with-python</code> flag with the correct Python's version:<br />
<pre> --with-python=/usr/bin/python || return 1</pre><br />
by<br />
<pre>--with-python=/usr/bin/python3.1 || return 1</pre><br />
<br />
* Build the package (run this command inside the PKGBULD folder):<br />
<br />
$ makepkg -s<br />
<br />
* Install it (as a root):<br />
<br />
# pacman -U mod_wsgi-3.0-1-$ARCH.pkg.tar.gz<br />
<br />
* Restart Apache :<br />
<br />
# /etc/rc.d/httpd restart<br />
<br />
* Test your set-up with this small app :<br />
<br />
<pre>#-*- coding: utf-8 -*-<br />
def wsgi_app(environ, start_response):<br />
import sys<br />
output = str(sys.version)<br />
status = '200 OK'<br />
headers = [('Content-type', 'text/plain'),<br />
('Content-Length', str(len(output)))]<br />
start_response(status, headers)<br />
yield output<br />
<br />
application = wsgi_app</pre><br />
<br />
When browsing to the url you bound your app on you should see :<br />
<br />
3.1.1 (r311:74480, Aug 27 2009, 04:56:37) <br />
[GCC 4.4.1]<br />
<br />
If you read :<br />
<br />
2.6.4 (r264:75706, Oct 27 2009, 06:25:13) <br />
[GCC 4.4.1]<br />
<br />
You have failed to compile and install mod_wsgi against Python3.1. Retry and double check all the previous steps.<br />
<br />
== See Also ==<br />
*[http://code.google.com/p/modwsgi/ mod_wsgi project's site]<br />
*[http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide Quick Configuration Guide]<br />
*[http://httpd.apache.org/docs/2.4/upgrading.html Upgrading to 2.4 from 2.2]<br />
<br />
''This article is partially adapted from [[mod_python]]. Many thanks to the authors.''</div>Jbsilva