Django - Apache2 can't load media files from media folder - python

I installed an linux server with apache where I want to run my Django app. So I set everything up and when I started the app everything was shown to me except for the pictures.
I couldn't find a error inside my site.conf
<VirtualHost *:80>
ServerName MYHOST
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 /static/ /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>
or inside my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/mysite/static'
MEDIA_ROOT = '/var/www/mysite/media'
MEDIA_URL = '/media/'
What I did was setting debug = True and inside linux console I run python3 manage.py collectstatic after reloading apache the website was shown with all its css and js but for every image request I get an error 404.
Inside my templates/base.html I call {% load static %}. I tried to replace it with {% load staticfiles from static %} but that crashed my entire app (err 500)
I use Django version 3.x and apache2

Related

Multiple wsgi.mod on single apache

I have one django3 application on apache http://myapp.example.com
For now it works, but I want to put second django3 application on the same server with Anaconda.
Is it possible? if so , How should I set WSGIPythonPath and Alias /static/ for each application??
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName myapp.example.com
DocumentRoot "/var/www/html/myapp/current/"
ErrorLog ${APACHE_LOG_DIR}/myapp_error.log
CustomLog ${APACHE_LOG_DIR}/myapp_access.log combined
<Directory /var/www/html/myapp/current/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /var/www/html/myapp/current/myapp/wsgi.py
WSGIDaemonProcess py37 user=ubuntu group=ubuntu python-path=/home/ubuntu/anaconda3/envs/py37/lib/python3.7/site-packages
</VirtualHost>
LoadModule wsgi_module /home/ubuntu/anaconda3/envs/py37/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so
WSGIPythonHome /home/ubuntu/anaconda3/envs/py37/
WSGIPythonPath /var/www/html/myapp/current/
<Directory /var/www/html/myapp/current>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
Alias /static/ /var/www/html/myapp/current/static/
Alias /media/ /var/www/html/myapp/current/media/
<Directory /var/www/html/myapp/current/static>
Order allow,deny
Allow from all
</Directory>
simply clone the virtual host and replace the paths relevant for the new application, you may need to move the directory alias:
Alias /static/ /var/www/html/myapp/current/static/
Alias /media/ /var/www/html/myapp/current/media/
<Directory /var/www/html/myapp/current/static>
Order allow,deny
Allow from all
</Directory>
into the virtual host. I do not use the wsgi.py but a site.wsgi. Which runs the app.
Its content is:
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('{{PATH TO}/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('{PATH TO PROJECT}')
os.environ['DJANGO_SETTINGS_MODULE'] = '{PROJECT}.settings'
# Activate your virtual env
activate_env=os.path.expanduser("{PATH TO}/bin/activate_venv.py")
exec(open(activate_env).read(), dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
What works on my stage is(httpd.conf):
LoadModule wsgi_module {PATH TO}/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
WSGISocketPrefix /var/run/wsgi
and the pyhton path/script is set in the virtual host instead in the main file:
WSGIDaemonProcess {GROUP} threads=1 python-home={PATH TO} python-path={PATH TO APP} user={USER}
WSGIProcessGroup {GROUP}
WSGIScriptAlias / {PATH TO FILE}/site.wsgi
you may need to comment the line that starts the application in your wsgi.py if you want to use the settings above

Django 3 admin link is coming without css

The Admin link is missing css, returns 404 when doing view source.
The .conf file is
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /var/www/django/medicalai
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /var/www/django/medicalai
<Directory /var/www/django/medicalai/static>
Require all granted
</Directory>
<Directory /var/www/django/medicalai>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess medicalai python-path=/var/www/django/medicalai python-home=/var/www/django/venv
WSGIProcessGroup medicalai
WSGIScriptAlias / /var/www/django/medicalai/medicalai/wsgi.py
WSGIPassAuthorization On
</VirtualHost>
The Static file contains.
STATIC_DIR = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
The static folder and files are here.
/var/www/django/medicalai/static/admin/css
All the files and folders are downloaded by using
python manage.py collectstatic.
But there is no CSS when I open http://example.com/admin/login/?next=/admin/
Please help to point the mistake.
This issue could happen because you do not have STATIC_ROOT configured when you run
python manage.py collectstatic
What this setting does is to copy all the static files from all the STATICFILES_DIRS directories into a single ROOT prepared for production serving.
STATIC_DIR = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
STATIC_ROOT = '/var/www/django/medicalai/static/'

Permission denied when uploading file (Django, Apache2)

I deployed a beta version of my django app to DigitalOcean and I am serving it using Apache2 for both python and static files.
Here is the issue, when I upload a file, it gives me the following error:
PermissionError at /admin/dashboard/marque/add/
[Errno 13] Permission denied: '/verauto/site/public/media/marques'
Here is an excerpt of my settings.py file, as well as the sites-available configuration for apache2:
settings.py:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
#MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_ROOT = '/verauto/site/public/media'
MEDIA_URL = '/media/'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
#STATICFILES_DIRS = (
# os.path.join(BASE_DIR, 'verautonoapi/static'),
# os.path.join(BASE_DIR, 'boot'),
#)
#STATIC_ROOT = os.path.join(BASE_DIR, 'root')
STATIC_ROOT = '/verauto/site/public/static'
apache2 configuration:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog /verauto/site/logs/error.log
CustomLog /verauto/site/logs/access.log combined
alias /static /verauto/site/public/static
<Directory /verauto/site/public/static>
Require all granted
</Directory>
alias /media /verauto/site/public/media
<Directory /verauto/site/public/media>
Require all granted
</Directory>
<Directory /verauto/app/verauto-staging/verautonoapi/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess verauto python-path=/verauto/app/verauto-staging/ python-home=/verauto/verenv
WSGIProcessGroup verauto
WSGIScriptAlias / /verauto/app/verauto-staging/verautonoapi/wsgi.py
</VirtualHost>
As you can see, in my attempts to solve the issue, I added the media directory in the apache2 configuration, I also did the following commands for chmod rights:
sudo groupadd XXXXXX
sudo adduser www-data XXXXXX
sudo chgrp -R XXXXXX /verauto/site/public/media/
sudo chmod -R 760 /verauto/site/public/media/

How can I fix a 403 forbidden on an apache server using Django?

I get the following error when I try to access my website:
Forbidden. You don't have permission to access / on this server.
My virtual host file looks like the following:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static ~/myproject/static
<Directory ~/myproject/static>
Require all granted
</Directory>
<Directory ~/myproject/myprojectapi>
Order allow,deny
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
And my apache2.confis the default file. I am using Django 1.9 with Python 2.7 on Ubuntu 14.04
I have literally attempted most, if not all, the solutions found on the site but none of them are working for some reason. I followed this tutorial and gave these permissions:
chmod 664 ~/myproject/db.sqlite3
sudo chown :www-data ~/myproject/db.sqlite3
sudo chown :www-data ~/myproject
I am also operating as the root user.
Please let me know if you require any other information; I will be more than happy to provide it. Any help would be greatly appreciated.
Here are the WSGI lines:
WSGIDaemonProcess goggest python-path=~/myproject:/usr/lib/python2.7/dist-packages
WSGIProcessGroup goggest
WSGIScriptAlias / ~/myproject/myprojectapi/wsgi.py
Below is how I fixed (with a lot of help) the problem.
This answer assumes you have the following project structure:
~/myproject
- myproject
- ...
- settings.py
- wsgi.py
- myapp
- ...
- static
- myapp
- ...
Digression
I was facing the problem where I got an error in my apahce error.log which said it could not find settings. This issue is most likely caused by an error in your python-path. This portion assumes that you are using the daemon method (recommended by docs).
Check your wsgi.py file and make sure it says myproject.settings instead of settings in os.environ.setdefault(" ... ", " ... ")
Check and double check your python-path which should be something like
WSGIDaemonProcess [any-name] python-path=/home/[username]/myproject:/usr/lib/python2.7/site-packages
This should fix the problem (at least it did for me).
Fixing 403 Forbidden (or at least, circumventing it)
The following is the conf that did the job for me. It is not apache2.conf and there is nothing new in my apache2.conf, it remains the default one. The following is in apache2/sites-available/000-default.conf.
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
DocumentRoot /home/[username]/myproject # (1)
Alias /static path/to/your/static_root # (2)
<Directory path/to/your/static_root>
Require all granted
</Directory>
<Directory /home/[username]/myproject/myproject>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
<Directory /home/[username]/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess [any-name] python-path=/home/[username]/myproject:/usr/lib/python2.7/site-packages
WSGIProcessGroup [any-name]
WSGIScriptAlias / /home/[username]/[myproject]/[myproject]/wsgi.py
WSGIApplicationGroup %{GLOBAL}
</VirtualHost>
RE: Points (1), (2)
(1). Change the document root to the head of the directory which contains your django project files. In this case, I changed it to /home/[username]/myproject. Do not use ~/ for reference. I tried that and it didn't seem to pan out.
(2). If you are serving your static files from the same server then make sure you have set a STATIC_ROOT and STATIC_URL in your settings.py file. In my case, I set STATIC_URL to /static/ and STATIC_ROOT to /var/www/[any-name]/static/. When you decide to deploy, you will have to run sudo python manage.py collectstatic.
Also note that it should be Alias /static not Alias /static/ (note the trailing slash). The docs show /static/ but that is incorrect and will not work when you try and serve static files (apache does not complete the path).
That's it. All of these steps got my server up and running. If you have any questions, feel free to comment and I will try to help out. Good luck!

404 Error while serving Django 1.7 static files on Apache

I've been a django developer for years now, yet a small practice project with 1.7 is giving me a great headache when it comes to serving static files.
I've set STATIC_ROOT
STATIC_ROOT = '/var/www/mydomain/static'
I've set STATIC_URL
STATIC_URL = '/static/' #as default
I'm not using STATICFILES_DIRS since I have one app called 'pages' and it's in INSTALLED_APPS. On localhost the static files are served correctly
I'm using Ubuntu 14.4 and Apache/2.4.7
My apache conf is
<VirtualHost *:80>
ServerName mydomain.net #I own the domain and pointed it correctly in GoDaddy
ServerAlias www.mydomain.net
Alias /static/ /var/www/mydomain/static
<Directory /var/www/mydomain/static>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/mydomain/mydomain/wsgi.py
<Directory /var/www/mydomain/mydomain>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mydomain.net python-path=/var/www/mydomain:/var/www/mydomain/venv/lib/python2.7/site-packages
WSGIProcessGroup mydomain.net
</VirtualHost>
I've run collectstatic and confirmed all static files are in /var/www/mydomain/static/*.
The site loads, but I get a 404 on all css and js files.
All debugging efforts have failed. I've removed the STATIC_ROOT dir to expect a 403, but still getting a 404. I've chown'd all files/folder to root for testing; nothing. I created a deploy user and chown'd all files/folders to it; nothing. I've chown'd all files/folders to www-data; nothing!!
Is there a new config in Apache 2.4+ that's throwing me off?
What you can do is check your /var/www/mydomain path and look for the folder. I think its not there yet. If not then copy your folder to this place so that apache can serve it.
That will be worth looking

Categories

Resources