Gunicorn on Apache Server not serving static files in Django project: "Not Found" error for /static/
I’m running a Django project using Gunicorn and Apache with mod_wsgi, but Gunicorn is not serving the static files correctly. When I try to load my page, I get errors like:
Not Found: /static/css/style.css Not Found: /static/js/app.js
In browser console I get
Refused to apply style from 'http://localhost/static/css/style.css' because its MIME type
('text/html') is not a supported stylesheet MIME type, and strict MIME checking is
enabled.
Additionally, when I try to access the static files directly (e.g., http://localhost:9090/static/css/style.css), I get a 404 error. The static files are correctly configured in the Django settings, but Gunicorn is not able to locate them.
Environment: Django version: 5.1 Gunicorn version: 20.x.x Apache version: 2.4.x mod_wsgi version: 4.9.0 Operating System: Ubuntu 20.04 Python version: 3.10 Running inside a virtual environment
Django Static Configuration (settings.py):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'app/static')]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'userentry', //my django app
]
Current Setup: Apache is proxying requests to Gunicorn. Static files are collected in the STATIC_ROOT directory using python manage.py collectstatic. I have confirmed that the static files are located at /app/static/ on the server.
My apache virtualhost:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName localhost
ProxyPass / http://127.0.0.1:9090/
ProxyPassReverse / http://127.0.0.1:9090/
# Point WSGI to the proper directories
WSGIDaemonProcess malwarepurge python-home=/app/venv python-path=/app/malwarepurge
WSGIProcessGroup malwarepurge
WSGIScriptAlias /app /app/malwarepurge/wsgi.py
Alias /static/ /app/static/
# Allow access to the necessary app files (including other directories in /app)
<Directory /app>
AllowOverride All
Require all granted
</Directory>
# Specifically grant access to the malwarepurge folder for WSGI
<Directory /app/malwarepurge>
AllowOverride All
Require all granted
</Directory>
<Directory /app/static>
Require all granted
</Directory>
<Directory /app/userentry/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Steps Taken: I have added STATIC_URL, STATICFILES_DIRS, and STATIC_ROOT in the Django settings. I’ve run python manage.py collectstatic to gather the static files in the specified directory. I’ve configured Apache to use mod_wsgi for serving the Django app. Gunicorn is running but is unable to serve static files directly. Apache should be serving the static files, but Gunicorn still reports a "Not Found" error when trying to access them.
What I’ve Tried: Double-checked the STATIC_URL, STATIC_ROOT, and STATICFILES_DIRS settings. Ensured the static files are correctly located in /app/static/. Tried restarting both Apache and Gunicorn after making configuration changes. Verified that the paths to static files are correct and that Gunicorn is running on the correct IP/port. Checked that mod_wsgi is properly installed and enabled.