Apache2 Virtual Host Configuration

This is an Apache2 configuration file for name based virtual hosting.

As you can see in the following listing, there are several placeholders, that must be filled to make this work.

Usage

As you may notice, there are three different types of placeholders.

[[placeholder_name]]

These placeholders must be filled manually. Most noticable is line 4, where you must set the server name.

ServerName    [[SERVER_NAME]]

${placeholder_name}
These placeholders are filled by Apache itsself. Only mess with them, if you do exactly know what you are doing.
{{ placeholder_name }}

These placeholders do look familiar, don’t they? These are Django templatetags. You may fill them manually (please refer to the provided resources in the comments), but you can Django let them fill them for you during project creation. This will render the file through Django’s template engine and fill these placeholders:

$ django-admin startproject --template=/path/to/template --name apache2_vhost.sample

Concept

This will set up a name based virtual host that uses mod_wsgi to interact with Django.

It will serve static- and media-files from the default locations set in settings/common.py. This is not a production-setting, but is well suited for development purposes.

Line 10: Alias /static/   {{ project_directory }}/run/static
Serve static files from STATIC_ROOT under STATIC_URL. Note lines 36 - 40, where the directory is made accessible for Apache.
Line 15: Alias /media/    {{ project_directory }}/run/media
Serve media files from MEDIA_ROOT under MEDIA_URL. Note lines 45 - 49, where the directory is made accessible for Apache.

The dynamic Django content is served using the WSGI-application. Apache2 will use mod_wsgi in Daemon-mode. This is in fact the preferred way of deploying Django with Apache2, so you will not have to mess with these settings.

Line 18: WSGIScriptAlias  /   {{ project_directory }}/{{ project_name }}/wsgi.py
This must be set to the absolute filesystem path to the WSGI-application.
Line 27: WSGIDaemonProcess ...
This sets the name of the daemon process. Using Django’s template engine, this will be set to the name of your project. Please notice the python-path-parameter. It is prepared to a virtualenv-setup, but frankly, it must contain the project directory and the path to Python’s site-packages.
Line 31: WSGIProcessGroup ...
Specifies a distinct name for the daemon process’s group.

Source

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<VirtualHost *:80>
    # This is name based virtual hosting. So place an appropriate server name
    #   here. Example: django.devsrv.local
    ServerName  [[SERVER_NAME]]
    ServerAdmin webmaster@localhost

    # This alias makes serving static files possible.
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    Alias /static/  {{ project_directory }}/run/static/

    # This alias makes serving media files possible.
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    Alias /media/  {{ project_directory }}/run/media/

    # Insert the full path to the wsgi.py-file here
    WSGIScriptAlias /   {{ project_directory }}/{{ project_name }}/wsgi.py

    # PROCESS_NAME specifies a distinct name of this process
    #   see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
    # PATH/TO/PROJECT_ROOT is the full path to your project's root directory, 
    #   containing your project files
    # PATH/TO/VIRTUALENV/ROOT: If you are using a virtualenv specify the full
    #   path to its directory.
    #   Generally you must specify the path to Python's site-packages.
    WSGIDaemonProcess   {{ project_name }}  python-path={{ project_directory }}:{{ project_directory }}/../lib/python2.7/site-packages

    # PROCESS_GROUP specifies a distinct name for the process group
    #   see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIProcessGroup
    WSGIProcessGroup    {{ project_name }}

    # Serving static files from this directory
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    <Directory {{ project_directory }}/run/static>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    # Serving media files from this directory
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    <Directory {{ project_directory }}/run/media>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    LogLevel warn

    # PROJECT_NAME is used to seperate the log files of this application
    ErrorLog    ${APACHE_LOG_DIR}/{{ project_name }}_error.log
    CustomLog   ${APACHE_LOG_DIR}/{{ project_name }}_access.log combined
</VirtualHost>