Apache & mod_wsgi Deployment

NOTE 1: This tutorial assumes that you already have Apache and mod_wsgi installed. If you’re on a shared hosting platform, and don’t have the ability to install mod_wsgi, have no fear. Your hosting provider probably already supports mod_fastcgi so check out the Apache & mod_fcgid Deployment tutorial.

NOTE 2: If you’re administrating your own system and looking for pointers on how to get mod_wsgi installed, check out the developer’s site; the documentation is verbose, but very complete: mod_wsgi main site.

Components

The following five components are involved in getting web requests through to MediaDrop with this setup. Don’t worry if this sounds like a lot! By this stage you already have three, and the remaining ones are very easy to set up.

Apache
the web server
mod_wsgi
Apache module that lets Apache host WSGI enabled Python applications
httpd.conf
Your apache configuration; tells mod_wsgi how to run your app
mediadrop.wsgi
The script that runs MediaDrop as a WSGI application
mediadrop
The reason we’re here!

Instructions

NOTE 1: You should have already created a deployment.ini file and set the permissions on the data subdirectories as outlined in Step 6: Production Deployments

NOTE 2: The following instructions assume that you’re deploying MediaDrop to http://site.example/my_media/. To deploy MediaDrop to any other path, simply replace all references to /my_media below with your desired path. If MediaDrop should run at the URL root (no subdirectory), just remove the references to /my_media entirely.

NOTE 3: We will not actually be creating a my_media directory, but we will use aliases in the Apache config to make sure that requests to http://site.example/my_media/ are passed to MediaDrop.

First, copy the mediadrop.wsgi file from /path/to/mediadrop_install/deployment-scripts/mod_wsgi/mediadrop.wsgi to the directory where your deployment.ini is. Then edit the paths in the wsgi file to point to your own MediaDrop installation and virtual environment. The two lines you need to edit are at the top of the file, and look like this:

deployment_config = '/path/to/deployment.ini'
temp_dir = '/path/to/data/tmp'

Finally, you will need to add the following lines to your Apache configuration. Depending on your setup, you may want to add it to the main httpd.conf file, or inside a VirtualHost include.

Make sure that you replace all references to /path/to/mediadrop_install/ and /path/to/venv/ with the correct paths for your own MediaDrop source code and virtual environment.

# You can tweak the WSGIDaemonProcess directive for performance, but this
# will work for now.
# Relevant doc pages:
#     http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
#     http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
# Hint: pay attention to issues surrounding worker-mpm and prefork-mpm.

# For best performance the number of processes should equal the number of CPU 
# cores (but please note that each process may use about 500 MB RAM).
WSGIDaemonProcess mcore \
    processes=2 \
    threads=1 \
    display-name=%{GROUP} \
    python-path=/path/to/venv/lib/python2.6/site-packages \
    python-eggs=/path/to/data/python-egg-cache

WSGIProcessGroup mcore

# Intercept all requests to /my_media/* and pass them to mediadrop.wsgi
WSGIScriptAlias /my_media /path/to/mediadrop.wsgi

# Create an exception for media and podcast image from your data directory
AliasMatch ^/my_media/images/(media|podcasts)(.*) /path/to/data/images/$1$2

# Create an exception for all static MediaDrop content
AliasMatch ^/my_media/(admin/)?(images|scripts|styles)(.*) /path/to/mediadrop_install/mediadrop/public/$1$2$3

# Create an exception for your custom appearance css and images
Alias /my_media/appearance /path/to/data/appearance

# Make all the static content accessible
<Directory /path/to/mediadrop_install/mediadrop/public/*>
    Order allow,deny
    Allow from all
    Options -Indexes
</Directory>

Performance Enhancements

By default, all files are served through MediaDrop. The configuration above ensures that Apache will serve all static files (.css, .js, and images) directly, but MediaDrop will still check for static files before serving any page. There are two speedups we can enable here.

First, edit one line in /path/to/deployment.ini. Find the static_files line, and set it to false.

static_files = false

The second speedup is only available if you have mod_xsendfile installed and enabled in Apache. MediaDrop can take advantage of mod_xsendfile and have Apache serve all media files (.mp3, .mp4, etc.) directly. To enable this, edit another line in /path/to/deployment.ini. Find the files_serve_method line, and set it to apache_xsendfile.

files_serve_method = apache_xsendfile

Changing the MediaDrop Source Code

If you make any changes to your MediaDrop installation while Apache is running (eg. if you upgrade MediaDrop or make any customizations), you’ll need to make sure that mod_wsgi recognizes those changes.

The easiest way to do this is to ‘touch’ the .wsgi script. This will modify the ‘last modified on’ timestamp of the file, so that mod_wsgi thinks it has been updated and will read and re-load it.

# Navigate to the directory where your modified mediadrop.wsgi is
cd /path/to/...

# Force a refresh of the MediaDrop code
touch mediadrop.wsgi
You're reading the documentation for MediaDrop 0.11dev (current git master). For the latest stable release please consult the documentation for MediaCore CE 0.10.