stay Django Development environment of , We simply pass through “Python manage.py runserver 0.0.0.0:8000” You can start a simple HTTP The server Conduct Django Development of . When the project development is completed and released , This simple application The server Can't meet the demand . A better solution at this time is to Django Application integration into Apache.
Django A lot of work has been done for us , So will Django Integrated into the Apache It's a very simple thing .Django Integrated into the Apache There are two ways :python_mod and wsgi, The latter is more stable than the former , So here we pass wsgi The way to integrate .
-------------------------------- Split line --------------------------------
First introduce my environment :
0.CentOS X64
1.Apache 2.2.3
2.Django 1.6.1
The first step of integration : install mod_wsgi
yum insall python26-mod_wsgi.x86_64
Check after installation Apache Catalog /etc/httpd/conf.d/ There will be python26-mod_wsgi.conf, It has been automatically loaded for us mod_wsgi.so Configuration of :
################################################################################# # Do not enable mod_python and mod_wsgi in the same apache process. ################################################################################# #
# NOTE: By default python26-mod_python with not load if mod_wsgi is installed # and enabled. Only load if mod_python and mod_wsgi are not already loaded.
<IfModule !python_module> <IfModule !wsgi_module> LoadModule wsgi_module modules/python26-mod_wsgi.so </IfModule> </IfModule>
The second step of integration : edit python26-mod_wsgi.conf
WSGIScriptAlias / "/search/lizhigang/mysite/mysite/wsgi.py" WSGIPythonPath /search/lizhigang/mysite
<Directory "/search/lizhigang/mysite/mysite"> <Files wsgi.py> Order deny,allow Allow from all </Files> </Directory>
There's a little bit of clarification here , my Django The project is located in “/search/lizhigang/mysite/”, Please replace according to the location of your project .
The third step of integration : restart Apache
service apache restart
Access your Django application ( No 8000 port , It is Apache The port of ), Under normal circumstances, you will be prompted at this time “500, The server internal error ”. Check /etc/httpd/logs/error_log, See if there are the following errors :
[Errno 13] Permission denied: '/var/www/.python-eggs'
It's time to edit “/search/lizhigang/mysite/mysite/wsgi.py”, Yes “PYTHON_EGG_CACHE” Set it up :
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") os.environ.setdefault("PYTHON_EGG_CACHE", "/tmp/.python-eggs")
from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
Refresh the browser , Now Django Successfully integrated into Apache 了 .
however , If your application uses static resources , Such as the picture 、css、js, these , You need to configure static 了 . We assume that these resources are located in mysite/static Catalog .
Integration step 4 : To configure static
open python26-mod_wsgi.conf, Join in static/ Access support :
Alias /static/ /search/lizhigang/mysite/static/ <Directory "/static/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory>
Try refreshing the website , Are all functions related to 8000 Port development is the same ? thus , We're done Django To Apache Integration of .