我已按照本教程部署我的django项目:https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-debian-8
为什么无法从apache加载静态文件?
我正在使用Debian9,Python3.5和Django 1.11
这是我的虚拟主机配置:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/alex/djangoproject/static
<Directory /home/alex/djangoproject/static>
Require all granted
</Directory>
<Directory /home/alex/djangoproject/cpanel>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess djangoproject python-home=/home/alex/djangoproject/djangoprojectenv python-path=/home/alex/djangoproject
WSGIProcessGroup djangoproject
WSGIScriptAlias / /home/alex/djangoproject/cpanel/wsgi.py
</VirtualHost>
`
来自error.log的apache错误:
AH01630: client denied by server configuration /home/alex/djangoproject/static
我的settings.py配置与static foder相关
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
答案 0 :(得分:0)
通过在静态之前添加django项目名称目录解决了这个问题 所以这个:
Alias /static /home/alex/djangoproject/static
<Directory /home/alex/djangoproject/static>
Require all granted
</Directory>
将成为这个:
Alias /static /home/alex/djangoproject/"django-project-name(in mycase: cpanel)"/static
<Directory /home/alex/djangoproject/"django-project-name(in mycase: cpanel)"/static>
Require all granted
</Directory>
执行./migrate.py collectstatic,重启apache并顺利运行。