I think this is common problem, but non of the solutions I tried work.
The code from apache conf:
<VirtualHost *:80>
ServerName xxxx
ServerAdmin xxxx
DocumentRoot /home/matousc/apps/iacah
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/matousc/apps/iacah/www/static
<Directory /home/matousc/apps/iacah/www/static>
Require all granted
Allow from all
</Directory>
<Directory /home/matousc/apps/iacah/app/mainapp>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess iacah python-path=/home/matousc/apps/iacah/app python-home=/home/matousc/apps/appenv
WSGIProcessGroup iacah
WSGIScriptAlias / /home/matousc/apps/iacah/app/mainapp/wsgi.py
</VirtualHost>
I can access the page via Internet, so I am sure that I am editing the right apache conf file. However the static files are not loaded.
The static files are not downloaded with 403 error. I noticed that if I change the line:
Alias /static/ /home/matousc/apps/iacah/www/static
to (removed slash at the end of static
:
Alias /static /home/matousc/apps/iacah/www/static
then I will get 404 error. In tutorials I saw both options, so I am little bit confused why it can play a role.
The owner of the /www/
folder is www-data
(I am using ubuntu 18):
drwxrwx--x 3 www-data www-data 4096 Sep 21 10:14 .
drwxr-xr-x 8 matousc matousc 4096 Sep 21 10:14 ..
drwxrwxrwx 12 www-data www-data 4096 Sep 21 10:14 static
I use this machine as a multihost, and I have there one other static website, that works (the files are served correctly)
<VirtualHost *:80>
ServerName xxxxx
ServerAdmin xxxx
DocumentRoot /home/matousc/apps/placeholder
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/matousc/apps/placeholder>
Require all granted
Options +Indexes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
In Django I use (I hope recommended) settings:
STATIC_URL = "/static/"
if production_machine:
level_up = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(level_up, "www", "static")
STATICFILES_DIRS = (
STATIC_ROOT,
)
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
(the production_machine
should be True).
Any ideas what else I can try?
2
Answers
I am not really sure what was the major problem, however problem magically disappear after another hour of pure witchcraft. For other people that stuck in the same situation:
make sure that you are editing the correct apache conf file
as @Du D. suggest make sure that the static files are collected correctly. There is multiple issues that can occur (it does not grab all your static folders etc...). Search for problem in your
settings.py
check that you really know who is the apache user on your machine
make sure that the folder with collected static files is owned by apache user and has the rwx access rights recursively!
play a lot with slashes in apache conf file. Seems that some combinations of slash usage are supperior to others. In my case
/static/
was the incorrect one (even though it throw only 403 instead of 404 before). My working example work only with/static
.I probably miss some steps because I was lucky and not encounter all possible problems, so feel free to edit/extend my answer.
Have you run manage.py collectstatic? In production, you need to call this for django to copy from ur dev code to the STATIC_ROOT location.
https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#collectstatic