Howto configure Apache WSGI for multiple separate Django instances? - python

I have an apache instance where I have the following
WSGIPythonPath /production/somelocation/django12/lib/python2.4/site-packages/
<VirtualHost 192.168.1.1:443>
WSGIScriptAlias / /opt/project.wsgi
.....
My Django 1.5 app apache config looks like,
WSGIPythonPath /production/somelocation/django15/lib/python2.7/site-packages/
<VirtualHost 192.168.1.2:443>
....
WSGIScriptAlias / /opt/project2.wsgi
My /opt/project.wsgi looks like
import os
import sys
# django1.2 virtualenv
import site
site.addsitedir("/production/somelocation/django12/lib/python2.4/site-packages")
.....
However when I go to the site I still get my default django (1.5) instance. What am I missing ?

The other answers mention setting the python path, however using WSGIPythonPath or WSGIPythonHome are not correct. The WSGIPythonPath / WSGIPythonHome can only be set server-wide, so no different paths per virtualhost.
You would want to use the WSGIDaemonProcess python-path and home arguments to set the python path and your apps home directory per virtualhost.
Also, within your code there is no need to adjust python paths; just make sure your virtualhost config is correct.

You may need to set WSGIPythonHome since you have different Django installations.
WSGIPythonPath is used to define additional directories, but this option do not set default python installation. So probably, your default python directory also includes django (1.5) and recognize this version as the default django version. I do not know your python and django installation and configuration but this might be the reason.
Additional info for WSGIPythonHome

This is how I do with Pyramid:
<VirtualHost *:80>
Servername hackintosh
DocumentRoot "/Library/WebServer/Documents"
</VirtualHost>
<VirtualHost *:80>
ServerName modwebsocket.local
ErrorLog "/PythonProjects/MOD_WEBSOCKET/logs/error_log"
CustomLog "/PythonProjects/MOD_WEBSOCKET/logs/access_log" common
WSGIDaemonProcess pyramid-modwebsocket user=apero group=staff threads=4 python-path=/PythonProjects/MOD_WEBSOCKET/lib/python2.7/site-packages
WSGIProcessGroup pyramid-modwebsocket
WSGIScriptAlias / /PythonProjects/MOD_WEBSOCKET/wsgi/pyramid.wsgi
<Directory "/PythonProjects/MOD_WEBSOCKET/wsgi">
WSGIProcessGroup pyramid-modwebsocket
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName ai.local
ErrorLog "/PythonProjects/AI/logs/error_log"
CustomLog "/PythonProjects/AI/logs/access_log" common
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
WSGIDaemonProcess pyramid-ai user=apero group=staff threads=4 python-path=/PythonProjects/AI/lib/python2.7/site-packages
WSGIProcessGroup pyramid-wizard
WSGIScriptAlias / /PythonProjects/AI/wsgi/pyramid.wsgi
<Directory "/PythonProjects/AI/wsgi">
WSGIProcessGroup pyramid-ai
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

This topic and the typical causes are detailed in:
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
There is not enough information in your question to properly evaluate which of the problems you are encountering.

Related

how to use mod_wsgi for hosting multiple django projects under single domain?

I have multiple django projects and i want to host them under same domain
eg: example.com/one<br> example.com/two
I have searched for various solutions and found the below given link which helped me alot.
Is it possible to host multiple django projects under the same domain?
From the above reading , I get to know that I need mod_wsgi for this but I am confused that where to install this mod_wsgi - Do i need to install under every project folder (seperate for every myenv) or it should be installed only once .
Please help me in how and where to install this mod_wsgi and finally how to host multiple projects under same domain name.
Some Code Tried By Another User With Same Problem But Also Not Working
<VirtualHost *:80>
ServerAdmin admin#my_domain.com
ServerName my_domain.com
ServerAlias www.my_domain.com
DocumentRoot /var/www/my_domain.com
ErrorLog ${APACHE_LOG_DIR}/my_domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/my_domain.com_access.log combined
# site_1
Alias /site_1_project/static /var/www/my_domain.com/site_1_project/static
<Directory /var/www/my_domain.com/site_1_project/static>
Require all granted
</Directory>
Alias /site_1_project/media /var/www/my_domain.com/site_1_project/media
<Directory /var/www/my_domain.com/site_1_project/media>
Require all granted
</Directory>
<Directory /var/www/my_domain.com/site_1_project/site_1_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_1_project python-path=/var/www/my_domain.com/site_1_project python-home=/var/www/my_domain.com/site_1_project/django_env_site_1
WSGIProcessGroup site_1_project
WSGIScriptAlias /site_1_project/ /var/www/my_domain.com/site_1_project/site_1_project/wsgi.py
# site_2
Alias /site_2_project/static /var/www/my_domain.com/site_2_project/static
<Directory /var/www/my_domain.com/site_2_project/static>
Require all granted
</Directory>
Alias /site_2_project/media /var/www/my_domain.com/site_2_project/media
<Directory /var/www/my_domain.com/site_2_project/media>
Require all granted
</Directory>
<Directory /var/www/my_domain.com/site_2_project/site_2_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_2_project python-path=/var/www/my_domain.com/site_2_project python-home=/var/www/my_domain.com/site_2_project/django_env_site_2
WSGIProcessGroup site_2_project
WSGIScriptAlias /site_2_project/ /var/www/my_domain.com/site_2_project/site_2_project/wsgi.py
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =my_domain.com [OR]
#RewriteCond %{SERVER_NAME} =www.my_domain.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Installing mod_wsgi depends on what your host OS is. Check the instructions. If you're using CentOS or RedHat, I'd recommend looking at IUS Community; they provide a repository with yum installable packages for Python 3.6 and mod_wsgi. The version of mod_wsgi you install has to be compiled against the same version of Python you are running into your virtual environment.
Then you need to configure your VirtualHost properly. If you have a host at the root, it has to come last in your definition. Here's an example:
<VirtualHost *:443>
TimeOut 300
SSLEngine On
ServerName mysite.example.com
# Set to the lobal Application Group
WSGIApplicationGroup %{GLOBAL}
# Pass Authorizations through to the WSGI app for Django REST Framework Token Auth
WSGIPassAuthorization On
WSGIDaemonProcess subsite-develop-https python-home=/web/subsite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup subsite-develop-https
WSGIScriptAlias /subsite /web/subsite-develop/config/wsgi.py process-group=subsite-develop-https
<Directory /web/subsite-develop/config>
Require all granted
</Directory>
Alias /subsite/static/ /web/subsite-develop/static/
<Directory /web/subsite-develop/static>
Header always set Access-Control-Allow-Origin "*"
Require all granted
</Directory>
WSGIDaemonProcess django-mysite-develop-https python-home=/web/django-mysite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup django-mysite-develop-https
WSGIScriptAlias / /web/django-mysite-develop/config/wsgi.py process-group=django-mysite-develop-https
<Directory /web/django-mysite-develop/config>
Require all granted
</Directory>
Alias /static/ /web/django-mysite-develop/static/
<Directory /web/django-mysite-develop/static>
Header always set Access-Control-Allow-Origin "*"
Require all granted
</Directory>
Alias /media/ /var/media/mysite-www/
<Directory /var/media/mysite-www>
Require all granted
</Directory>
</VirtualHost>
This example will host one site at /subsite/ and another at the root, /. Notice that the root site comes last. It also means that you won't be able to use the route /subsite/ within the root project, since Apache will have diverted it to via the WSGIScriptAlias definition.
This is also for a site with TLS; you may have to switch the 443 to 80, and remove SSLEngine On if you're not using TLS. The WSGIPassAuthorization is for Django REST Framework tokens, you can probably remove it as well, but I've left it for a more complete example. This is for Apache 2.4+, when they switched to the Require all granted syntax.
IUS Community, if on RedHat/CentOS: https://ius.io/
I'll tell you how we did in our project. We have a single Django project with different routes. For example /players, /tablet. We hosted our project in two Docker containers. We have NGINX as our reverse proxy. NGINX redirects the request to the appropriate container based on the route. NGINX is exposed to the world. But, I'm not sure if it is useful for you or not.

Problems with deploying Flask application on Apache 2.4

I'm trying to deplay my Flask application on a RHEL7 Apache 2.4 server.
File structure is the following inside /var/www/html
/app
app.wsgi
/app
app.py
/templates
/static
In my /etc/httpd/conf/httpd.conf I have the following code to set up my project:
<VirtualHost *>
ServerName 10.65.112.75:443
WSGIDaemonProcess app user=apache group=apache threads=5 home=/var/www/html/app/app
WSGIScriptAlias / /var/html/app/app.wsgi
<Directory /var/www/html/app/app/>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Require all granted
</Directory>
Alias /static /var/www/html/app/app/static/
<Directory /var/www/html/app/app/static/>
Order deny,allow
Require all granted
</Directory>
And my app.wsgi contains the following:
#!/usr/bin/python
import sys
sys.path.insert(0, "/var/www/html/app/app/")
from app import app as application
The code for the project itself can be found in my github repository here.
I do not get any errors when trying to browse the server. It just doesnt do anything. Running my script from the terminal works, though.
Thanks for the help.
There are various things which aren't right.
You have:
WSGIScriptAlias / /var/html/app/app.wsgi
whereas it appears it should be:
WSGIScriptAlias / /var/www/html/app/app.wsgi
And:
<Directory /var/www/html/app/app/>
appears it should be:
<Directory /var/www/html/app>
Your VirtualHost definition also looks wrong. You have:
<VirtualHost *>
ServerName 10.65.112.75:443
If you really want this to be for HTTPS connections, you are missing all the SSL directives to add a SSL certificate.
ServerName would also usually be a fully qualified domain name and not an IP:PORT. The port number would usually be in the VirtualHost directive. For example, for HTTP, use:
<VirtualHost *:80>
ServerName my.host.name
where my.host.name is the full public host name for your machine which you use in the URL, not an IP address.

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!

Alternative to WSGIDaemonProcess & WSGIProcessGroup for Windows

I have installed the following on my Windows machine (everything is 64bit):
Python 3.4.3
Apache 2.4.16
mod_wsgi 4.4.13
Then I installed Django(v1.8.3) in a virtual environment using virtualenv. Using django-admin I created two project and I created two VirtualHost for that. And everything is working perfect. Now I would like to use a different environment for the second project but though the Windows doesn't support WSGIDaemonProcess and WSGIProcessGroup I don't know how to do that.
WSGIPythonPath "C:/_pythonDev/project1;C:/_pythonDev/project2;C:/_pythonDev/env1/Lib/site-packages"
<VirtualHost *:80>
ServerName one.local.com
WSGIScriptAlias / "C:/_pythonDev/project1/project1/wsgi.py"
<Directory "C:/_pythonDev/project1/project1">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName two.local.com
WSGIScriptAlias / "C:/_pythonDev/project2/project2/wsgi.py"
<Directory "C:/_pythonDev/project2/project2">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
So, If someone knows how to run Django projects in different environment please guide me.
Solution: click this link
You need to address two things.
Don't set WSGIPythonPath in the Apache configuration. Activate the Python virtual environment and set sys.path from the WSGI script file.
Change the WSGI script file so as not to use setdefault() in os.environ as that screws things up.
For reading see:
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html

Apache Conf file issue -WSGIPythonPath error

So i'm trying to deploy a simple website using django, Apache, and wsgi. I wrote my conf file using the django guide (https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/) and this previous answer (Deploying Django on Apache and WSGI) but when I restart Apache I get an syntax error on line 3 stating WSGIPythonPath cannot occur within the VirtualHost section, once removed Apache restarted successfully but still does not redirect to the appropriate django website I created even after changing the hosts files so that it redirects back to my server.
Apache Version: 2.4.7
Python Version: 2.7.6
Django Version: 1.8.3
<VirtualHost *:80>
WSGIScriptAlias / /var/www/mywebsite/mywebsite/wsgi.py
WSGIPythonPath /var/www/mywebsite
ServerAdmin my_email#gmail.com
ServerName mywebsite.com
ServerAlias www.mywebsite.com
<Directory /var/www/mywebsite/mywebsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
move the line with WSGIPythonPath to the httpd.conf location

Categories

Resources