djangoapachegunicorn

Deploy Django static files with Apache/Gunicorn


I am using Apache2 with Gunicorn/Django to deploy my app. However my app doesn't display the static files like CSS sheets etc.

I read many topics but I think I need help because I probably missed something...

Setting.py

ALLOWED_HOSTS = ['localhost']

STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )
STATIC_ROOT = '/var/www/media/myapp/static/'

Apache2 VHost

<VirtualHost *:80>
    ServerName myapp.fr
    ServerAlias www.myapp.fr myapp.fr

    DocumentRoot /home/django-project/myapp/

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass / http://localhost:9000/

    ProxyPass /static/ !
    Alias /static/ /var/www/media/myapp/static/

    <Directory /home/django-project/myapp/>
        Order deny,allow
        Allow from all
        Options -Indexes
    </Directory>

</VirtualHost>

Solution

  • I think the exclusion for for ProxyPass /static/ ! should come before ProxyPass /

    ProxyPass /static/ !
    ProxyPass / http://localhost:9000/
    Alias /static/ /var/www/media/myapp/static/
    

    Otherwise the static request will be sent to gunicorn, which will return a 404.

    The ProxyPass documentation says:

    The configured ProxyPass and ProxyPassMatch rules are checked in the order of configuration. The first rule that matches wins.