Can't get Apache to serve django admin static files - django

I'm trying to deploy Django to apache but can't get it to serve my static admin files. It seems to be looking for them under /var/www/static and I can't seem to be able to change that.
The admin site seem to be working except for styling. I get a title and a log in form. My django app is working too. It's the static files for the admin that aren't served.
Using Django 1.4.1.
The files are under /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static and linked to from /home/dutt/vaccapp/backend/static/admin.
The apache error log says this
[Sun Sep 30 10:57:20 2012] [error] [client 192.168.1.10] File does not exist: /var/www/home, referer: http://dathui.example.com/vaccapp/admin/
[Sun Sep 30 10:57:20 2012] [error] [client 192.168.1.10] File does not exist: /var/www/home, referer: http://dathui.example.com/vaccapp/admin/
But I'm not sure how to change it.
In my django site config I have
<VirtualHost *:80>
ServerAdmin me#host.com
ServerRoot "/home/dutt/vaccapp"
DocumentRoot "/home/dutt/vaccapp"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/dutt/vaccapp/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Alias /static/ "/home/dutt/vaccapp/backend/static/"
<Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
ServerRoot is not set in apache2.conf.
From my settings.py
STATIC_ROOT = '/home/dutt/vaccapp/backend/'
STATIC_URL = '/static/'
Nothing added to STATICFILES_DIRS.
This is added to my apache2.conf
WSGIScriptAlias /vaccapp /home/dutt/vaccapp/backend/wsgi.py
WSGIPythonPath /home/dutt/vaccapp
<Directory /home/dutt/vaccapp>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

ADMIN_MEDIA_PREFIX is set by default to /static/admin/ # Deprecated in Django 1.4 (now using STATIC_URL + 'admin/'. The result is the same.
Here's the fixes to the apache config:
Alias /static/admin "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static"
Alias /static "/home/dutt/vaccapp/backend/static"
<Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
And the WsgiScriptAlias had to be moved from the main apache config into the VirtualHost.
After a long discussion we found the problem was that Django did not install the admin static properly ... they were symlinked to eachother (very weird). A Django reinstall fixed it and it worked fine now.

My low reputation forces me to write a whole answer to add a small detail to Igor's answer.
I simply added the apache config part to my configuration, but it was not sufficient. I had to change:
"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static"
to
"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin"
both in the first Alias and in the <Directory> directive.

Just like Blazor I had to use a small variation of the original. My setup includes graphite-web, which is using django admin. I'm just posting it for reference.
My apache's virtual host:
<VirtualHost *:80>
ServerName graphite.myhost.com
Redirect permanent / https://graphite.myhost.com/
</VirtualHost>
<VirtualHost *:443>
ServerName graphite.myhost.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/graphite.cert
SSLCertificateKeyFile /etc/apache2/ssl/ssl_graphite.key
SSLStrictSNIVHostCheck on
WSGIDaemonProcess _graphite processes=5 threads=5 display-name='%{GROUP}' inactivity-timeout=120 user=_graphite group=_graphite
WSGIProcessGroup _graphite
WSGIImportScript /usr/share/graphite-web/graphite.wsgi process-group=_graphite application-group=%{GLOBAL}
WSGIScriptAlias / /usr/share/graphite-web/graphite.wsgi
AliasMatch ^/admin/(.*)static/admin(.*)$ /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/$2
<Directory "/usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/">
Order allow,deny
Allow from all
</Directory>
Alias /content/ /usr/share/graphite-web/static/
<Location "/content/">
SetHandler None
</Location>
<Location "/">
Order allow,deny
allow from all
AuthType Basic
AuthName "Restricted Zone"
AuthBasicProvider wsgi
WSGIAuthUserScript /var/www/django_auth.wsgi
Require valid-user
</Location>
ErrorLog ${APACHE_LOG_DIR}/graphite-web_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/graphite-web_access.log combined
</VirtualHost>
I also added STATIC_URL = 'static/' just to make sure I didn't have any issues with the regex.

Try using next
python manage.py collectstatic
The staticfiles app - Django documentation

I got similar problem when running Django + Apache, the Django admin site misses all styling. This is how I solved it:
apache conf: httpd-app.conf
Alias /static "/...path/to/your/django.../site-packages/django/contrib/admin/static"
<Directory "/...path/to/your/django.../site-packages/django/contrib/admin/static">
Require all granted
</Directory>
The /...path/to/your/django.../ above, you can find it by pip show django

Related

Stuck on apache2 default page while deploying Django website

I have gone through a few answers on the web and I am still struggling to get past the apache2 default page. The webapp works fine locally and without apache. I am using an AWS EC2 instance for this on an ubuntu machine.
The whole code is hosted in /home/ubuntu/sm on the machine, the venv is in /home/ubuntu/sm/venv and running on the ip addr 3.X.X.212
I've tried adding, editing, removing a few variables, to no avail. I got nothing in the error logs either. The 'gracefully restarting' entries are starting to look narcissistic given how long I've been stuck on this.
apachectl -S produces the following response:
[Tue Nov 05 05:16:52.198045 2019] [alias:warn] [pid 13664:tid 139764718218176] AH00671: The Alias directive in /etc/apache2/sites-enabled/sm.conf at line 33 will probably never match because it overlaps an earlier Alias.
VirtualHost configuration:
*:80 ip-172-*-*-29.ap-southeast-2.compute.internal (/etc/apache2/sites-enabled/sm.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
"sm.conf" is the config file used for this, as below:
ServerAdmin webmaster#localhost
LogLevel info
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /robots.txt /home/ubuntu/sm/website/static/robots.txt
Alias /favicon.ico /home/ubuntu/sm/website/static/favicon.ico
Alias /static/ /home/ubuntu/sm/website/static
Alias /static/ /home/ubuntu/sm/media
WSGIDaemonProcess website.net processes=2 threads=15 python-home=/home/ubuntu/sm/venv/lib/python3.6 python-path=/home/ubuntu/sm/
WSGIProcessGroup website.net
WSGIScriptAlias /sm /home/ubuntu/sm/sm/wsgi.py process-group=website.net
<Directory /home/ubuntu/sm/>
AllowOverride all
Require all granted
Options FollowSymlinks
</Directory>
<Directory /home/ubuntu/sm/sm>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/ubuntu/sm/website/static/>
Require all granted
</Directory>
<Directory /home/ubuntu/sm/media>
Require all granted
</Directory>
Comments, concerns, suggestions, critique of shoddy code, all appreciated.

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

Using iRedMail with a django site on the same server

I am trying to create a small django site and use iRedMail for e-mail. I installed iRedMail first, and ensured that it worked. I could go to both www.domain.com/iredadmin and www.domain.com/mail and have it work perfectly. My next step was to install my django site and configure Apache. Unfortunately, this caused my django site to try and handle /mail/ and /iredadmin/. I've been fidgeting with the config for a few hours now and have no idea what to do. Here are the settings:
apache2.conf:
# Defaults...
WSGIPythonPath /path/to/website.com/website
sites-enabled/website.com:
<VirtualHost *:80>
ServerName website.in
ServerAlias www.website.in
ErrorLog ${APACHE_LOG_DIR}/error.log
Alias /static /path/to/website.com/website/static
Alias /media /path/to/website.com/website/media
Alias /mail /usr/share/apache2/roundcubemail/
Alias /admin /usr/share/apache2/iredadmin/
<Directory /usr/share/apache2/roundcubemail/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /path/to/website.com/website/website.wsgi
<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE website.settings
PythonDebug Off
PythonPath "['/path/to/website.com/website/']+sys.path"
</Location>
<Directory /path/to/website.com/website>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Directory /path/to/website.com/website/static>
Order allow,deny
Allow from all
</Directory>
<Location /static/>
SetHandler None
</Location>
<Directory /path/to/website.com/website/media>
Order allow,deny
Allow from all
</Directory>
<Location /media/>
SetHandler None
</Location>
</VirtualHost>
The django website displays fine, although I have been getting internal server errors.
You are trying to use both mod_wsgi and mod_python to handle the Django site at the same time, with mod_python overriding mod_wsgi. Choose one of the other. Since mod_python is no longer developed or supported and support for it in Django deprecated, probably not a good option to keep using it.
The next thing which is wrong is:
Alias /mail /usr/share/apache2/roundcubemail/
Alias /admin /usr/share/apache2/iredadmin/
Remove the trailing slashes:
Alias /mail /usr/share/apache2/roundcubemail
Alias /admin /usr/share/apache2/iredadmin
Even then it will still not work, because when using mod_python you have to tell mod_python not to handle those paths.
<Location /mail/>
SetHandler None
</Location>
<Location /admin/>
SetHandler None
</Location>
A further problem you may have is that /admin is usually used for the Django admin interface and you are overriding that.

Deploy a Django site and a PHP site on the same server with Apache and mod_wsgi

I currently have a Django site working at cinepass.com.ec , I would like to deploy an additional PHP site to the same server at mobile.cinepass.com.ec
My current httpd.conf (from DjangoFoo) :
<Directory "/home/ec2-user/cinepass/media">
Order deny,allow
Allow from all
</Directory>
<Directory "/home/ec2-user/cinepass/cinepass">
AllowOverride All
Order deny,allow
Allow from all
</Directory>
Alias /media/ /home/ec2-user/cinepass/media/
ServerAdmin smansfield#palapa.com.ec
ErrorLog "logs/cinepass.com-error_log"
CustomLog "logs/cinepass.com-access_log" common
# mod_wsgi configuration is here
# we are running as user/group 'deamon', if you don't have those you need to change or create.
WSGIDaemonProcess cinepass python-path=/home/ec2-user/cinepass:/home/ec2-user/cinepass/venv/lib/python2.6/site-packages user=daemon group=daemon processes=2 threads=25
WSGIProcessGroup cinepass
# this is our WSGI file.
WSGIScriptAlias / /home/ec2-user/cinepass/cinepass/wsgi.py
My current wsgi.py :
import os, sys
sys.path.append('/home/')
sys.path.append('/home/ec2-user/cinepass/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cinepass.settings_production.py")
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
How would I edit my Apache configuration so that I can also run a php site at mobile.cinepass.com.ec?
Using apache´s virtualhosts, here I put an example of something similar in a server of mine, in which I have a djangp app in the main domain and a joomla in a subdomain. Both files are located in /etc/apache2/sites-enabled
Joomla´s apache conf file (named /etc/apache2/sites-enabled/manual.domain.com):
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin dsanabria#domain.com
ServerName manual.domain.com
DocumentRoot "/home/ubuntu/manual/"
<Directory /home/ubuntu/manual/>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/manual.domain-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/manual.domain-access.log combined
</VirtualHost>
And the django app (named /etc/apache2/sites-enabled/www.domain.co):
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin diego#diegue.us
ServerName domain.co
ServerAlias machete.anotherdomain.com
Alias /admin/media/ /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/
Alias /media/ /home/ubuntu/webapps/machete/machete/media/
Alias /static/ /home/ubuntu/webapps/machete/machete/collected/
<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/django/contrib/admin/media/ >
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/machete/media/>
Order deny,allow
Allow from all
</Directory>
<Directory /home/ubuntu/webapps/machete/machete/collected/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptReloading On
WSGIDaemonProcess machete python-path=/home/ubuntu/webapps/machete/lib/python2.7/site-packages
WSGIProcessGroup machete
WSGIApplicationGroup machete
WSGIPassAuthorization On
WSGIScriptAlias / /home/ubuntu/webapps/machete/machete/machete/wsgi.py
ErrorLog /var/log/apache2/machete-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/machete-access.log combined
</VirtualHost>
The first, tells to apache, that if the user gets to manual.domain.com, just response with a php application (joomla). The second file says to apache, that if the user calls the server with www.domain.com response with a python wsgy, (django).
This is in a ubuntu server, redhat/centos/fedora locates the folder sites-enabled in another location that I can´t remember, but anyway you can use virtualhosts.
Generraly, I avoid to mess with the httpd.conf file and prefer use virtualhosts.

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/