lunedì 23 luglio 2012

virtualenv + django + mod_wsgi + apache

A typical configration is when you want to setup an django in virtualenv running inside a Apache+mod_wsgi system. A simple recipe, and I stress SIMPLE, that can handle multiple deployments is the following. Lets create the following folders
  • /pyEnv - the python deployment environment
  • /pyEnv/log - where the deployed application may write its logs
  • /pyEnv/ve - where the virtualenv instance is deployed
  • /pyEnv/httpd2Confs - where the project shall deploy its wsgi configuration file (see below for a template)
  • /srv/www/vhosts/yourHostname - where the project can deploy static content
Moreover /pyEnv/httpd2Confs contains a file global.conf

WSGIDaemonProcess myGreatProcess user=apache group=apache threads=25 WSGIProcessGroup myGreatProcess
Usually the Apache configuration file that we are interested in is /etc/httpd/conf/httpd.conf and should be updated as follow

WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>
    ServerAdmin j.kirk@enterprise.uss
    DocumentRoot /srv/www/vhosts/yourHostName
    ServerName yourHostName
    ErrorLog logs/yourHostName
    CustomLog logs/yourHostName-access_log common

    Include /pyEnv/httpd2Confs/*.conf

    <Directory "/srv/www/vhosts/yourHostName">

        #
        # Possible values for the Options directive are "None", "All",
        # or any combination of:
        #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        #
        # Note that "MultiViews" must be named *explicitly* --- "Options All"
        # doesn't give it to you.
        #
        # The Options directive is both complicated and important.  Please see
        # http://httpd.apache.org/docs-2.2/mod/core.html#options
        # for more information.
        #
        Options Indexes FollowSymLinks

        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   Options FileInfo AuthConfig Limit
        #
        AllowOverride None

        #
        # Controls who can get stuff from this server.
        #
        Order allow,deny
        Allow from all
 </Directory>
A project wsgi template configuration may look like

###
# Configuration for Apache mod_wsgi module
# This file has to be INCLUDE in a <VirtualHost> tag
###

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

 ErrorLog /pyEnv/logs/YourProjectNameError.log
        CustomLog /pyEnv/logs/YourProjectName.log combined

    WSGIScriptAlias /pyEnv/pathToYourProjectWSGI/wsgi.py

    <Directory PATH_TO_DJANGO_PROJECT>
        Order allow,deny
        Allow from all
    </Directory>