在apache上部署django webserver

时间:2015-02-09 07:13:45

标签: django apache wsgi

我使用以下方式测试了我的博客网站:

 python manage.py runserver

一切都是正确的。现在我想在apache上部署我的博客网站。但是我无法正确配置django的apache。基本上,我的博客结构如下:

├── blog
│   ├── __init__.py
│   ├── models.py
│   ├── static
│   │   └── blog
│   │       ├── css
│   │       ├── images
│   │       └── js
│   ├── templates
│   │   └── blog
│   │       ├── base.html
│   │       ├── index.html
│   ├── templatetags
│   │   ├── custom_filter.py
│   │   ├── __init__.py
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
├── blogC
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── manage.py
└── usermanage
    ├── admin.py
    ├── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

我已经安装了apache,mod_wsgi和数据库。我的问题是django中间件会自动查找静态文件。我应该在httpd.conf中添加静态文件的路径吗?我该如何编写配置文件?我按照django官方网站上的说明进行操作。但事实证明apache服务无法重启,所以我必须配置错误。

更新error.log的最后几行:

[Tue Feb 10 10:02:08.796042 2015] [core:error] [pid 6610] (13)Permission denied: [client 113.240.234.213:14433] AH00035: access to /phppath/php5 denied (filesystem path '/home/ec2-user') because search permissions are missing on a component of the path
[Tue Feb 10 10:02:11.700382 2015] [core:error] [pid 5220] (13)Permission denied: [client 113.240.234.213:15162] AH00035: access to /local-bin/php denied (filesystem path '/home/ec2-user') because search permissions are missing on a component of the path
[Tue Feb 10 10:02:12.407788 2015] [core:error] [pid 5257] (13)Permission denied: [client 113.240.234.213:15339] AH00035: access to /local-bin/php5 denied (filesystem path '/home/ec2-user') because search permissions are missing on a component of the path
[Tue Feb 10 10:02:13.118587 2015] [core:error] [pid 5221] (13)Permission denied: [client 113.240.234.213:15501] AH00035: access to / denied (filesystem path '/home/ec2-user') because search permissions are missing on a component of the path
[Tue Feb 10 11:14:12.291824 2015] [core:error] [pid 5218] (13)Permission denied: [client 205.145.18.5:47369] AH00035: access to / denied (filesystem path '/home/ec2-user') because search permissions are missing on a component of the path
[Tue Feb 10 11:14:14.319037 2015] [core:error] [pid 6308] (13)Permission denied: [client 205.145.18.5:57326] AH00035: access to / denied (filesystem path '/home/ec2-user') because search permissions are missing on a component of the path

1 个答案:

答案 0 :(得分:1)

这是一个可能的apache配置:

WSGIDaemonProcess blog processes=2 threads=15
WSGIScriptAlias / /path/to/project/blog/blogC/wsgi.py

<Directory /path/to/project/>
            WSGIProcessGroup blog
            WSGIApplicationGroup %{GLOBAL}
            Options All
            AllowOverride All
            Require all granted
</Directory>

Alias /media/ /path/to/project/media/
<Directory /path/to/project/media/>
            Options FollowSymLinks MultiViews
            Order deny,allow
            Allow from all
</Directory>

Alias /static/ /path/to/project/static/
<Directory /path/to/project/static/>
            Options FollowSymLinks MultiViews
            Order allow,deny
            Allow from all
</Directory>

还可以在项目根文件夹中创建staticmedia个文件夹,并将其设置为settings.py

import os

BASE_PATH = os.path.join(os.path.dirname(__file__), '..')
MEDIA_ROOT = os.path.join(BASE_PATH, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_PATH, 'static/')
STATIC_URL = '/static/'

然后将所有静态文件收集到提供的目录中(这会将所有应用程序中的静态文件链接到一个位置):

./manage.py collectstatic --link

还要确保apache具有写入媒体文件夹的权限,因此可以上传文件:

sudo chown -R www-data:www-data /path/to/project/media

现在您必须确保正确配置wsgi.py

import os, sys

## apache/mod_wsgi cannot find the path without it!
path = os.path.split(os.path.dirname(__file__))[0]
if path not in sys.path:
    sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blogC.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

应该是它。