步骤如下:
1: 环境安装
Fedora 20, Apache 2.4.9, Python 2.7.5, mod_wsgi 3.4, Django 1.5.5
开发环境使用了Eclipse+PyDev
1.1 yum install eclipse, 然后在Help-->Install new Software中 install Pydev, URL : http://pydev.org/updates
1.2 yum install django
1.3 yum install httpd,
1.4 yum install mod_wsgi
1.5 查看版本:
httpd -v to check apache version
>>>import django
>>>django.VERSION to check django version
在Apache /var/log/httpd/error_log中有版本信息:Apache/2.4.9 (Fedora) mod_wsgi/3.4 Python/2.7.5 configured
2: 开发一个Django Web
2.1 新建Python Django project, 路径为/home/xxx/workspace/First
2.2 添加一个View及修改URL
view.py:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
urls.py:
from django.conf.urls import patterns, url
from view import hello
urlpatterns = patterns('',
url(r'^hello/', hello),
)
2.3利用Django 附带的web测试
cd /home/xxx/workspace/First
python manage.py runserver 8000
在浏览器中打开http://localhost:8000/hello 可以看到结果:Hello world
3: 部署到Apache
打开Django文档: https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/modwsgi/
请注意选择Django 版本。 我的是1.5.5 所以我在该页面中选择的是1.5。按照文档建议,在/etc/httpd/conf/httpd.conf尾部加上下下面一段:
WSGIScriptAlias / /home/xxx/workspace/First/First/wsgi.py
WSGIPythonPath /home/xxx/workspace/First
<Directory /home/xxx/workspace/First/First>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
启动httpd服务 service httpd start
在浏览器中打开http://localhost/hello 出错,页面提示没有权限,error中信息为: AH00035: access to /hello denied (filesystem path '/home/xxx') because search permissions are missing on a component of the path
4: 排错
2个原因导致出现权限问题: 1:Selinux 2: Apache无权限访问目录/home/xxx/workspace/First
4.1 Selinux
两种途径解决: A: 更改/etc/sysconfig/selinux 把Selinux设置为disabled
B: 运行 setsebool -P httpd_read_user_content 1
setsebool -P httpd_enable_homedirs 1
4.2 更改httpd user/group
User apache ----> User xxx
Group apache ----> Group xxx
注意:上述过程多次出现xxx,实际为我的用户名。