Static files are not loading after deployment on digitelocean - python

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.

Related

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)

Apache2 host, url not served with mod_wsgi

Just stated to host a website with Django, the problem is like this.
I put an html file for testing under /usr/local/django/virenv/Personal/mysite.com/, which is also the Document root in 000-default.conf, that page can be access via mysite.com
Then I started a django project and an app, in the app/view.py I put
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, a django view.")
In the app/urls.py I put
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^index$', views.index, name = 'index'),
]
added the following to project/urls.py
urlpattrens = [
url(r'^home/', include('app.urls')),
# below is admin url created with startproject
url(r'^admin/', admin.site.urls)),
]
lastly I turn DEBUT=False in project/settings.py, and added ip to ALLOWED_HOSTS
After python manage.py runserver ip:8080 , ip:8080/home/index, and ip:8080/admin are all available.
Next I tried restart apache2, went to mysite.com/admin and mysite.com/home/index
both got
404
Not Found The requested URL /admin was not found on this server.
I guess the mod_wsgi is not working.
The httpd.conf looks like
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / /usr/local/django/virenv/Personal/mysite.com/project/wsgi.py process-group=mysite.com
WSGIPythonHome /usr/local/django/virenv
WSGIDaemonProcess mysite.com python-home=/usr/local/django/virenv python-path=/usr/local/django/virenv/Personal/mysite.com
WSGIProcessGroup mysite.com
WSGIApplicationGroup %{GLOBAL}
<Directory /usr/local/django/virenv/Personal/mysite.com/project>
<Files wsgi.py>
Order allow,deny
Allow from all
Require all granted
</Files>
</Directory>
<Directory /usr/local/django/virenv/Personal/mysite.com/>
Order allow,deny
Allow from all
Require all granted
Options -Indexes +FollowSymLinks -Includes
</Directory>
000-default.conf looks like
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /usr/local/django/virenv/Personal/mysite.com
<Directory /usr/local/django/virenv/Personal/mysite.com>
AllowOverride All
Options +FollowSymLinks
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I use ubuntu 16 lts with apache2.4, django 1.10
every directory and file along this path
/usr/local/django/virenv/Personal/mysite.com/*
is set to chmod a+x and chmod a+r
Any guesses or solution?
EDIT: by the way, in wsgi.py
import os
import sys # added from other stack overflow answers
import django.core.handlers.wsgi as WSGIhand # added from other posts
os.environ.["DJANGO_SETTINGS_MODULE"] = "project.settings"
application = WSGIhand.WSGIHandler()

Forbidden access for django apache2 media files

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!

Serving static files through apache

I am new to the whole mod_wsgi and serving files through apache. I am really comfortable with flask but this is something i can't get my head around. I did the hello-world program and successfully displayed hello world!
Now i wanted to display a image file. So I updated my hello-world.py to:
from flask import *
yourflaskapp = Flask(__name__)
#yourflaskapp.route("/")
def hello():
file="203.jpg"
return render_template("hello.html",file=file)
# return"HEY"
if __name__ == "__main__":
yourflaskapp.run()
my directory structure is something like:/var/www/hello-world
/hello-world
test.py
yourflaskapp.wsgi
/static
-203.jpg
/templates
-hello.html
My template is simple:
<!DOCTYPE html>
<html><head><title>hi</title></head>
<body>
<img src="{{url_for('static',filename=file)}}"/>
</body></html>
and my apache conf file is:
<VirtualHost *:80>
WSGIDaemonProcess yourflaskapp
WSGIScriptAlias / /var/www/hello-world/yourflaskapp.wsgi
Alias /static/ /var/www/hello-world/static
Alias /templates/ /var/www/hello-world/templates
<Directory /var/www/hello-world>
WSGIProcessGroup yourflaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/hello-world/static>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/hello-world/templates>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Though when i open the browser and head to my ip, it doesn't show the image file.
What am i doing wrong? Is there any other approach i should follow?
and if anyone could recommend any good links from where i can understand working with flask+mod_wsgi+apache2
It is generally always a good idea to have trailing slash balanced when mounting static files at a sub URL. So instead of:
Alias /static/ /var/www/hello-world/static
use:
Alias /static /var/www/hello-world/static

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?

Categories

Resources