django + wsgi + multiple sites - django

I have a django-app using sites framework and need to deploy in a single apache virtual host. I was using mod-python with PythonInterpreter and it was working fine. But mod-python is deprecated and I want to migrate to wsgi. But I can figure out how to configure this scenario using wsgi. Can anyone help me? I guess just using WSGIScriptAlias for each site is not working because it is running on same python interpreter.
Can anybody help me?

The default for WSGIApplicationGroup is %{RESOURCE} which expands to include the value of ServerName and the application mount point. Thus each distinct virtual host would be in separate sub interpreter by default.
This means that if you have multiple VirtualHost definitions with different ServerName setting then they would be distinct.
In general though, it would be better to create a distinct daemon process group for each site and delegate each to a different set of processes. This is done using WSGIDaemonProcess and WSGIProcessGroup directives.
When each site is in a separate daemon process group, then often better to set WSGIApplicationGroup to %{GLOBAL} so using main interpreter in process as that avoids some issues with third party C extension modules for Python which aren't written so as to work in sub interpreters.

Related

Exposing a WSGI app from a Docker container

I have a VM running on Google Compute Engine hosting a Flask application served by Apache/WSGI. This application has to be accessible on the Internet via www.my_application.com.
What is the best way to expose the application through WSGI when inside a Docker container?
Is there a specific docker run command to use?
Does my_application.conf need to be modified?
Anything else to know/to do?
Below is my_application.conf:
<VirtualHost *:80>
ServerName www.my_application.com
WSGIDaemonProcess my_application user=www-data group=www-data threads=5
WSGIScriptAlias / /var/www/my_application/application.wsgi
ErrorLog /var/log/my_application.log
<Directory /var/www/my_application>
WSGIProcessGroup my_application
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
This may or may not be that relevant to you, but I made a public (and open source) Docker image with all the bells and whistles that you can use to build a Python Flask web application.
It has uWSGI for running the application, Nginx to serve HTTP and Supervisord to control them, so you don't have to learn how to install and configure all those to build your Python Flask web app.
And Google Compute Engine can run Docker: https://cloud.google.com/compute/docs/containers
It seems like uWSGI with Nginx is one of the more robust (and with great performance) ways to deploy a Python web app. Here are the benchmarks: http://nichol.as/benchmark-of-python-web-servers.
There are even some template projects you can use to bootstrap your own. And also, you don't have to clone the full project or something, you can just use it as a base image.
Docker Hub: https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/
GitHub: https://github.com/tiangolo/uwsgi-nginx-flask-docker

Django with multiple sites in apache

I have the following configuration in Apache web server for one site in a virtual environment and I am not using virtualhost...
WSGIPythonHome /var/www/mapsite
WSGIPythonPath /var/www/mapsite/lib/python3.4/site-packages
WSGIScriptAlias /mapflat /var/www/mapsite/mapsite/wsgi.py
WSGIApplicationGroup %{GLOBAL}
<Location "/mapflat">
Options +Indexes +FollowSymLinks +ExecCGI
Order deny,allow
Allow from all
Allow from all
AddHandler wsgi-script .py
</Location>
My question: if I were to have another site in another virtual environment in future, do I append it to WSPythonHome with : or repeat the whole thing?
Hopefully,This link should be helpful for you.
Deploying multiple django apps on Apache with mod_wsgi
If it still doesn't help.Please comment. :-)
At your query regarding the executing mod_wsgi as a daemon mode-:
It is for the sake of performance.
Check here
Kindly revert for more queries :-)
At your query for windows.
Thanks for asking
Daemons-
They are the processes which run in the background and are not interactive. They have no controlling terminal.
They perform certain actions at predefined times or in response to certain events. In *NIX, the names of daemons end in d.
In context of windows ,Daemonss becomes services.
So in context of windows,You need to install apache as a service.Detailed description is here.
Process of implementation may be slightly different but actual intent is exactly same-The optimization.
cheers :-)

Django project doesn't show up with Apache and mod_wsgi

I've installed Apache and mod_wsgi on windows xp service pack 3 and added these line to my httpd.conf :
WSGIScriptAlias / "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo/wsgi.py"
WSGIPythonPath "C:/Documents and Settings/X/My Documents/Downloads/Foo"
<Directory "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
but when I open localhost on my firefox, it shows Apache's It Works! message, what should I do to run my project on localhost ?
EDIT :
I checked and recognized that my project's path is not included in PYTHONPATH. Isn't the line WSGIPythonPath ... expected to add the address to PYTHONPATH ?
Alright, so my setup is in linux so this is not tested on windows, but:
I did not see your LoadModule statement
File: httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so
modwsgi wont work without that.
Also: the your grant statement seems a bit suspicious.
In the wsgi configuration guide suggests using a Directory directive for allowing this access to your mod_wsgi application.
<Directory "C:/Documents and Settings/X/My Documents/Downloads/Foo/Foo/">
Order allow,deny
Allow from all
</Directory>
Finally:
Make your life easy down the road.
configure apache in worker mode
configure mod_wsgi in daemon mode.
profit
Might I suggest watching this PyCon talk Making Apache suck less for hosting Python web applications from 'the-man' Graham. I wish I knew all of that stuff years ago.
Note: To figure out if you have apache in mpm worker mode.
httpd.exe -V
look for the "Server MPM" value of worker.
Django runs on port 8000 so you'll want to do two things. First, you need to run the server by entering into your console python manage.py runserver. Second, you need to direct your browser to localhost:8000.
As an aside, you don't need Apache to run a simple, local development environment. Django has its own server built in that you can leverage.

Can I serve one django app from 2 different apache vhosts?

I've got a django app that's currently available at dev.mydomain.com, and I'm about to move it to clientsdomain.com. I'm on Ubuntu so I'll run a2dissite dev.mydomain.com and then a2ensite clientsdomain.com.
My vhost files are identical except for the server name -
<VirtualHost 8.8.8.4>
ServerName dev.mydomain.com
#....
</virtualHost>
and
<VirtualHost 8.8.8.4>
ServerName clientdomain.com
#....
</virtualHost>
(obviously that's not my ip address)
I just want to know if I actually have to take down the dev vhost before I run my app from the live vhost. Can I run them both together? Are there any risks to having them up at the same time (if that's even possible).
Each Django application will serve requests coming in to its corresponding VirtualHost. So no, in theory they can run in parallel.
However, you don't get into much detail about your setup. For example, are they backed by the same database? In this case, you do realize the problem, right?

I need help on configuring mod_wsgi and Django

Apache & mod_wsgi are configured correctly (I've created a hello
world .html apache file and a hello world mod_wsgi application with
no problems). I now need my Django app to work with my django.wsgi
file. What makes me think that it's not recognizing my wsgi file is that I
went into my django.wsgi file I created and completely deleted all of
the code in the file and restarted Apache and it still gives me the
same page (a listing of the files from Django app, not my actual
Django application. Configuring Apache and mod_wsgi went really well
but I'm at a loss of how to fix this. Here are some details:
Here is my current django.wsgi file:
import os
import sys
sys.path.append('/srv/www/duckling.org/store/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'store.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I've tried a few different versions of the django.wsgi file
(including a version like the one over at http://www.djangoproject.com/).
This version of my wsgi is from here:
http://library.linode.com/frameworks/django-apache-mod-wsgi/ubuntu-10...
Also, here is my vhost apache configuration file below. I think these
are the main files that are suppose to do the job for me. Let me know if
you see any errors in what I'm doing and what else I might do to fix
this. The django app runs fine on the django's built-in development
server so I'm thinking it might have something with my paths.
No errors in my apache error.log file as well. It's acting as there's
no problem at all, which is not the case...the project isn't loading,
like I said just a listing of my files and directories of my Django
project. Here is my apache config file:
<VirtualHost 1.2.3.4:80>
ServerAdmin hi#duckling.org
ServerName duckling.org
ServerAlias www.duckling.org
DocumentRoot /srv/www/duckling.org/store/
<Directory /srv/www/duckling.org/store/>
Order Allow,Deny
Allow from all
</Directory>
Alias /static/ /srv/www/duckling.org/store/static/
<Directory /srv/www/duckling.org/store/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias store/ /srv/www/duckling.org/store/wsgi-scripts/django.wsgi
<Directory /srv/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And here are versions of the stack that I'm using, I saw over at the
mod_wsgi site that you all would like the versions of what I'm using
on the server:
Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.5 with Suhosin-Patch
mod_python/3.3.1 Python/2.6.5 mod_wsgi/2.8
thanks,
j.
For a start, you should definitely not keep your Django files under your DocumentRoot. There's no need for them to be there, and it's a potential security risk - as you've seen, your current misconfiguration allows Apache to serve up your files directly: an attacker could guess that and download your settings.py, complete with your database password.
So, get rid of that DocumentRoot directive completely, as well as the first Directory section which allows direct access to /srv/www/duckling.org/store/. (You probably don't need the one serving up /srv/www/wsgi-scripts either.) That should make things a bit better.
By the way, this configuration will serve your website under duckling.org/store - is that what you want? If you want it under the root, you should just use:
WSGIScriptAlias / /srv/www/duckling.org/store/wsgi-scripts/django.wsgi