4
votes

I'm trying to deploy Django to apache but can't get it to serve my static admin files. It seems to be looking for them under /var/www/static and I can't seem to be able to change that.

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. It's the static files for the admin that aren't served.

Using 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.

The apache error log says this

[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

<VirtualHost *:80>
ServerAdmin [email protected]

    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.

From my settings.py

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

Nothing added to STATICFILES_DIRS.

This is added to my 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>
5

5 Answers

7
votes

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.

Here's the fixes to the apache config:

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.

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). A Django reinstall fixed it and it worked fine now.

1
votes

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. 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.

1
votes

Just like Blazor I had to use a small variation of the original. My setup includes graphite-web, which is using django admin. I'm just posting it for reference.

My apache's virtual host:

<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.

1
votes

Try using next

python manage.py collectstatic

The staticfiles app - Django documentation

0
votes

I got similar problem when running Django + Apache, the Django admin site misses all styling. This is how I solved it:

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