Roshan Book

My Tech Notebook

Django deployment on linux machine

Using Django with Apache and mod_python

Apache with mod_python currently is the most robust setup for using Django on a production server.

mod_python ( is an Apache plug-in that embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements.

Django requires Apache 2.x and mod_python 3.x, and we prefer Apache’s prefork MPM, as opposed to the worker MPM.4


Configuring Apache is well beyond the scope of this book, so we’ll simply mention details as needed. Luckily, a number of great resources are available if you need to learn more about Apache. A few of them we like are as follows:
The free online Apache documentation, available via
Pro Apache, Third Edition (Apress, 2004) by Peter Wainwright, available via
Apache: The Definitive Guide, Third Edition (O’Reilly, 2002) by Ben Laurie and Peter Laurie, available via
Basic Configuration

To configure Django with mod_python, first make sure you have Apache installed with the mod_python module activated. This usually means having a LoadModule directive in your Apache configuration file. It will look something like this:3
LoadModule python_module /usr/lib/apache2/modules/

Then, edit your Apache configuration file and add the following:

SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On

Make sure to replace mysite.settings with the appropriate DJANGO_SETTINGS_MODULE for your site.2

This tells Apache, “Use mod_python for any URL at or under ‘/’, using the Django mod_python handler.” It passes the value of DJANGO_SETTINGS_MODULE so mod_python knows which settings to use.

Note that we’re using thedirective, not thedirective. The latter is used for pointing at places on your filesystem, whereaspoints at places in the URL structure of a Web site.would be meaningless here.2

Apache likely runs as a different user than your normal login and may have a different path and sys.path. You may need to tell mod_python how to find your project and Django itself.4
PythonPath “[‘/path/to/project’, ‘/path/to/django’] + sys.path”

You can also add directives such as PythonAutoReload Off for performance. See the mod_python documentation for a full list of options.

Note that you should set PythonDebug Off on a production server. If you leave PythonDebug On, your users will see ugly (and revealing) Python tracebacks if something goes wrong within mod_python.

Restart Apache, and any request to your site (or virtual host if you’ve put this directive inside ablock) will be served by Django.


If you deploy Django at a subdirectory — that is, somewhere deeper than “/” — Django won’t trim the URL prefix off of your URLpatterns. So if your Apache config looks like this:

SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On

then all your URL patterns will need to start with “/mysite/”. For this reason we usually recommend deploying Django at the root of your domain or virtual host. Alternatively, you can simply shift your URL configuration down one level by using a shim URLconf:
urlpatterns = patterns(”,
(r’^mysite/’, include(‘normal.root.urls’)),
Running Multiple Django Installations on the Same Apache Instance

It’s entirely possible to run multiple Django installations on the same Apache instance. You might want to do this if you’re an independent Web developer with multiple clients but only a single server.

To accomplish this, just use VirtualHost like so:1
NameVirtualHost *
# …
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
# …
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings

If you need to put two Django installations within the same VirtualHost, you’ll need to take a special precaution to ensure mod_python’s code cache doesn’t mess things up. Use the PythonInterpreter directive to give different directives separate interpreters:

# …

SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonInterpreter mysite
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings
PythonInterpreter mysite_other
The values of PythonInterpreter don’t really matter, as long as they’re different between the two Location blocks.
Running a Development Server with mod_python

Because mod_python caches loaded Python code, when deploying Django sites on mod_python you’ll need to restart Apache each time you make changes to your code. This can be a hassle, so here’s a quick trick to avoid it: just add MaxRequestsPerChild 1 to your config file to force Apache to reload everything for each request. But don’t do that on a production server, or we’ll revoke your Django privileges.1

If you’re the type of programmer who debugs using scattered print statements (we are), note that print statements have no effect in mod_python; they don’t appear in the Apache log, as you might expect. If you have the need to print debugging information in a mod_python setup, you’ll probably want to use Python’s standard logging package. More information is available at Alternatively, you can or add the debugging information to the template of your page.2
Serving Django and Media Files from the Same Apache Instance

Django should not be used to serve media files itself; leave that job to whichever Web server you choose. We recommend using a separate Web server (i.e., one that’s not also running Django) for serving media. For more information, see the “Scaling” section.1

If, however, you have no option but to serve media files on the same Apache VirtualHost as Django, here’s how you can turn off mod_python for a particular part of the site:

SetHandler None

Change Location to the root URL of your media files.

You can also use to match a regular expression. For example, this sets up Django at the site root but explicitly disables Django for the media subdirectory and any URL that ends with .jpg, .gif, or .png:

SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
SetHandler None
SetHandler None

In all of these cases, you’ll need to set the DocumentRoot directive so Apache knows where to find your static files.


14 responses to “Django deployment on linux machine

  1. Alex Somerfield November 9, 2011 at 8:06 am

    Hello Webmaster, I noticed that is ranking pretty low on Google and has a low Google PageRank. Now the Google PageRank is how Google is able to see how relevant your webpage is compared to all the other webpages online, if you cannot rank high at the top of Google, then you will NOT get the traffic you need. Now usually trying to get to the top of Google costs hundreds if not thousands of dollars and very highly optimized targeted marketing campaigns that takes a team of experts months to achieve. However, we can show you how to get to the top of Google with no out of pocket expenses (free traffic), no stupid ninja tricks, no silly mind control techniques, and this will be all white hat with no blackhat software or tactics that could possibly land you on bad terms with Google and put you in the dreaded “Google Sandbox”. We’ll show you how to easily capture all the targeted traffic you need, for free, multiple ways to land fast (not months) first-page rankings in Google and other major search engines (Bing, Yahoo, Ask, etc), even show you strategies on how to earn daily commissions just try Ranking Top of Google, please check out our 5 minute video.

  2. Ugg Felicity Boots 5450 November 17, 2011 at 11:27 pm

    Normally I don’t read post on blogs, but I wish to say that this write-up very forced me to take a look at and do so! Your writing style has been amazed me. Thank you, quite great post.

  3. VictorAdkins November 19, 2011 at 1:27 pm

    I dugg some of you post as I cogitated they were very useful invaluable

  4. Louis Vuitton Replica UK November 20, 2011 at 2:02 am

    Especially amazing post. I just stumbled upon your blog and wanted to mention that I’ve truly enjoyed browsing your weblog posts. In any case I will be subscribing for your rss feed and I hope you write again incredibly soon!

  5. Louis Vuitton Outlet UK November 21, 2011 at 1:45 pm

    This blog gives the light in which I can observe the reality. This is pretty good one and provides beneficial information. Thanks for this good blog.

  6. Louis Vuitton Bags For Cheap November 22, 2011 at 2:26 pm

    Good article, lots of superb facts. I want to show my girlftriend and ask them the issues they think.

  7. Cheap Louis Vuitton Bags November 23, 2011 at 1:49 pm

    That is definitely fascinating, You’re an overly professional blogger. I’ve joined your rss feed and stay up for in the hunt for more of your excellent post.

  8. Louis Vuitton Replica UK November 26, 2011 at 5:17 am

    Highly beneficial appreciate it, There’s no doubt that your audience would definitely want a whole lot more well written articles like this maintain up the wonderful work.

  9. Replica Louis Vuitton Handbags November 26, 2011 at 1:32 pm

    This really is actually my pretty first comment any weblog anywhere, Just joined the community, These are rather great tips for a first timer like myself. Thank You for the tips.

  10. Louis Vuitton wallets November 26, 2011 at 8:19 pm

    Thank you for your devotion to excellence in writing. While reading your post I related to the points made in this content. This really is excellent.

  11. Louis Vuitton Wallet November 28, 2011 at 11:28 am

    Thank you for this great post, I’ve emailed the url to my friend, sure he will turn into a standard viewer soon enough also.

  12. LV Sale November 29, 2011 at 11:20 am

    Wonderful post, you’ve pointed out some wonderful details , I besides believe this s a incredibly fantastic web webpage.

  13. Louis Vuitton Luggage Sale November 30, 2011 at 4:27 am

    Your article seems interesting, i’ve noted it my digg and stumble account.The point you are making is easy to recognize and effective.

  14. ebay sucks December 17, 2011 at 9:02 pm

    Have eBay suspend your account? I got suspended for absolutely no cause! I misplaced all of my revenue, my business had been online based. I had to find a solution. By the end I am back for real.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: