Installing Apache mod_wsgi in Arch Linux

This covers how to install and start using mod_wsgi with Apache on Arch Linux.

Install WSGI for Apache

sudo pacman -S mod_wsgi

For more details refer to Arch Linux Wiki page on mod_wsgi.

Add the module to Apache's config file /etc/httpd/conf/httpd.conf

LoadModule wsgi_module modules/mod_wsgi.so

Tell Apache when and where to use WSGI

Apache will only run WSGI when you tell it to. By adding a WSGIScriptAlias in the Apache host entry, Apache will know to when to invoke WSGI. You can create your own Python file, but it has to follow a few rules to return data correctly. Applications like Django create a wsgi.py file for you, and you just need to point Apache at that file.

This script alias line will alias the URL /myapp to the index.py file.

WSGIScriptAlias /myapp /srv/http/index.py

Example Apache VirtualHost entry

<VirtualHost *:80>
    ServerAdmin nanodano@devdungeon.com.com
    DocumentRoot "/srv/http/devdungeon.com.com"
    ServerName devdungeon.com.com
    ServerAlias www.devdungeon.com.com
    ErrorLog "/var/log/httpd/devdungeon.com-error_log"
    CustomLog "/var/log/httpd/devdungeon.com-access_log" common
    WSGIScriptAlias / /srv/http/devdungeon.com/index.py
</VirtualHost>

Example index.py file

def wsgi_app(environ, start_response):
    output = "<html><body><h1>WSGI working!</ht></body></html>\n".encode('utf-8')
    status = '200 OK'
    headers = [('Content-type', 'text/html'),
               ('Content-Length', str(len(output)))]
    start_response(status, headers)
    yield output


# mod_wsgi needs the "application" variable to serve our small app
application = wsgi_app