0

How to configure Apache to run Django application?

What are the tricks to set up a Django application for production under Apache webserver?

How to make /admin interface secure?

How to run another site (either php or static html-based) on the same domain?

How to make sure that plain text files are served directly by the webserver to insure the fastest response?

Evgeny's avatar
13.2k
Evgeny
updated 2010-04-15 00:46:41 -0500, asked 2010-04-15 00:22:54 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

3 Answers

0

Hi Evgeny, I was hoping you could now share the steps to deploy Askbot on shared web hosting, as you mentioned above. I just posted a question on this (question = 12711). Many thanks.

valuenaut's avatar
1
valuenaut
answered 2014-08-31 10:32:33 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments
0

Below is a sample configuration that has been tested for Askbot. This recipe will work only if you can edit the apache configuration directly (basically assumes that you have root access to the webserver). What to do if you don't have root access - is a subject of a future post.

This walkthrough also assumes the following:

  • you have mod_wsgi mod_ssl and mod_rewrite working on apache
  • if you are planning to use php (or other) applications along with django - you have handlers for those files set up for the directories where those other files reside

Finally in this example django site will run under url / and for the sake of an illustration - there will be a blog (maybe php) under say /blog.

Other properties of this setup are that /admin urls are forced to go through https - secure connection, and non-admin urls - through regular unencrypted connection, and all media files served by the Apache server directly. It is a common oversight to leave static files - like images and style sheets served by the Django application.

Also notice that the main site is installed in directory /path/to/mysite/production. You should seriously consider setting up a clone of the same setup under say /path/to/mysite/staging and reserve it for experimental installs and migrations. You don't want to do brain surgery on a running site :).

Later you can either just carefully swap directories staging and production (you must be very careful when doing that), or create two versions for the apache setup for example production1.conf and production2.conf where roles of directories would rotate (and in that case it's probably better to name them just node1 and node2.

Finally - always test under Apache (or other webserver) before deploying - to minimize crisis management, do not use python manage.py runserver because conditions of these two environments are not identical. Specifically in the two cases system accounts running the two servers are most likely different and the file access permissions for them may differ too.

< VirtualHost 74.208.164.82:80 >
   ServerAdmin forum@example.com
    ServerName www.example.com

    #document root exposes stuff underneath to everyone
    #some things will be intercepted by django, but you 
    #must be careful so that you don't show contents of your settings.py 
    #and any other files containing source code, etc
    #it's a good idea to verify that you are not exposing things you shouldnt be
    DocumentRoot /path/to/mysite/production

    #uncomment if you use html or php files as well or comment if you don't
    DirectoryIndex index.html index.php

    #set up aliases to non-django resources
    #for example here could be your php blog (separately make sure that php is on in that dir)
    #notice - no trailing slash here. it's important that way /blog will be a valid url too
    Alias /blog /path/to/mysite/production/blog
    #add more Alias lines as needed

    #run mod_wsgi process for django in daemon mode
    #this allows avoiding confused timezone settings when ...
(more)
Evgeny's avatar
13.2k
Evgeny
updated 2010-04-15 17:41:46 -0500, answered 2010-04-15 00:44:21 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments
0

congratulations. using your detailed explanations i finally put my admin app running on ubuntu server 12.04 using apache2. special detail is your line about Alias /admin/media/ /usr/local/lib/python2.6/site-packages/django/contrib/admin/media/

that in my server i solved with a symbolic link on root server of apache2 (/var/www).

ln -s /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static /var/www/static

with this link finally my admin app on django have css, js .. etc.

today is 17.june.2013 and asap i will update django docs because we have lots of explanations from people never put complete solution like (install server, install http server, install apache2, install django, install flup and put all pieces together).

your samples have make i go forward some steps, but my journey is some hard because we have pending:

using sqlite3 (directory with db is write protect); using shared host with hostgator.com;

we use python 2.7 (ubuntu server amd64) we use django 1.6 (beta) here on 1.6 admin app alredy come active this is good for my app. until 1.5 developer may enable admin app.

thanks

from gsavix@gmail.com sao paulo - brazil

gsavix's avatar
1
gsavix
answered 2013-06-17 19:38:44 -0500
edit flag offensive 0 remove flag delete link

Comments

Hi, thanks if you really would like to modify the writeups - please make pull requests via github. Re: sqlite - don't use it for production. Shared hosting may bring additional challenges too.

Evgeny's avatar Evgeny (2013-06-17 19:43:10 -0500) edit
add a comment see more comments