How to setup Apache + Python + Django + mod_wsgi

Apache + Python + Django + mod_wsgi Installation Guideline

 

1.  Install Apache

Install the apache follow the instruction.

 

2.  Install Python

Add “D:/software/Python27” and “D:/software/Python27/Scripts” to %Path%

Open “cmd”, and type “python”, you will see below detail:

D:/djworkspace>python

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>>

 

3.  Install Django

Unpackage the Django-1.2.3, then open “cmd”, and type “python setup.py install” to finish the installation.

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import django

>>> django.VERSION

(1, 2, 3, 'final', 0)

 

4.  Setup project

 

 

5.  Configure httpd.conf of Apache

Add below segment to httpd.conf to include external httpd.conf for website:

# Django workspace

Include D://djworkspace//httpd.conf

 

6.  Configure httpd.conf of webserver

Add below segment to httpd.conf to setup the environment of webserver:

<VirtualHost *:80>

    #ServerName www.example.com

    #ServerAlias example.com

    #ServerAdmin [email protected]

    DocumentRoot D://djworkspace//mycompany

    <Directory D://djworkspace//mycompany>

                   Order allow,deny

                   Allow from all

    </Directory>

    WSGIScriptAlias / D://djworkspace//mycompany//django.wsgi

</VirtualHost>

 

If want to host static media by apache, please add below segment under the DocumentRoot:

# Static resource

Alias /robots.txt D://djworkspace//resources//robots.txt

Alias /favicon.ico D://djworkspace//resources//favicon.ico

AliasMatch /([^/]*/.css) D://djworkspace//resources//css//$1

Alias /resources/ D://djworkspace//resources//

<Directory D://djworkspace//resources//>

Order deny,allow

Allow from all

</Directory>

 

7.  Configure mod_wsgi

Create a new file named “django.wsgi” to website, and add below segment to this file:

import os

import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'mycompany.settings'

sys.path.append('D:/djworkspace')

sys.path.append('D:/djworkspace/mycompany')

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

 

8.  Run website

Restart the apache server, then visit your website.

你可能感兴趣的:(How to setup Apache + Python + Django + mod_wsgi)