unable to access admin media folder - django

I have deployed my application on apache 2.2.21
In my settings.py file i have given like this
MEDIA_ROOT = os.path.dirname(__file__)+'\\media'
MEDIA_URL = '/mymedia/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
i am getting admin style as we run 127.0.0.1:8000/admin(django server)
but unable to get admin style when running on apache
http.conf of apache i have only mentioned as follows
WSGIScriptAlias / C:/Python27/Lib/site-packages/django/bin/myapp/django.wsgi
<Directory C:/Python27/Lib/site-packages/django/bin/myapp>
Order deny,allow
Allow from all
</Directory>
is there any changes or anything else i have missed here please advise me,
thank you,

Try using Apache to serve the static content instead of Django:
Alias /static/ /path/to/static/dir/

I think you just forgot ' in MEDIA_ROOT line.
Ok, then try to add this in your apache conf:
<Location "/mymedia/">
SetHandler None
</Location>
<Location "/static/">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png)$">
SetHandler None
</LocationMatch>

Related

How to properly serve a Django site under a Sub URI in Apache?

I am trying to serve Mayan EDMS (a Django project) in a sub URI of another site. already checked and followed instructions from these posts:
1) https://gitlab.com/mayan-edms/mayan-edms/issues/350
2) How to host a Django project in a subpath?
3) https://docs.webfaction.com/software/django/config.html#mounting-a-django-application-on-a-subpath
here is part of my apache config:
# Mayan in running on http://0.0.0.0:8000
ProxyPass /mayan http://127.0.0.1:8000
<Directory "/mayan">
Options FollowSymLinks Indexes
SetHandler uwsgi-handler
</Directory>
Alias "/mayan-static" "/opt/mayan/mayan-edms/media/static/"
<Location "/mayan-static">
SetHandler None
Require all granted
</Location>
I added these in the Django settings file:
USE_X_FORWARDED_HOST = True
FORCE_SCRIPT_NAME = '/mayan'
BASE_PATH = '/mayan'
STATIC_URL = '/mayan-static/'
MEDIA_URL = BASE_PATH + '/media/'
I expected that Mayan will be loaded in http://example.com/mayan,
but it redirects to http://example.com/#/mayan and returns a 404 error.
Did I miss something? or did I do something wrong?
I asked this too in https://gitlab.com/mayan-edms/mayan-edms/-/issues/1002
and https://forum.mayan-edms.com/viewtopic.php?f=7&t=5665.
Good news as apparently it is fixed in commit
https://gitlab.com/mayan-edms/mayan-edms/-/commit/2b567ed24a1e0c431a1d7cedca8e39923d47c5f4
Cheers

Django + Apache: cannot serve static files

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?
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
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.

Linking stylesheet to Django website on Apache

I'm trying to get my Django website to use a stylesheet that until now worked just fine on a development server. As you can see below, I've been using the static files loader until now but no luck ever since I deployed it on Apache.
I'm hardcoding it now but that doesn't work either. I used collectstatic thus I should have all static files I need. I modified Apache config file to serve static files (I followed tutorial).
<!-- {% load static %}
<link href='{% static "stylesheet.css" %}' rel='stylesheet' type='text/css'>-->
<link href='var/www/html/site/static/stylesheet.css' rel='stylesheet' type='text/css'>
Any ideas?
UPDATE: Config files and settings.py file:
<VirtualHost *:80>
WSGIScriptAlias / /home/pi/site.wsgi
ServerName 192.168.1.159
Alias /static /var/www/html/site/static/
Alias /static /var/www/html/site/media/txt/
Alias /static /var/www/html/site/media/photos/
<Directory /var/www/html/site/media/txt>
Require all granted
</Directory>
<Directory /var/www/html/site/media/photos>
Require all granted
</Directory>
<Directory /var/www/html/site/static>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/html/site/>
Allow from all
</Directory>
</VirtualHost>
Settings.py:
STATIC_ROOT = 'static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'/var/www/html/site/static/',
]
And in Apache.conf file I just added this to the end:
WSGIPythonPath /var/www/html/site
Changes made:
STATIC_ROOT = 'static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
'/var/www/html/site/app/static', # this line modified to be more
] # clear
FILES_URL = '/media/txt/'
FILES_ROOT = os.path.join(BASE_DIR, 'media/txt')
MEDIA_URL = '/media/photos/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/photos')
Changes made in site's config file on Apache:
ServerName 192.168.1.159
Alias /static /var/www/html/site/app/static/
Alias /files /var/www/html/site/media/txt/
Alias /media /var/www/html/site/media/photos/
And I'm using this loader to fetch it:
{% load static %}
<link href='{% static "stylesheet.css" %}' rel='stylesheet'
type='text/css'>
When I type the path into terminal and hit Enter the stylesheet opens as expected.
It looks like your Apache configuration isn't quite right. You have three aliases for /static here:
Alias /static /var/www/html/site/static/
Alias /static /var/www/html/site/media/txt/
Alias /static /var/www/html/site/media/photos/
Each of these should point to a different alias, something like this:
Alias /static /var/www/html/site/static/
Alias /media /var/www/html/site/media/
I'm not 100% sure, but I'm guessing that /static is pointing to the last one you declare, /var/www/html/site/media/photos/. You can verify this by checking a file that exists under /var/www/html/site/media/photos/ in the browser like this: http://yoursite/static/photo_that_exists.jpg
See here for more details: https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/modwsgi/#serving-files
Either way, you should only have one Alias definition per path. Give that a try.
As turned out ... All I needed to do is change ServerName 192.168.1.159 to ServerName 127.0.0.1 and static files loaded up like nothing.
Apparently Django in deployment needs to be pointed to 127.0.0.1 which is IP representation of 'localhost'. If I use my internal IP address Django loader for static files won't accept it.
I'd like to acknowledge #FlipperPA who pointed out at other bug I had which I would've otherwise missed. Thank you.
I don't think my understanding of this problem is correct so if you've got better explanation, please do share with me. Cheers.

Serving admin css for django 1.5 using apache

I am moving a application built on Django 1.5 to the development server, running apache, for the first time. I have it mostly running properly, but I am having issues with the CSS serving. I can either get the site's CSS working but not the admin's css or have the admin's css working but not the site's css.
I followed the documentation's and used the collectstatic command to get all the static assets into the STATIC_ROOT folder.
This is my relevant data from my settings file
STATIC_ROOT = '/var/www/projectmanagement/django/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/var/www/projectmanagement/django/projectmanagement/projects/static',
)
This is the relevant data from httpd.conf
Alias /static/admin/ /var/www/projectmanagement/django/static/admin/
Alias /static /var/www/projectmanagement/django/static/
<Directory /var/www/projectmanagement/django/static/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /var/www/projectmanagement/django/projectmanagement/django.wsgi
<Directory /usr/local/wsgi/scripts>
Order allow,deny
Allow from all
</Directory>
and from my vhost.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#pmt.com
ServerName pmt.com
ServerAlias www.pmt.com
DocumentRoot /var/www/projectmanagement/html/
ErrorLog /var/www/projectmanagement/logs/error.log
CustomLog /var/www/projectmanagement/logs/access.log combined
WSGIScriptAlias /projectmanagement /var/www/projectmanagement/django/projectmanagement/django.wsgi
<Directory "/var/www/projectmanagement/html">
php_admin_value open_basedir "/var/www/projectmanagement/html/:/tmp/:/var/www/projectmanagement /django/projectmanagement/"
php_admin_value include_path "/var/www/projectmanagement/html/:/tmp/:/var/www/projectmanagement /django/projectmanagement/"
</Directory>
</VirtualHost>
When I add the line
AliasMatch /([^/]*\.css) /var/www/projectmanagement/django/static/admin/css/$1
to before the first alias in my httpd.conf file, I get the admin css to work but not the site's css. If it's gone then the site's css works but not the admin's. This has me fairly stumped.
Adding that alias will capture all .css requests, but only looks capable of serving the admin css. Removing it means the css requests will reach your WSGIScriptAlias, and I presume you've got DEBUG = True... so django will be serving your static media for you.
I don't know why django's built in static server isn't working for your admin css - have you set up STATICFILES_FINDERS?
Anyway your alias really should be something like this, so it can serve all of your static files:
AliasMatch /static/ /var/www/projectmanagement/django/static/

Serving static files on my own server

im setting a django server, but im having problem with my static's file's:
django settings
STATIC_URL = 'http://localproject/static/'
STATIC_ROOT = '/srv/www/project/static/'
MEDIA_ROOT = '/srv/www/project/public/'
MEDIA_URL = '/public/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
apache
Alias /static/ /srv/www/project/static/'
<Document /srv/www/project/static>
Order allow,deny
Allow form all
</Document>
Alias /public/ /srv/www/project/public/'
<Document /srv/www/project/public>
Order allow,deny
Allow form all
</Document>
So, i have the admin without style and the website, for example http://localproject/public is showing a error about * Not FlatPage matches the given query *
yes, im using django.contrib.staticfiles
Any idea?
Thanks
Two things:
First, ADMIN_MEDIA_PREFIX = STATIC_URL+'admin/'. You can actually use that or change it to ADMIN_MEDIA_PREFIX = 'http://localproject/static/admin/'. /static/admin/ is incorrect in your scenario.
Second, Django is still being handed the request, and 'public' is being passed as a slug to the FlatPages view, which is why you're getting that error. See: https://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/#serving-files for the proper Apache configuration for staticfiles.
Im answering maybe that help someone, the problem was that im using apache (/etc/apache2/sites-available/project), but i was wrong with "use another apache config into the project" /srv/www/project/apache/httpd.conf and setting my Alias in this last one config, when the Alias config need to be in (/etc/apache2/sites-available/project)
using:$ sudo nano /etc/apache2/sites-available/project
and the content for project
<VirtualHost *:80>
ServerName project
DocumentRoot /srv/www/project
<Directory /srv/www/project>
Order allow,deny
Allow from all
</Directory>
AliasMatch ^/([^/]*\.css) /srv/www/project/static/css/$1
Alias /public/ /srv/www/project/public/
Alias /static/ /srv/www/project/productos/static/
<Directory /srv/www/project/productos/static>
Order deny,allow
Allow from all
</Directory>
<Directory /srv/www/project/public>
Order deny,allow
Allow from all
</Directory>
WSGIDaemonProcess project processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup project
WSGIScriptAlias / /srv/www/project/apache/django.wsgi
</VirtualHost>
my settings.py:
MEDIA_ROOT = '/srv/www/project/public/'
MEDIA_URL = 'http://project/public/'
STATIC_ROOT = '/srv/www/project/productos/static/'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = 'http://project/static/admin/'
And now is working :), remember this is my own server