{"id":76001,"date":"2020-01-20T12:50:20","date_gmt":"2020-01-20T07:20:20","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=76001"},"modified":"2024-04-12T14:17:44","modified_gmt":"2024-04-12T08:47:44","slug":"using-django-with-apache-and-mod_python","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/","title":{"rendered":"Using Django with Apache and mod_python"},"content":{"rendered":"<p>Apache with mod_python currently is the most robust setup for using Django on a production server. mod_python (http:\/\/www.djangoproject.com\/r\/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\u2019s prefork MPM, as opposed to the worker MPM.<\/p>\n<p><strong>Note<\/strong> &#8211; Configuring Apache is <em>well<\/em> beyond the scope of this book, so we\u2019ll 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:<\/p>\n<ul>\n<li>The free online Apache documentation, available via http:\/\/www.djangoproject.com\/r\/apache\/docs\/<\/li>\n<li><em>Pro Apache, Third Edition<\/em> (Apress, 2004) by Peter Wainwright, available via http:\/\/www.djangoproject.com\/r\/books\/pro-apache\/<\/li>\n<li><em>Apache: The Definitive Guide, Third Edition<\/em> (O\u2019Reilly, 2002) by Ben Laurie and Peter Laurie, available via http:\/\/www.djangoproject.com\/r\/books\/apache-pra\/<\/li>\n<\/ul>\n<p><u>Basic Configuration<\/u> &#8211; 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:<\/p>\n<p>LoadModule python_module \/usr\/lib\/apache2\/modules\/mod_python.so<\/p>\n<p>Then, edit your Apache configuration file and add the following:<\/p>\n<p>&lt;Location &#8220;\/&#8221;&gt;<br>\nSetHandler python-program<br>\nPythonHandler django.core.handlers.modpython<br>\nSetEnv DJANGO_SETTINGS_MODULE mysite.settings<br>\nPythonDebug On<br>\n&lt;\/Location&gt;<\/p>\n<p>Make sure to replace mysite.settings with the appropriate DJANGO_SETTINGS_MODULE for your site. This tells Apache, \u201cUse mod_python for any URL at or under \u2018\/\u2019, using the Django mod_python handler.\u201d It passes the value of DJANGO_SETTINGS_MODULE so mod_python knows which settings to use. Note that we\u2019re using the &lt;Location&gt; directive, not the &lt;Directory&gt; directive. The latter is used for pointing at places on your filesystem, whereas &lt;Location&gt; points at places in the URL structure of a Web site. &lt;Directory&gt; would be meaningless here.<\/p>\n<p>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.<\/p>\n<p>PythonPath &#8220;[&#8216;\/path\/to\/project&#8217;, &#8216;\/path\/to\/django&#8217;] + sys.path&#8221;<\/p>\n<p>You can also add directives such as PythonAutoReload Off for performance. 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\u2019ve put this directive inside a &lt;VirtualHost&gt; block) will be served by Django.<\/p>\n<p><strong>Note &#8211; If you deploy Django at a subdirectory<\/strong> \u2014 that is, somewhere deeper than \u201c\/\u201d \u2014 Django won\u2019t trim the URL prefix off of your URLpatterns. So if your Apache config looks like this:<\/p>\n<p>&lt;Location &#8220;\/mysite\/&#8221;&gt;<br>\nSetHandler python-program<br>\nPythonHandler django.core.handlers.modpython<br>\nSetEnv DJANGO_SETTINGS_MODULE mysite.settings<br>\nPythonDebug On<br>\n&lt;\/Location&gt;<\/p>\n<p>then all your URL patterns will need to start with &#8220;\/mysite\/&#8221;. 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:<\/p>\n<p>urlpatterns = patterns(&#8221;,<br>\n(r&#8217;^mysite\/&#8217;, include(&#8216;normal.root.urls&#8217;)),<br>\n)<\/p>\n<p><strong>Running Multiple Django Installations on the Same Apache Instance<\/strong> &#8211; It\u2019s entirely possible to run multiple Django installations on the same Apache instance. You might want to do this if you\u2019re an independent Web developer with multiple clients but only a single server. To accomplish this, just use VirtualHost like so:<\/p>\n<p>NameVirtualHost *<br>\n&lt;VirtualHost *&gt;<br>\nServerName www.example.com<br>\n# &#8230;<br>\nSetEnv DJANGO_SETTINGS_MODULE mysite.settings<br>\n&lt;\/VirtualHost&gt;<\/p>\n<p>&lt;VirtualHost *&gt;<br>\nServerName www2.example.com<br>\n# &#8230;<br>\nSetEnv DJANGO_SETTINGS_MODULE mysite.other_settings<br>\n&lt;\/VirtualHost&gt;<\/p>\n<p>If you need to put two Django installations within the same VirtualHost, you\u2019ll need to take a special precaution to ensure mod_python\u2019s code cache doesn\u2019t mess things up. Use the PythonInterpreter directive to give different &lt;Location&gt; directives separate interpreters:<\/p>\n<p>&lt;VirtualHost *&gt;<br>\nServerName www.example.com<br>\n# &#8230;<br>\n&lt;Location &#8220;\/something&#8221;&gt;<br>\nSetEnv DJANGO_SETTINGS_MODULE mysite.settings<br>\nPythonInterpreter mysite<br>\n&lt;\/Location&gt;<\/p>\n<p>&lt;Location &#8220;\/otherthing&#8221;&gt;<br>\nSetEnv DJANGO_SETTINGS_MODULE mysite.other_settings<br>\nPythonInterpreter mysite_other<br>\n&lt;\/Location&gt;<br>\n&lt;\/VirtualHost&gt;<\/p>\n<p>The values of PythonInterpreter don\u2019t really matter, as long as they\u2019re different between the two Location blocks.<\/p>\n<p><strong>Running a Development Server with mod_python<\/strong> &#8211; Because mod_python caches loaded Python code, when deploying Django sites on mod_python you\u2019ll need to restart Apache each time you make changes to your code. This can be a hassle, so here\u2019s 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\u2019t do that on a production server, or we\u2019ll revoke your Django privileges.<\/p>\n<p>If you\u2019re the type of programmer who debugs using scattered print statements (we are), note that print statements have no effect in mod_python; they don\u2019t appear in the Apache log, as you might expect. If you have the need to print debugging information in a mod_python setup, you\u2019ll probably want to use Python\u2019s standard logging package. More information is available at http:\/\/docs.python.org\/lib\/module-logging.html. Alternatively, you can or add the debugging information to the template of your page.<\/p>\n<p><strong>Serving Django and Media Files from the Same Apache Instance<\/strong> &#8211; 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\u2019s not also running Django) for serving media. If, however, you have no option but to serve media files on the same Apache VirtualHost as Django, here\u2019s how you can turn off mod_python for a particular part of the site:<\/p>\n<p>&lt;Location &#8220;\/media\/&#8221;&gt;<br>\nSetHandler None<br>\n&lt;\/Location&gt;<\/p>\n<p>Change Location to the root URL of your media files. You can also use &lt;LocationMatch&gt; 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:<\/p>\n<p>&lt;Location &#8220;\/&#8221;&gt;<br>\nSetHandler python-program<br>\nPythonHandler django.core.handlers.modpython<br>\nSetEnv DJANGO_SETTINGS_MODULE mysite.settings<br>\n&lt;\/Location&gt;<\/p>\n<p>&lt;Location &#8220;\/media\/&#8221;&gt;<br>\nSetHandler None<br>\n&lt;\/Location&gt;<\/p>\n<p>&lt;LocationMatch &#8220;\\.(jpg|gif|png)$&#8221;&gt;<br>\nSetHandler None<br>\n&lt;\/LocationMatch&gt;<\/p>\n<p>In all of these cases, you\u2019ll need to set the DocumentRoot directive so Apache knows where to find your static files.<\/p>\n<p><strong>Error Handling<\/strong> &#8211; When you use Apache\/mod_python, errors will be caught by Django \u2014 in other words, they won\u2019t propagate to the Apache level and won\u2019t appear in the Apache error_log.<br>\nThe exception to this is if something is really messed up in your Django setup. In that case, you\u2019ll see an \u201cInternal Server Error\u201d page in your browser and the full Python traceback in your Apache error_log file. The error_log traceback is spread over multiple lines. (Yes, this is ugly and rather hard to read, but it\u2019s how mod_python does things.)<\/p>\n<p><strong>Handling a Segmentation Fault<\/strong> &#8211; Sometimes, Apache segfaults when you install Django. When this happens, it\u2019s almost always one of two causes mostly unrelated to Django itself:<\/p>\n<p>\uf0fc It may be that your Python code is importing the pyexpat module (used for XML parsing), which may conflict with the version embedded in Apache..<br>\n\uf0fc It may be because you\u2019re running mod_python and mod_php in the same Apache instance, with MySQL as your database back-end. In some cases, this causes a known mod_python issue due to version conflicts in PHP and the Python MySQL back-end.<\/p>\n<p>If you continue to have problems setting up mod_python, a good thing to do is get a bare-bones mod_python site working, without the Django framework. This is an easy way to isolate mod_python-specific problems. The article \u201cGetting mod_python Working\u201d details this procedure: http:\/\/www.djangoproject.com\/r\/articles\/getting-modpython-working\/.<\/p>\n<p>The next step should be to edit your test code and add an import of any Django-specific code you\u2019re using \u2014 your views, your models, your URLconf, your RSS configuration, and so forth. Put these imports in your test handler function and access your test URL in a browser. If this causes a crash, you\u2019ve confirmed it\u2019s the importing of Django code that causes the problem. Gradually reduce the set of imports until it stops crashing, so as to find the specific module that causes the problem. Drop down further into modules and look into their imports as necessary. For more help, system tools like ldconfig on Linux, otool on Mac OS, and ListDLLs (from SysInternals) on Windows can help you identify shared dependencies and possible version conflicts.<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache with mod_python currently is the most robust setup for using Django on a production server. mod_python (http:\/\/www.djangoproject.com\/r\/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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8884],"class_list":["post-76001","page","type-page","status-publish","hentry","category-django-web-development","tag-using-django-with-apache-and-mod_python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using Django with Apache and mod_python - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Django with Apache and mod_python - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Apache with mod_python currently is the most robust setup for using Django on a production server. mod_python (http:\/\/www.djangoproject.com\/r\/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...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:44+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/\",\"name\":\"Using Django with Apache and mod_python - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-20T07:20:20+00:00\",\"dateModified\":\"2024-04-12T08:47:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Django with Apache and mod_python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Django with Apache and mod_python - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/","og_locale":"en_US","og_type":"article","og_title":"Using Django with Apache and mod_python - Tutorial","og_description":"Apache with mod_python currently is the most robust setup for using Django on a production server. mod_python (http:\/\/www.djangoproject.com\/r\/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...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:44+00:00","twitter_misc":{"Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/","name":"Using Django with Apache and mod_python - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-20T07:20:20+00:00","dateModified":"2024-04-12T08:47:44+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/using-django-with-apache-and-mod_python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Using Django with Apache and mod_python"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/76001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=76001"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/76001\/revisions"}],"predecessor-version":[{"id":83480,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/76001\/revisions\/83480"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=76001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=76001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=76001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}