Forbidden access for django apache2 media files - python

I have trouble with access my files from django admin site. I save files without any trouble, but when I try open it, I get error:
Forbidden
You don't have permission to access /media/file.pdf on this server.
In django project settings:
STATIC_URL = '/static/'
STATIC_ROOT = '/full/path/to/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
In project urls.py:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In my virtualhost I added this settings:
Alias /static/ "/static/folder/"
Alias /media/ "/meida/folder/"
<Directory "/static/folder">
Require all granted
</Directory>
<Directory "/media/folder">
Require all granted
</Directory>
But still get this error. Where can be bug/mistake?
Edit 1: apache error log gives:
client denied by server configuration: /etc/apache2/home

Change /etc/apache2/conf-available/php5-fpm.conf. Replace every
Order Deny,Allow
Deny from all
to
Require all granted
The configuration has to be enabled by a2enconf php5-fpm.
EDIT
Follow the Basic Configuration: https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/
EDIT 2
Do the changes and add this to your virtualhost settings:
<Directory /usr/local/django_apps/myapp/static>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Note that the path is from the root!

Related

Static files are not loading after deployment on digitelocean

I have deploye my django application on digitel ocean by following this
blog:alocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-16-04
this is the url as an error i am getting in console for static files
(index):5913 GET http://67.205.160.21/static/js/dashkit.min.js
net::ERR_ABORTED 403 (Forbidden) (index):5913 GET
http://67.205.160.21/static/js/style.js net::ERR_ABORTED 403
(Forbidden)
every thing is fine except static file,
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
after running the python3 manage.py collect static it gives this
/home/podstatsclub/webapp/Podcast_stats/static
which i have put into default.config file which look something like this
Alias /static /home/podstatsclub/webapp/Podcast_stats/static
<Directory /home/podstatsclub/webapp/Podcast_stats/static>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/podstatsclub/webapp/Podcast_stats/podcast>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess Podcast_stats python-home=/home/podstatsclub/webapp/Podcast_stats/env pytho>
WSGIProcessGroup Podcast_stats
WSGIScriptAlias / /home/podstatsclub/webapp/Podcast_stats/podcast/wsgi.py
In your static directory you accidentally grant access only to wsgi.py ... thats a copy paste mistake. Just leave the "require all granted" there.

Django 2.0 Static Files not recognized in Apache2

My website is online, but it can't find the static files ;/
The configurations seem to be all ok, can't find the error...
I don't have admin permission on the server, so it kind'a sucks..
but, at least they pass me the configurations they've put..
Apache Configuration:
<VirtualHost *>
ServerAdmin suporte#pop-rs.rnp.br
DocumentRoot /home/metis/public_html/
ServerName agata.pgie.ufrgs.br
ServerAlias www.agata.pgie.ufrgs.br
#ErrorLog /var/log/apache2/agata-error.log
#CustomLog /var/log/apache2/agata-access.log common
<Directory /home/metis/public_html/static>
Require all granted
</Directory>
<Directory /home/metis/public_html/AGATA/textMiningProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess agata python-path=/home/metis/public_html/AGATA python-home=/home/metis/public_html/AGATA/agataenv
WSGIProcessGroup agata
WSGIScriptAlias / /home/metis/public_html/AGATA/textMiningProject/wsgi.py
</VirtualHost>
Django Version: 2.0
settings.py
Static files (CSS, JavaScript, Images)
https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_PATH = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/' # You may find this is already defined as such.
STATIC_ROOT = '/home/metis/public_html/static'
STATICFILES_DIRS = [
STATIC_PATH
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
And it was asked to the admins to insert the following, no changes in the result tho..
Alias /media/ /home/metis/public_html/AGATA/media
Alias /static/ /home/metis/public_html/static
<Directory /home/metis/public_html/static>
…
Order allow,deny
Allow from all
</Directory>
can someone help?
Edit 1:
The output for collecstatic:
0 static files copied to '/home/metis/public_html/static', 166
unmodified.
The error on the site:
simple-sidebar.css Failed to load resource: the server responded with
a status of 404 (Not Found)

Django with Apache working for admin page but gives 404 for all other urls

I am a django beginner.
I tried to deploy my django(1.10) site from local server using apache2 (2.4.7)
Port opened.
Admin page is getting opened properly but all other views are not getting opened.
URLS.PY
from django.conf.urls import url,include
from django.contrib import admin
from django.views.generic import RedirectView
from django.conf import settings
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^treedump/',include('treedump.urls')),
url(r'^$', RedirectView.as_view(url='/treedump/', permanent=True)),
url(r'^accounts/', include('django.contrib.auth.urls')),
]
TREEDUMP.URLS
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.index,name='index'),
url(r'printx$',views.printx,name = 'printx'),
url(r'download$',views.download,name = 'download'),
]
.CONF FILE IN APACHE
<VirtualHost *:80>
ServerName *********
DocumentRoot /var/www/html/
Alias /static /data1/ekmmsc/webapp/ekmmsc/static
WSGIDaemonProcess ekmmsc python-path=/data1/ekmmsc/webapp/ekmmsc python- home=/data1/ekmmsc/webapp/ekmmsc/ekmmscenv
WSGIProcessGroup ekmmsc
WSGIScriptAlias /ekmmsc /data1/ekmmsc/webapp/ekmmsc/ekmmsc/wsgi.py
# Other directives here ...
<Directory "/var/www/html/">
allow from all
order allow,deny
AllowOverride All
AddType text/css .css
AddType text/javascript .js
</Directory>
<Directory "/data1/ekmmsc/webapp/ekmmsc/static">
Require all granted
</Directory>
<Directory "/data1/ekmmsc/webapp/ekmmsc/ekmmsc">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
OUTPUT
Not Found
The requested URL /treedump/ was not found on this server.
Any help will be appreciated , i have been working almost for 2 days on this.
Please help.
You should use pattern_name rather than url in your view. This will make Django look up the URL by name, rather than using a hard-coded value, so it will take into account the script prefix.
RedirectView.as_view(pattern_name='index', permanent=True)),

Upload image via Django admin

At the moment I am working on a Django project for a scholarship using Django 1.6.4. I would like to upload some images to my apache server. Unfortunately, I was not able to overcome the "Permission denied" message after filling out the form in the admin page. My project is located under /home/nick/Workspace/Web/kleyboldt_django/. There is my virtualenv directory and my real project called kleyboldt_homepage located (kleyboldt is the name of the guy I am writing for). My model looks like this:
/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/homepage/models.py:
class stipendiat(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
content = models.TextField()
year = models.IntegerField()
image = models.ImageField(upload_to='stipendiaten/', default='default.jpg')
My settings are:
/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/kleyboldt_homepage/settings.ps:
...
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static-only')
STATIC_URL = '/static/'
...
and
/etc/apache2/apache2.conf:
WSGIDaemonProcess mks.com python-path=/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage:/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages
WSGIProcessGroup mks.com
<VirtualHost *:80>
Alias /robots.txt /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/robots.txt
Alias /favicon.ico /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static-only/favicon.ico
<LocationMatch "\.(jpg|css|gif|pdf|ico)$">
SetHandler None
</LocationMatch>
Alias /media/ /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/media/
Alias /static/ /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static-only/
Alias /static/admin /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static-only/admin/
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/media>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static-only>
Require all granted
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /home/nick/Workspace/Web/kleyboldt_django/kleyboldt.wsgi
ServerName mks.com
<Directory /home/nick/Workspace/Web/kleyboldt_django/>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Files /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/homepage.log>
Require all granted
Order allow,deny
Allow from all
</Files>
<Directory /home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/>
Require all granted
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/apache_error.log
LogLevel debug
</VirtualHost>
Sorry that I wrote so much but I wanted to be sure that I did not forget a directory.
mks.com is redirected to my local machine with an entry in /etc/hosts.
The permissions for my media directory are:
drwxrwxrwx 4 nick www 4096 Mai 20 13:21 media
Does anybody know how I can solve this error?

Django admin page is missing images/css/js

I have found a few questhons re my problem, you the solutions didn't help me so I started a new question. Basically that's how I see the admin page
Apache's config:
<VirtualHost *.*.*.*:80>
ServerName ********.org
ServerAdmin ****#******.org
WSGIScriptAlias / /var/www/webproxy/webproxy/wsgi.py
DocumentRoot /var/www/cgi-bin/
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
ErrorLog /var/www/webproxy/apache/error.log
LogLevel warn
CustomLog /var/www/webproxy/apache/access.log combined
Alias /media/ "/var/www/webproxy/media/"
<Directory "/var/www/webproxy/media/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<Directory "/var/www/webproxy/static/">
Order deny,allow
Allow from all
</Directory>
This is what I have in urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from core import urls as core_urls
from settings import MEDIA_ROOT, WEBPROXY_MEDIA_ROOT
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT}),
(r'^ui/(?P<path>.*)$', 'django.views.static.serve', {'document_root': WEBPROXY_MEDIA_ROOT}),
)
settings.py:
MEDIA_ROOT = '/var/www/webproxy/media/'
STATIC_ROOT = '/var/www/webproxy/static/'
WEBPROXY_MEDIA_ROOT = '/var/www/webproxy/static/media/'
I'm not sure what's wrong here. Any help would be really appreciated.
First, you shouldn't be serving the static files via django.views.static.serve in production as that is meant to be used only for local development (read docs here):
There may be files other than your project's static assets that, for
convenience, you'd like to have Django serve for you in local
development.
Second, your Django Admin static files reside under the location Django was installed at. I find using directly these files a better solution than copying (or symlink'ing) the Django admin static files under my own static files directories.
Example Django static files location on my server:
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
Therefore in your web server config you have to point the url /media/admin to
[my_django_location]/contrib/admin/static/admin/
If you're doing things locally, then you might need another entry in your urls.py:
(r'^admin/static/(?P<path>.*)$', 'django.views.static.serve', '/path/to/your/admin/files/'),

Categories

Resources