Django Apache上运行单个Django项目,mod_wsgi 配置(转载)

【Django】Apache上运行单个Django项目,mod_wsgi配置

1 安装环境

操作系统:Ubuntu 12.04 LTS 32 位(安装在VMware虚拟机中)

python 版本: 

Python 2.7.3

Django版本  

>>> django.VERSION
(1, 6, 2, 'final', 0)

mod_wsgi版本

 mod_wsgi3.x

 apache版本及查看命令

x@x:~/program$ /usr/sbin/apachectl -v    (版本查看命令)
/usr/sbin/apachectl: 87: ulimit: error setting limit (Operation not permitted)
Server version: Apache/2.2.22 (Ubuntu)
Server built:   Mar 19 2014 21:10:40

 

2 安装过程

安装apache2 

sudo apt-get install apache2

安装mod_wsgi

sudo apt-get install libapache2-mod-wsgi

配置工作

···新建Django项目

切换到工作目录(可自己定义别的)

cd /home/x/program/django/

在这儿个目录下创建Django项目

django-admin.py startproject mywork

项目的目录是(Django1.6 项目新建后,会有两层以项目名为名字的目录)

复制代码
x@x:~/program/django$ cd mywork/
x@x:~/program/django/mywork$ tree
.
├── apache    (刚创建是没有,自己后来新建)
│   └── django.wsgi    (一会自己创建)
├── manage.py
├── mywork
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── static    (一会儿自己创建)
复制代码

 

···配置apache2

① 修改httpd.conf 配置文件

sudo vim /etc/apache2/httpd.conf

添加以下代码

复制代码
#user defined configuration

#加载mod_wsgi模块
#LoadModule wsgi_module modules/mod_wsgi.so (这句不用加,若加上,系统会提示自动加载)
WSGIScriptAlias / /home/x/program/django/mywork/apache/django.wsgi
<Directory /home/x/program/django/mywork/ >
     Order deny,allow
     Allow from all
</Directory>

Alias /static/ /home/x/program/django/mywork/static/

<Directory /home/x/program/django/mywork/static>
    Order deny,allow
    Allow from all
</Directory>
复制代码

要注意文件路径,不要写错

②修改 envvars文件

sudo vim /etc/apache2/envvars

添加以下配置

export LANGUAGE=zh_CN:zh
export LANG=zh_CN.utf8

③ 在项目外层mywork目录下建立apache下新建django.wsgi(参考:上边新建Django项目中的目录结构,那个是已经新建好的结构)

向文件 django.wsgi 中添加内容

复制代码
import os, sys
sys.path.append('/home/x/program/django/mywork')

os.environ['DJANGO_SETTINGS_MODULE'] = 'mywork.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
#print >> sys.stderr, sys.path  
复制代码

要特别注意标绿的一行,不能写错,要不会报错:

复制代码
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1] mod_wsgi (pid=4524): Exception occurred processing WSGI script '/home/x/program/django/mywork/apache/django.wsgi'.
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1] Traceback (most recent call last):
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]     self.load_middleware()
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 46, in load_middleware
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]     self._setup(name)
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]     self._wrapped = Settings(settings_module)
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1]     % (self.SETTINGS_MODULE, e)
[Fri Apr 25 14:57:51 2014] [error] [client 127.0.0.1] ImportError: Could not import settings 'mywork.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mywork.settings
复制代码

提示找不到模块文件settings.py。这是因为在步骤③中:

sys.path.append('/home/x/program/django/mywork')

设置有误。参考文2

④ 在外层目录myword下 新建一个空的static文件(用来存放js css,目前是空的)

完成以后的目录是:

复制代码
── mywork
│   ├── apache
│   │   └── django.wsgi
│   ├── manage.py
│   ├── mywork
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   └── static
复制代码

⑤重启apache服务器

sudo service apache2 restart

⑥在地址栏打开localhost即可

说明配置成功。

若发生错误,可能会出现以下界面

这是要到Server的log中查看错误原因

查看错误日志文件

sudo vim /var/log/apache2/error.log

部分错误信息如下,根据具体情况,到google查找解决办法

复制代码
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1] mod_wsgi (pid=6569): Exception occurred processing WSGI script '/home/x/program/django/mywork/apache/django.wsgi'.
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1] Traceback (most recent call last):
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]     self.load_middleware()
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 46, in load_middleware
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]     self._setup(name)
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]     self._wrapped = Settings(settings_module)
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1]     % (self.SETTINGS_MODULE, e)
[Fri Apr 25 16:42:06 2014] [error] [client 127.0.0.1] ImportError: Could not import settings 'mywork.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mywork.settings
复制代码

3 参考文章

ubuntu下django+apache2+mod_wsgi快速部署

发生importError后参考这个文章

posted @ 2014-05-05 20:20  cnshen  阅读(289)  评论(0编辑  收藏  举报