今天我在部署Django项目和Apache2 mod_wsgi时遇到了一些问题。
让我们澄清一些事实: 1. 这不是我第一次部署 2. 网站正在使用mod_wsgi响应 3. “static”文件夹包括文件和文件夹,权限为777
我的虚拟主机配置如下:
让我们澄清一些事实: 1. 这不是我第一次部署 2. 网站正在使用mod_wsgi响应 3. “static”文件夹包括文件和文件夹,权限为777
我的虚拟主机配置如下:
<VirtualHost *:80>
ServerName myproject.com
AddDefaultCharset UTF-8
ServerAdmin webmaster@myproject.com
ServerAlias www.myproject.com
DocumentRoot /home/ilyas/open.tm/bin/myproject/
WSGIScriptAlias / /home/ilyas/open.tm/bin/myproject/myproject/index.wsgi
<Directory /home/ilyas/open.tm/bin/myproject>
Order deny,allow
Require all granted
</Directory>
Alias /static/ /home/ilyas/open.tm/bin/myproject/static/
<Directory /home/ilyas/open.tm/bin/myproject/static>
Order allow,deny
Require all granted
</Directory>
</VirtualHost>
我的index.wsgi文件看起来像这样:
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/.virtualenvs/open.tm/local/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/home/ilyas/open.tm/bin')
sys.path.append('/home/ilyas/open.tm/bin/myproject')
sys.path.append('/home/ilyas/open.tm/bin/myproject/static')
sys.path.append('/home/ilyas/open.tm/bin/myproject/myproject')
# Activate your virtual env
activate_env=("/home/ilyas/open.tm/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
application = get_wsgi_application()
最后是我的settings.py文件部分
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
'static', '/home/ilyas/open.tm/bin/myproject/static/',
'media', '/home/ilyas/open.tm/bin/myproject/media/',
)
MEDIA_ROOT = os.path.join(BASE_DIR, "static/")
网站运行良好,我可以浏览页面,访问管理后台,也可以通过后台添加记录... 但是静态文件无法加载,提示"您没有权限访问此服务器上的 /static/image.png。"
Django = 1.9.2 Apache = 2.4.7