css style not working in django production enviornment? - django

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>

Related

Static files 404 in production

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

Django Deployment 500 Internal Server Error - Apache2

I have been trying to figure this out for a while but I am not sure of what it's wrong. I have tried modifying the .conf file, uncomment the ServerName but nothing seems to work.
Please provide some insight.
django_project.conf
# ServerName 45.33.82.190
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/rodrez/PersonalPortfolio/static
<Directory /home/rodrez/PersonalPortfolio/static>
Require all granted
</Directory>
Alias /media /home/rodrez/PersonalPortfolio/media
<Directory /home/rodrez/PersonalPortfolio/media>
Require all granted
</Directory>
<Directory /home/rodrez/PersonalPortfolio/PersonalPortfolio>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/rodrez/PersonalPortfolio/PersonalPortfolio/wsgi.py
WSGIDaemonProcess Portfolio python-path=/home/rodrez/PersonalPortfolio python-home=/home/rodrez/PersonalPortfolio/venv
WSGIProcessGroup Portfolio
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
settings.py
DEBUG = False
ALLOWED_HOSTS = ["45.33.82.190"]
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
I was able to figure it out.
I used
sudo tail -100 /var/log/apache2/error.log
to check for the errors and found that my code to look for the key in another folder was not working properly.
Remember you sudo tail -100 /var/log/apache2/error.log to check for any apache errors.

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.

What am I missing here? Apache keeps serving default page

I have configured my Django site but Apache keeps serving the default apache's page. What am I missing here? My setup is Django 1.10 on CentOS server with python3.5.
Project structure
/home/sbsadmin/web/sbsportal
requirements.txt
src
manage.py
media
sbsportal
__init__.py
__pycache__
settings.py
urls.py
wsgi.py
static
templates
venv
/etc/httpd/conf.d/sbsportal.conf
LoadModule wsgi_module modules/mod_wsgi.so
<VirtualHost *:80>
ServerAdmin admin#email.com
ServerName sbsportal
ServerAlias sbsportal.com
DocumentRoot /home/sbsadmin/web/sbsportal/src/
WSGIDaemonProcess sbsportal python-path=/home/sbsadmin/web/sbsportal/src:/home/sbsadmin/web/sbsportal/venv/lib/python3.5/site-packages
WSGIApplicationGroup sbsportal
WSGIScriptAlias / /home/sbsadmin/web/sbsportal/src/sbsportal/wsgi.py process-group=djangoproj
<Directory /home/sbsadmin/web/sbsportal/src>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride None
Order allow,deny
Require all granted
</Directory>
<Directory /home/sbsadmin/web/sbsportal/src/sbsportal>
<Files wsgi.py>
Order deny,allow
Allow from all
Require all granted
</Files>
</Directory>
Alias /static /home/sbsadmin/web/sbsportal/src/static
<Directory /home/sbsadmin/web/sbsportal/src/static>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride None
Order allow,deny
Require all granted
</Directory>
Alias /static /home/sbsadmin/web/sbsportal/src/templates
<Directory /home/sbsadmin/web/sbsportal/src/templates>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride None
Order allow,deny
Require all granted
</Directory>
Alias /media /home/sbsadmin/web/sbsportal/src/media
<Directory /home/sbsadmin/web/sbsportal/src/media>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride None
Order allow,deny
Require all granted
</Directory>
</VirtualHost>
wsgi.py
"""
WSGI config for sbsportal project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sbsportal.settings")
application = get_wsgi_application()
Settings.py
.
.
.
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
.
.
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I have resolved pretty much all the problems. Only thin is that now I am seeing 403 forbidden error.
Did you added selinux context in your project diretory ? if not do the following :
semanage fcontext -a -t httpd_sys_content_t '/home/sbsadmin/myproject/(/.*)'
restorecon -Rv /home/sbsadmin/myproject/
Or try disabling selinux setenforce 0