(亲手作过!!!)python
一,个人python、django、apatch2版本:apache
python:python -Vdjango
2.7.3ubuntu
django:python -c "import django; print(django.VERSION)"浏览器
(1, 9, 4, 'final', 0)app
apache2:/usr/sbin/apachectl -vdom
Server version: Apache/2.2.22 (Ubuntu)
Server built: Jul 24 2015 17:25:52网站
二,安装python 和django。ui
三,安装apache2 和 wsgi。spa
sudo apt-get insall apache2
sudo apt-get install libapache2-mod-wsgi
若是以前安装配置过apache2的而且配置很乱,没法修复,建议仍是彻底卸载以后再安装。彻底卸载的命令:
sudo apt-get --purge remove apache*
sudo apt-get --purge remove apache-common
安装完之后,去 /etc/apache2/ 下的 mods-available 和mods-enabled 中查看是否都存在 wsgi.conf 和wsgi.load 两个文件。有则说明wsgi模块加载到apache2成功。
四,配置apache 和 wsgi
(PS 假设你是在目录/var/www/mysite 下面 django-admin.py startproject testproject )
我项目的tree:
一、apache的配置:
在/etc/apache2/site-available中新建一个文件 testdjango.conf (PS中文注释都删除)
<VirtualHost *:80> ServerName testdjango.com #ServerName这个无所谓,只要在host中把你的ip地址映射到这个上面就能够了。不想改host最简单的方法就是用 sudo a2dissite 000-default.conf 等虚拟主机给disable掉,只留 testdjango.conf。(PS.site-enabled中的文件是link site-availabel下的文件,若是存在link文件则是enable的,能够根据这个来查看) DocumentRoot /var/www/mysite/testproject #这一条是指向网站的根目录 <Directory /var/www/mysite/testproject> Order allow,deny Allow from all </Directory> WSGIDaemonProcess testdjango.com processes=2 threads=15 display-name=%{GROUP} WSGIProcessGroup testdjango.com #对应前面的ServerName
WSGIScriptAlias / /var/www/mysite/testproject/apache/django.wsgi #将后面的那个路径指向网站的根目录。第一个“/”表示外部访问时的网站根目录,当有新的requset的时候,apache从这里开始解析。
</VirtualHost>
二、wsgi的配置
在/var/www/mysite/testproject/下新建dir:apache 。在./apache下新建文件django.wsgi。(文件名对应前面的WSGIScriptAlias第二个路径下的文件名)
#coding:utf-8
import os import sys path = '/var/www/mysite' if path not in sys.path: sys.path.insert(0, '/var/www/mysite/testproject') #将模块路径加到当前模块扫描的路径。或sys.path.apend('/var/www/mysite/testproject')
os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings' #新建一个环境变量DJANGO_SETTINGS_MODULE,目的是指向django工程的settings.py,这里
#import django.core.handlers.wsgi #old version use
#application = django.core.handlers.wsgi.WSGIHandler()
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
五,验证
sudo a2ensite testdjango.conf #enable testjango.conf
sudo service apache2 reload (PS 若是reload时出现 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName。虽然不影响但有个warning让人不爽,能够在/etc/apatch2/httpd.conf 中加入ServerName localhost)
最后浏览器 http://127.0.0.1或localhost或你ubunt的IP 就能够看见django的初始页面了。
其它机器的浏览器也能够经过你的ubuntu的IP来查看你django的项目的页面。
六,常见错误及log:
apache2的error log文件:/var/log/apache2/error.log
错误1:Internal Server Error 以下图
应该是django.wsgi里用了我注释的 django.core.handlers.wsgi 这个方法。用下面的那个get_wsgi_application 就没问题了。