Static files 404 in production - django

None of my static files are loading after deploying my app. When I check in the dev tools they're all 404.
I ran python3 manage.py collectstatic
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/srv/example/static/'
My static folder has the following permissions and ownership:
drwxr-xr-x 8 root root 4096 Nov 17 23:28 static
My /etc/apache2/sites-enabled/example.conf:
<VirtualHost *:80>
ErrorLog ${APACHE_LOG_DIR}/example-error.log
CustomLog ${APACHE_LOG_DIR}/example-access.log combined
WSGIDaemonProcess example processes=2 threads=25 python-ho>
WSGIProcessGroup example
WSGIScriptAlias / /srv/example/example/wsgi.py
Alias /robots.txt /srv/example/static/robots.txt
Alias /favicon.ico /srv/example/static/favicon.ico
Alias /static /srv/example/static/
Alias /media /srv/example/media/
<Directory /srv/example/example>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /srv/example/static>
Require all granted
</Directory>
<Directory /srv/example/media>
Require all granted
</Directory>
</VirtualHost>

did you check after "collectstatic" that the files are in the folder /static/?
I am not sure if it matters ... but my Alias has no slash at the end:
Alias /static /.../...../static

Related

Django: How to deploy static files on an apache server in production

Static files are not loading in production of my Django project. I am using mod-wsgi to serve the static files.
Static files were serving fine in development (when DEBUG=True), but now I get 404 errors when the static files are trying to be called.
My file structure is:
https://imgur.com/a/FuFZSSh
/etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
ServerName <ip address>
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Alias /robots.txt /var/www/mysite/static/robots.txt
Alias /favicon.ico /var/www/mysite/static/favicon.ico
Alias /static/ /var/www/mysite/static/
Alias /media/ /var/www/mysite/media/
<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite/static>
Require all granted
</Directory>
<Directory /var/www/mysite/media>
Require all granted
</Directory>
</VirtualHost>
settings.py
STATIC_ROOT = '/var/www/mysite/static_root/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_URL = '/static/'
MEDIA_ROOT = '/var/www/mysite/media/'
MEDIA_URL = '/media/'
Any help would be greatly appreciated :)
ol' reliable: DigitalOcean
how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-16-04
Apache should be looking at your STATIC_ROOT directory, after you've run collectstatic.
<Directory /var/www/mysite/static_root>
Require all granted
</Directory>

Loading Django static files via Apache

I know there are dozens of this questions around and I think I've read all of them, but couldn't find my problem.
I've created a file named /etc/httpd/conf.d/django.conf and this is what I've written in it:
<VirtualHost *:8000>
Alias /media/ /var/www/html/igame/media/
Alias /static/ /var/www/html/igame/static/
<Directory /var/www/html/igame/static>
Require all granted
</Directory>
<Directory /var/www/html/igame/media>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/html/igame/igame/wsgi.py
<Directory /var/www/html/igame/igame>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess igame python-path=/lib/python3.6/site-packages
WSGIProcessGroup igame
</VirtualHost>
And this is my static settings in settings.py:
DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
PROJECT_DIR = os.path.dirname(__file__)
I've ran python3.6 manage.py collectstatic.
But when I run the project by python3.6 manage.py runserver 0.0.0.0:8000, the statics files does not load on my project.
I use python 3.6 plus django 2.1and Apache 5.4.
What am I missing here?
UPDATE
I installed mod_wsgi and added these lines to httpd.conf as this documents suggests:
WSGIScriptAlias / /var/www/html/igame/igame/wsgi.py
<Directory /var/www/html/igame/igame>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIPythonHome /var/www/html/djangoenv
WSGIPythonPath /var/www/html/igame
WSGIDaemonProcess igame python-home=/var/www/html/djangoenv python-path=/var/www/html/igame
WSGIProcessGroup igame
And removed this lines from django.conf:
WSGIDaemonProcess igame python-home=/var/www/html/djangoenv python-path=/var/www/html/igame
WSGIProcessGroup igame
I normally do this with nginx and now I'm really confused how to make it work with Apache.

Django - Apache : How to change static repertory for upload files

I installed Django on a VPS and Apache2,
but when I want to upload a file (Avatar with userena)
I have this error Permissions refused: 'var/www/static'
but my css files and others have Loaded (file in the static directory of my project),
And the directory of my django project is in /home/user/... and not in /var/www, I tried a "DocumentRoot" change in `etc/apache2/site-available/000-Default.conf..'But it does not work
My conf in 000-default.conf :
```
ServerName www.site_example.com
ServerAdmin webmaster#site_example
DocumentRoot home/ubuntu/my_site/site_example/
Alias /static /home/ubuntu/my_site/site_example/static
<Directory /home/ubuntu/my_site/site_example/static >
Require all granted
</Directory>
<Directory "/home/ubuntu/my_site/site_example">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_example python-path=/home/ubuntu/my_site/site_example python-home=/home/ubuntu/web_sites/my_site__venv
WSGIProcessGroup site_example
WSGIScriptAlias / /home/ubuntu/my_site/site_example/wsgi.py
```

css style not working in django production enviornment?

I am beginner in Django. When i hosted a django application with mod_wsgi in Apache2.4. I got a 404 error for my base.css. Any help should be appreciated.
<IfModule mod_alias.c>
Alias /cgi-bin /home/vetconnect/public_html/cgi-bin`
Alias /static/ /home/vetconnect/djangosites/vetconnect/static/
</IfModule>
<IfModule mod_wsgi.c>
WSGIScriptAlias / /home/vetconnect/djangosites/vetconnect.wsgi
WSGIDaemonProcess vetconnect processes=7 threads=1 display-name=%{GROUP}
WSGIProcessGroup vetconnect WSGIApplicationGroup %{GLOBAL}
</IfModule>
According official django documentation to manage static files you have check this points:
django.contrib.staticfiles is included in your INSTALLED_APPS.
define STATIC_URL in settings.py, for example STATIC_URL = '/static/'
Use {% load static %} in templates.
you can define a list of directories STATICFILES_DIRS
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
]
Finally, you need to set the script alias so that Apache will pass requests for the root domain to the wsgi.py file and server static and media files. Here is the link how to do this with full description
<VirtualHost *:80>
. . .
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>

apache + mod_wsgi + django +askbot

error:File does not exist:
/home/WorkSpace/fenxiang/site_media/media/photos/3.jpg the real
address 3.jpg is at
/home/WorkSpace/fenxiang/askbot/upfiles/photos/3.jpg
All apalication is run well as python manage.py runserver:
centos6.2 python2.7(/opt/python2.7.2)
hpptd.conf
<VirtualHost *:80>
ServerAdmin matt#360yuer.com
ServerName www.360yuer.com
DocumentRoot /home/WorkSpace/fenxiang
WSGIScriptAlias / /home/WorkSpace/fenxiang/apache/fenxiang.wsgi
Alias /site_media/ /home/WorkSpace/fenxiang/site_media/
Alias /upfiles/ /home/WorkSpace/fenxiang/askbot/upfiles/
Alias /static/ /home/WorkSpace/fenxiang/static/
AddType text/html .py
<Directory /home/WorkSpace/fenxiang/apache >
Order allow,deny
Allow from all
</Directory>
<Directory /home/WorkSpace/fenxiang/site_media>
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>
<Directory /home/WorkSpace/fenxiang/askbot/upfiles>
Order deny,allow
Allow from all
</Directory>
<Directory /home/WorkSpace/fenxiang/static>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I am a newer, and I do not know how to fix it ,
the current problem is can not find the /askbot/upfiles/3.jpg, and some css in /static/css
settings.py
STATICFILES_DIRS = (os.path.join(ASKBOT_ROOT, 'skins'), os.path.join(PROJECT_ROOT, "static"), ASKBOT_EXTRA_SKINS_DIR)
RECAPTCHA_USE_SSL = True
Instead of:
/askbot/upfiles/3.jpg
you should be using URL with:
/upfiles/3.jpg
in it. That is what you have Alias set up for.
As for CSS files, are they actually in the directory:
/home/WorkSpace/fenxiang/static/css/