Deploying Django to Apache Web Server

  
in Windows 7 In essence, Django is just a set of libraries written in Python. Using the Django development site is to write Python code using these libraries. So the key to learning Django is learning how to do Python programming and understanding how the Django class library works.
If you have Python development experience, there should be no problems in the learning process. Basically, Django code does not use some black magic (such as deceptive behavior in the code, an implementation explanation or understanding is very difficult ). For you, learning Django is about learning her naming conventions and APIs.
Configuration ideas
1, configure Apache httpd.conf file
2, configure django related configuration files
configuration process
In fact, the configuration takes effect for different environments have different details need to be processed, online The solution (including this one) is not necessarily universal, but it is effective under certain circumstances, but the general idea is to configure the two configuration files mentioned above.
The two common ways to deploy a django project are to use mod_python and mod_wsgi. Here I am using mod_wsgi.
1, first go online to download a thing called this: mod_wsgi-3.4.ap22.win32-py2.7, which has a file is mod_wsgi.so, and then copy this to the modules directory of the apache installation directory ( The default installation directory is: C:Program Files (x86) Apache Software Foundation Apache2.2modules)
The following two configurations involve errors in the path. If it is an absolute path, check if it is correct.
2, create two new files in the Django project directory:
django.wsgi:
#coding=utf-8
import os
import sys
import django.core. Handlers.wsgi
os.environ["DJANGO_SETTINGS_MODULE"] = "appops.settings"
app_apth = "D:/OPSAPP/appops"
sys.path.append(app_apth)
application = django.core.handlers.wsgi.WSGIHandler()
apache_django_wsgi.conf:
#Alias ​​/D:/OPSAPP/appops
Alias ​​/favicon.jpg D:/OPSAPP/appops/static/images/favicon .jpg
#WSGIScriptAlias ​​/api "D:/OPSAPP/appops/appapi/handler.py" #Note, if you have written it in httpd.conf, you don't need to write it here.
WSGIScriptAlias ​​/"D:/OPSAPP/django.wsgi"
WSGIPassAuthorization On
<Directory "D:/OPSAPP/appops/appops">
Order Deny,Allow
Allow from all
</Directory>
Alias ​​/static/D:/OPSAPP/appops/static/
<Directory D:/OPSAPP/appops/static/>
Order deny, Allow
Allow from all
IndexOptions FancyIndexing
</Directory>
<Directory D:/OPSAPP/appops/>
Order deny,allow
Allow from all
IndexOptions FancyIndexing
</Directory>
<Directory "D:/OPSAPP">
Allow from all
</Directory>
The directory structure is as follows:
3, edit the apache configuration file httpd.conf (C: Program Files (x86) Apache Software Foundation Apache2.2confhttpd.conf)
Add a sentence:
LoadModule wsgi_module modules /mod_wsgi.so
file end new Add the following configuration:
Alias ​​/static D:/OPSAPP/appops/static# This is to access the static file via url
<Location "/static/">
SetHandler None< Br>< ;/Location><br>
<VirtualHost *:80>#Configure Virtual Directory
ServerName app.ops.test.com
#ServerName 192.168.18.74
DocumentRoot D:/OPSAPP< Br>WSGIScriptAlias ​​/D:/OPSAPP/django.wsgi
<Directory />
Order deny,allow
Allow from all
</Directory>
<Directory /apache> ;
Allow from all
</Directory>
</VirtualHost>
<Directory "D:/OPSAPP/appops/static/"> #This must be needed, otherwise Web page style error, css does not work
Order Deny, Allow
Allow from all
</Directory>
Restart apache service is basically OK.
Common Errors
When the access fails, the error will be recorded in the apache log (C:Program Files (x86) Apache Software Foundation Apache2.2logs),
1, static resources can not be accessed, such as css style confusion, etc. Need to add configuration in the httpd.conf file:
<Directory D:/OPSAPP/appops/static/>
Order deny,allow
Allow from all
IndexOptions FancyIndexing
< /Directory>
2, there is a mistake in the module not found, such as no module named XXX, etc. There are two main reasons:
1), the path is wrong
2), the file is named with Django or python Internal module conflicts

Copyright © Windows knowledge All Rights Reserved