简体   繁体   English

无法让Apache提供django管理静态文件

[英]Can't get Apache to serve django admin static files

I'm trying to deploy Django to apache but can't get it to serve my static admin files. 我正在尝试将Django部署到apache但无法让它为我的静态管理文件提供服务。 It seems to be looking for them under /var/www/static and I can't seem to be able to change that. 它似乎是在/ var / www / static下寻找它们,我似乎无法改变它。

The admin site seem to be working except for styling. 除了样式之外,管理站点似乎正在工作。 I get a title and a log in form. 我得到一个标题和一个登录表单。 My django app is working too. 我的django应用程序也在运行。 It's the static files for the admin that aren't served. 这是管理员未提供的静态文件。

Using Django 1.4.1. 使用Django 1.4.1。

The files are under /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static and linked to from /home/dutt/vaccapp/backend/static/admin. 这些文件位于/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static下,并链接到/ home / dutt / vaccapp / backend / static / admin。

The apache error log says this apache错误日志说明了这一点

[Sun Sep 30 10:57:20 2012] [error] [client 192.168.1.10] File does not exist: /var/www/home, referer: http://dathui.example.com/vaccapp/admin/
[Sun Sep 30 10:57:20 2012] [error] [client 192.168.1.10] File does not exist: /var/www/home, referer: http://dathui.example.com/vaccapp/admin/

But I'm not sure how to change it. 但我不知道如何改变它。

In my django site config I have 在我的django网站配置中我有

<VirtualHost *:80>
ServerAdmin me@host.com

    ServerRoot "/home/dutt/vaccapp"
    DocumentRoot "/home/dutt/vaccapp"
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /home/dutt/vaccapp/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Alias /static/ "/home/dutt/vaccapp/backend/static/"
<Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static">
    Order allow,deny
    Options Indexes
    Allow from all
    IndexOptions FancyIndexing
</Directory>

ServerRoot is not set in apache2.conf. 未在apache2.conf中设置ServerRoot。

From my settings.py 来自我的settings.py

STATIC_ROOT = '/home/dutt/vaccapp/backend/'
STATIC_URL = '/static/'

Nothing added to STATICFILES_DIRS. 没有添加到STATICFILES_DIRS。

This is added to my apache2.conf 这被添加到我的apache2.conf中

WSGIScriptAlias /vaccapp /home/dutt/vaccapp/backend/wsgi.py
WSGIPythonPath /home/dutt/vaccapp

<Directory /home/dutt/vaccapp>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

ADMIN_MEDIA_PREFIX is set by default to /static/admin/ # Deprecated in Django 1.4 (now using STATIC_URL + 'admin/' . The result is the same. ADMIN_MEDIA_PREFIX默认设置为/static/admin/ #在Django 1.4中弃用(现在使用STATIC_URL + 'admin/' 。结果相同。

Here's the fixes to the apache config: 这是apache配置的修复:

Alias /static/admin "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static"
Alias /static "/home/dutt/vaccapp/backend/static"
<Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static">
    Order allow,deny
    Options Indexes
    Allow from all
    IndexOptions FancyIndexing
</Directory>

And the WsgiScriptAlias had to be moved from the main apache config into the VirtualHost. 并且必须将WsgiScriptAlias从主apache配置移动到VirtualHost中。

After a long discussion we found the problem was that Django did not install the admin static properly ... they were symlinked to eachother (very weird). 经过长时间的讨论,我们发现问题是Django没有正确安装admin静态......他们彼此符号链接(非常奇怪)。 A Django reinstall fixed it and it worked fine now. Django重新安装修复它现在工作正常。

Just like Blazor I had to use a small variation of the original. 就像Blazor一样,我不得不使用原版的一小部分。 My setup includes graphite-web, which is using django admin. 我的设置包括使用django admin的graphite-web。 I'm just posting it for reference. 我只是张贴它以供参考。

My apache's virtual host: 我的apache的虚拟主机:

<VirtualHost *:80>
    ServerName graphite.myhost.com
    Redirect permanent / https://graphite.myhost.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName graphite.myhost.com

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/graphite.cert
    SSLCertificateKeyFile /etc/apache2/ssl/ssl_graphite.key
    SSLStrictSNIVHostCheck on

    WSGIDaemonProcess _graphite processes=5 threads=5 display-name='%{GROUP}' inactivity-timeout=120 user=_graphite group=_graphite
    WSGIProcessGroup _graphite
    WSGIImportScript /usr/share/graphite-web/graphite.wsgi process-group=_graphite application-group=%{GLOBAL}
    WSGIScriptAlias / /usr/share/graphite-web/graphite.wsgi

    AliasMatch ^/admin/(.*)static/admin(.*)$ /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/$2
    <Directory "/usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/">
            Order allow,deny
            Allow from all
    </Directory>

    Alias /content/ /usr/share/graphite-web/static/
    <Location "/content/">
            SetHandler None
    </Location>

    <Location "/">
            Order allow,deny
            allow from all
            AuthType Basic
            AuthName "Restricted Zone"
            AuthBasicProvider wsgi
            WSGIAuthUserScript /var/www/django_auth.wsgi
            Require valid-user
    </Location>

    ErrorLog ${APACHE_LOG_DIR}/graphite-web_error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/graphite-web_access.log combined

</VirtualHost>

I also added STATIC_URL = 'static/' just to make sure I didn't have any issues with the regex. 我还添加了STATIC_URL = 'static/' ,以确保我对正则表达式没有任何问题。

Try using next 尝试使用下一个

python manage.py collectstatic

The staticfiles app - Django documentation staticfiles应用程序 - Django文档

My low reputation forces me to write a whole answer to add a small detail to Igor's answer. 我的低声望迫使我写一个完整的答案,为伊戈尔的答案添加一个小细节。

I simply added the apache config part to my configuration, but it was not sufficient. 我只是将apache配置部分添加到我的配置中,但这还不够。 I had to change: 我不得不改变:

"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static"

to

"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin"

both in the first Alias and in the <Directory> directive. 在第一个Alias<Directory>指令中。

I got similar problem when running Django + Apache, the Django admin site misses all styling. 我在运行Django + Apache时遇到了类似的问题,Django管理员网站错过了所有样式。 This is how I solved it: 这就是我解决它的方式:

apache conf: httpd-app.conf apache conf:httpd-app.conf

Alias /static "/...path/to/your/django.../site-packages/django/contrib/admin/static"
<Directory "/...path/to/your/django.../site-packages/django/contrib/admin/static">
    Require all granted
</Directory>

The /...path/to/your/django.../ above, you can find it by pip show django 上面的/...path/to/your/django.../ ,你可以通过pip show django找到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM