How to deploy a Django page on the Apache HTTP Server on Windows

Note that you may need to make further configurations in addition to the ones below in order to make your server safe from hacking. The guide below only covers how to set up the server to get the basics going, and I only used it for testing. One specific example of issues to keep in mind is where you place your Django code. For more info on that see the “Where should this code live” section in the Django tutorial at the following link (replace 1.11 in the link with the Django version number you are using): https://docs.djangoproject.com/en/1.11/intro/tutorial01/

This process has been tested on Windows 7 and Windows XP with Apache HTTP Server 2.2.22 using mod_wsgi 3.3 and Django 1.4.0.

1. Download the 2.2.x version of the Apache HTTP Server named “Win32 Binary including OpenSSL 0.9.8t (MSI Installer)” from here: http://httpd.apache.org/download.cgi

2. If you only want to test your server, follow the “Steps to Installing Apache 2.2 on Windows Vista” found in the link below (step nr. 2 on the website is not necessary for Windows 7): http://www.thesitewizard.com/apache/install-apache-on-vista.shtml

If you want to deploy your webpage on the internet, follow the instructions in the following link instead: http://httpd.apache.org/docs/2.2/platform/windows.html

3. Download the Apache module mod_wsgi for Windows and Apache 2.2 from here: http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2

4. Rename the downloaded file to “mod_wsgi.so”. Then place it in the “\Apache Software Foundation\Apache2.2\modules” folder of your Apache Server installation.

5. Open the conf\httpd.conf file of your Apache installation as an administrator. Then add the text “LoadModule wsgi_module modules/mod_wsgi.so” to the file at the place where the other modules are loaded (i.e. the place where you find many “LoadModule” statements). Also comment out “LoadModule dir_module modules/mod_dir.so” if you don’t want to use its functionality.

6. Restart the Appache server. If all is okay, you should see a line of the form: “[notice] Apache/2.2.22 (Win32) mod_wsgi/3.3 Python/2.7.3 configured — resuming normal operations” in the logs/error.log file of your Apache installation.

7. Copy the Django project’s root folder into the folder you want to use for your server’s files, e.g. “C:\server_files”. Note that Apache runs as a special user on your computer (defined in the conf\httpd.conf file, search for “User” or “Group”), and therefore cannot access your private folders, such as home, documents or desktop (see this presentation for more info). Because of this, always put the server files on a shared location, otherwise you will get a “403 Forbidden” error from the server.

8. At an empty spot in the conf\httpd.conf file of your Apache installation, paste the text below, and replace DJANGO_PROJECT_FOLDER with your Django project folder’s name (e.g. “myproject”), DJANGO_SETTINGS_FOLDER with the name of the Django project’s settings folder (this folder contains settings.py and wsgi.py, usually the name is the same as your project name e.g. “myproject”) and DJANGO_APP_FOLDER with the name of the Django app where you store static files (e.g. “myapp”), assuming you’ve chosen to store the static files within the apps. Also note that all folder-paths have to use UNIX-style “/” instead of Windows style “\”.

# Sets default charset to utf-8. You should make sure AddDefaultCharset is not already set in the conf\httpd.conf file.
AddDefaultCharset utf-8

# Change Python path used by the server.
WSGIPythonPath “C:/server_files/DJANGO_PROJECT_FOLDER”

# Make calls to http://localhost/ refer to the Python/WSGI-script located at the specified location.
WSGIScriptAlias / “C:/server_files/DJANGO_PROJECT_FOLDER/DJANGO_SETTINGS_FOLDER/wsgi.py”

# Make calls to http://localhost/static refer to the specified folder.
Alias /static “C:/server_files/DJANGO_PROJECT_FOLDER/DJANGO_APP_FOLDER/static”

# Make the file wsgi.py accessible through the server (everything is inaccessible by default).
<Directory “C:/server_files/DJANGO_PROJECT_FOLDER/DJANGO_SETTINGS_FOLDER”>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
DJANGO_PROJECT_FOLDER/DJANGO_APP_FOLDER
# Make the specified directory and its content accessible through the server (everything is inaccessible by default).
<Directory “C:/server_files/DJANGO_PROJECT_FOLDER/DJANGO_APP_FOLDER/static”>
Order deny,allow
Allow from all
</Directory>

9. Open the “\server_files\DJANGO_PROJECT_FOLDER\DJANGO_SETTINGS_FOLDER\settings.py” file of the Django project. Locate and update the database settings to match the database on the server. Then run the “manage.py syncdb” command from a terminal/command prompt located at the Django project’s root folder (where manage.py can be found) to set up all the necessary database structures on the server.

10. Restart the Apache server (this must be done every time you want the Apache server to use new or modified files).

11. Navigate to http://localhost/ and the website should load.

Tips and Tricks

>   If you run into trouble, this link may contain some solutions: http://pradyumnajoshi.wordpress.com/2009/06/09/setting-up-mod_wsgi-for-apache-and-django-on-windows/

and this link discusses another problem: http://serverfault.com/questions/313770/django-and-wsgi-and-apache2-site-is-perfect-in-test-server-wont-load-with-ap

5 comments

  1. I thinκ this is amoոg the most vital info for me.
    And i’m glad reading your article. But waոt to remarκ on few general things, The ѕite
    ѕtyle is great, the articleѕ is really great : D. Good
    job, cheers

  2. S Ved · · Reply

    Just what I was looking for.
    I did all that you mentioned, but got stuck in step 8. One of the lines:
    DJANGO_PROJECT_FOLDER/DJANGO_APP_FOLDER
    which is in between two blocks is causing Apache to crash when re-started. So, I disabled that line and restarted Apache. It starts no problem, but I get a 403 Forbidden when I try to access the app.
    Any pointers?

    1. Hi S Ved!

      “DJANGO_PROJECT_FOLDER/DJANGO_APP_FOLDER” should actually be replaced with the real path for those things on your server. E.g. “myproject/myapp”. Did you try that?

  3. gaurhari · · Reply

    I am facing same issue as ved said

    1. Hi gaurhari.

      Did you replace the “DJANGO_PROJECT_FOLDER/DJANGO_APP_FOLDER” with the actual path on your server?

Leave a Reply to S Ved Cancel reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: