Django Apache DocumentRoot? - django

What directory must the DocumentRoot in Apache be set to run a Django app is it the static directory but that does not work? Thanks to all for the help in advance.

You point to project root; where wsgi.py file is. From documentation:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonHome /path/to/venv
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Static file are configured separately. Details here. Example (again, from docs):
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
<Directory /path/to/mysite.com/media>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

When using Django, the DocumentRoot directive is not needed.
Under the traditional approach to web sites, where Apache serves pre-written HTML files, Apache uses DocumentRoot, say
DocumentRoot = /var/www/website
so that when a user request a resource like users/profile.html, it can map that to the filesystem file /var/www/website/users/profile.html, which is an actual file.
In Django, by contrast, there are no pre-written HTML files for Apache to find. Instead, all requests are directed to Django directly, which constructs the HTML file dynamically and returns that. Thus, Apache doesn't need a DocumentRoot, it needs to know the location of Django's WSGI application file wsgi.py, which is the "gateway" for sending HTTP requests to Django so that it can construct the right HTML file to return. This is specifed by the WSGIScriptAlias directive and associated Apache configuration; e.g.
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
as given by the Django documentation on deploying Django with Apache.
TL;DR: You don't need DocumentRoot, you need WSGIScriptAlias for Django.

I think you are asking for this:
ServerAdmin example#localhost
DocumentRoot /var/www/html

Related

LOST on WSGIPythonPath takes one argument, Python module search path

I'm sorry if ask very dumb question.
But I had no idea what i'm doing, I'm just following the tutorial
Link here: https://www.youtube.com/watch?v=F6-yJpPEpoE
Now I'm just trying to start up Apache server to run my Django code in productions
I encounter this error:
C:\WINDOWS\system32>C:/Frontier_Website/Apache24/bin/httpd.exe -k startserver
AH00526: Syntax error on line 542 of C:/Frontier_Website/Apache24/conf/httpd.conf:
WSGIPythonPath takes one argument, Python module search path.
I'm assuming the error is unable to find my python path, something related to python, which apologies I'm not sure what is looking for.
This are the codes in httpd setting:
#python and mod_wsgi setting
LoadModule wsgi_module "c:/users/user/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIScriptAlias / "C:\Frontier_Website\FrounterWeb postgreDB-the secnond\FrounterWeb\wsgi.py"
WSGIPythonHome C:/users/user/appdata/local/programs/python/python37
WSGIPythonPath C:\Frontier_Website\FrounterWeb postgreDB-the secnond\zigview
<Directory C:\Frontier_Website\FrounterWeb postgreDB-the secnond\zigview\static>
Require all granted
</Directory>
<Directory C:\Frontier_Website\FrounterWeb postgreDB-the secnond\zigview>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
In advance thank you, so much on the help
Use this template to a django app in Apache:
<VirtualHost *:80>
. . .
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject python-home=/home/user/myproject/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>
WSGIPythonPath is for additional directories to search for Python modules, not neccesssary if you use a virtual env, I guess
Source: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 (The apache conf works in Windows too)

How to Serve static Media Files in Django?

I'm beginner with Web Server Apache. I want to ask your guys how to Serve Media Files in Django.
I read this: https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/modwsgi/#serving-files
This example sets up Django at the site root, but serves robots.txt, favicon.ico, and anything in the /static/ and /media/ URL space as a static file. All other URLs will be served using mod_wsgi:
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
<Directory /path/to/mysite.com/media>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.
I ask Web Server supporter, they said my server have supported mode mod_wsgi already and they said to me that I can config it in .htaccess. But I really have no knowledge about this. Please help me rewrite URL:
Rewrite URL mydomain.com/served/ to folder with path /home/mydomain.com/public_html/media. All files in /home/mydomain.com/public_html/media/filename.jpg will access with mydomain.com/served/
You should just be able to use the Alias directive to alias /served/ to the media folder in question. So for example:
Alias /served/ /home/example.com/public_html/media/
That would mean that a request to http://example.com/served/filename.jpg will map through to /home/example.com/public_html/media/filename.jpg. You can do a similar thing for resources in /static/.
(Note that the example above uses example.com rather than mydomain.com due to SO restrictions.)

How to set the apache2.conf file for a django project?

I am now deploying a django test project on aws-ec2 and the AMI is Ubuntu18.04 with Python 3.6, Django 2.1, Apache2.
The project is under /var/www/Project and I am trying to add the setting to apache.conf.
The project is simply generated by django-admin startproject Project and I want make sure that when hit the public IP provided by the instance, it should show up the django default page.
WSGIDaemonProcess ubuntu processes=2 threads=12 python-path=/var/www/Project
WSGIProcessGroup ubuntu
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /var/www/Project/Project/wsgi.py
<Directory /var/www/Project/Project>
Require all granted
</Directory>
Now i got the internal server error.
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
I have tried this previously. And it seems like it only works when i use python2.7 with django 1.11.
WSGIScriptAlias / /var/www/Project/Project/wsgi.py
WSGIPythonPath /var/www/Project
<Directory /var/www/Project/Project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
what's wrong with my conf file?
I finally come up with the following solution.
If you are using Apache2, Python2 and Ubuntu18.04 LTS, you can change the apache2.conf file by adding the following:
WSGIScriptAlias / [path_to_wsgi.py]
WSGIPythonPath [path_to_project]
<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
If you want to use the latest Django 2+ version, change the /etc/apache2/sites-available/000-default.conf which is the settings for HTTP port 80. (000-default.conf file can't contains WSGIPythonPath in virtualhost)
<VirtualHost *:80>
ServerAdmin webmaster#localhost
Alias /static [path_to_static_folder]
<Directory [path_to_static_folder]>
Require all granted
</Directory>
<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess [name] python-home=[path_to_virtualenv] python-path=[path_to_project]
WSGIProcessGroup [name]
WSGIScriptAlias / [path_to_project_wsgi.py]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

How to restrict an access to media files in Django on a production environment for anonymous users?

I need to restrict the access to media files in Django 1.11 on a production environment for anonymous users.
I found this snippet and it looks like what I want, but I need to use mod_wsgi with apache2. Is that possible and how could it be accomplished?
Apache config:
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
Alias /media /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-home=/home/user/myproject/myprojectenv python-path=/home/user/myproject
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py

Location of settings file for deploying static files with Django on Apache2 with mod_wsgi

I am trying to follow the tutorial on Deploying static files on Apache using mod_wsgi from the 1.6 Documentation. I already have a work in progress site deployed on Apache using mod_wsgi but I need to learn how to correctly deploy static files instead of linking to external Bootstrap CSS as I have been doing.
My issue is I have no idea where the below sample settings from the documentation is or where it should go.
Below is the relevant DjangoProject text
If, however, you have no option but to serve media files on the same
Apache VirtualHost as Django, you can set up Apache to serve some URLs
as static media, and others using the mod_wsgi interface to Django.
This example sets up Django at the site root, but explicitly serves
robots.txt, favicon.ico, any CSS file, and anything in the /static/
and /media/ URL space as a static file. All other URLs will be served
using mod_wsgi:
It then gives the below sample code which I will need to modify
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
AliasMatch ^/([^/]*\.css) /path/to/mysite.com/static/styles/$1
Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
<Directory /path/to/mysite.com/media>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Followed by some clarification instruction
If you are using a version of Apache older than 2.4, replace Require
all granted with Allow from all and also add the line Order deny,allow
above it.
There are some similarities to my below <VirtualHost> file. Should I just add it to this? Or should it be stored in another location?
<VirtualHost *:80>
ServerName phaedrus.scss.tcd.ie/bias_experiment
ServerAlias phaedrus.scss.tcd.ie
WSGIScriptAlias /bias_experiment/ /var/www/bias_experiment/src/bias_experiment/index.wsgi
Alias /static/ /var/www/bias_experiment/src/bias_experiment/static/
<Location "/static/">
Options -Indexes
</Location>
</VirtualHost>
Any help would be great.
Yes, this is supposed to go in the same place as your virtualhost configuration.