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

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>

Related

Apache - Hosting two Django projects issue - One project is very slow / unaccessible

I'm hosting two Django projects on my Ubuntu 20.04 server using Apache, with two different domains.
djangoproject1 is hosted on example1.com:8000
djangoproject2 is hosted on example2.com:8000
However I'm having a strange issue where only one of the two is accessible at a time. Both sites work perfectly when the other one is disabled (using "a2dissite"). But when both are enabled, only one will work and the other one loads forever until time out.
djangoproject1.conf
<VirtualHost *:8000>
ServerAdmin webmaster#localhost
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/djangoproject1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /var/www/djangoproject1/static
<Directory /var/www/djangoproject1/static>
Require all granted
</Directory>
Alias /static /var/www/djangoproject1/media
<Directory /var/www/djangoproject1/media>
Require all granted
</Directory>
<Directory /var/www/djangoproject1/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess djangoproject1 python-home=/var/www/djangoproject1/venv python-path=/var/www/djangoproject1
WSGIProcessGroup djangoproject1
WSGIScriptAlias / /var/www/djangoproject1/project/wsgi.py
</VirtualHost>
djangoproject2.conf (similar to djangoproject1.conf)
<VirtualHost *:8000>
ServerAdmin webmaster#localhost
ServerName example2.com
ServerAlias www.example2.com
DocumentRoot /var/www/djangoproject2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /var/www/djangoproject2/static
<Directory /var/www/djangoproject2/static>
Require all granted
</Directory>
Alias /static /var/www/djangoproject2/media
<Directory /var/www/djangoproject2/media>
Require all granted
</Directory>
<Directory /var/www/djangoproject2/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess djangoproject2 python-home=/var/www/djangoproject2/venv python-path=/var/www/djangoproject2
WSGIProcessGroup djangoproject2
WSGIScriptAlias / /var/www/djangoproject2/project/wsgi.py
</VirtualHost>
The apache logs shows the following error multiple times:
(11)Resource temporarily unavailable: mod_wsgi (pid=1498191): Couldn't create worker thread 6 in daemon process 'djangoproject2'.
I would highly appreciate some help with this issue as I have no idea what's causing it and I can't find any solutions to this problem. Any suggestions or advice is welcome.

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)

Django Apache DocumentRoot?

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

Python Djangocms Install App WSGI vs 8000 port

I follow the tutorial http://docs.django-cms.org/en/develop/introduction/plugins.html, when I install the Polls App as indicated it is visible on 8000 port But not in WSGI/Apache mode (error message no module polls). To see the APP polls I need to copy Polls application files in the default root directory. Idem with the Aldryn Blog News. I guess I have to specify some more PATH in wsgi mode to help Python to find the modules. Where and how to do that in my Virtualenv, to be also effective when I deploy all the stuff on a remote platform ?
Thanks for any help
Thank you for your interest. IMHO I don't think the problem is on APACHE configuration, everything is OK and DjangoCMS works without polls. Herinafter the conf file, the domain is a local virtual one.
DocumentRoot "/var/www/djangocms"
WSGIScriptAlias / /var/www/djangocms/default/wsgi.py
ServerName djangocms.net
Alias /static/ /var/www/djangocms/default/static/
Options +ExecCGI
Order Allow,Deny
Allow from All
ErrorLog "logs/errordjangocms_log"
LogLevel error
Marcel
Here is a working config i use
<VirtualHost *:80>
ServerName iot.mydomain.com
ServerAlias iot
WSGIDaemonProcess iot.local python-path=/home/name/PycharmProjects/iotdata
WSGIProcessGroup iot.local
WSGIScriptAlias / /home/name/PycharmProjects/iotdata/iotdata/wsgi.py process-group=iot.local
#WSGIPythonPath /home/name/PycharmProjects/iotdata
Alias /static/ /home/name/PycharmProjects/iotdata/static/
<Directory /home/name/PycharmProjects/iotdata>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/name/PycharmProjects/iotdata/static>
Require all granted
</Directory>
</VirtualHost>

Deploying Multiple Django projects on apache with wsgi

I am trying to give a Domain name and run multiple django projects on my apache
<VirtualHost first.site.com:80>
ServerName first.site.com
WSGIDaemonProcess first
WSGIScriptAlias / /opt/project/first/first/wsgi.py process-group=first application-group=%{GLOBAL}
ErrorLog ${APACHE_LOG_DIR}/error.log
<Directory /opt/project/first/first>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost second.site.com:80>
ServerName second.site.com
WSGIDaemonProcess second
WSGIScriptAlias / /opt/project/second/second/wsgi.py process-group=second application-group=%{GLOBAL}
ErrorLog ${APACHE_LOG_DIR}/error.log
<Directory /opt/project/second/second>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
but, second website don't work.
You don't have permission to access / on this server.
Any advice is much appreciated.
Thanks
Are all directories from / down to the directory where the WSGI script files are readable to the Apache user? That is the main reason why you get this error.
Updating Virtual Host Settings from Apache 2.2 to Apache 2.4
Apache 2.2
Order allow,deny
Allow from all
Apache 2.4
Require all granted