Setup django with WSGI and apache - python

I have been sold on mod_wsgi and apache rather than mod_python.
I have all the parts installed (django, apache, mod_wsgi) but have run into a problem deploying.
I am on osx 10.5 with apache 2.2 and django 1.0b2, mod_wsgi-2.3
My application is called tred.
Here are the relevant files:
httpd-vhosts (included in httpd-conf)
NameVirtualHost tred:80
ServerName tred
Alias /admin_media /usr/lib/python2.5/site-packages/django/contrib/admin/media
Order allow,deny
Allow from all
Alias /media /Users/dmg/Sites/tred/media
Order allow,deny
Allow from all
Alias / /Users/dmg/Sites/tred/
Order allow,deny
Allow from all
WSGIScriptAlias / /Users/dmg/Sites/tred/mod_wsgi-handler.wsgi
WSGIDaemonProcess tred user=dmg group=staff processes=1 threads=10
WSGIProcessGroup tred
mod_wsgi-handle.wsgi
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
os.environ['DJANGO_SETTINGS_MODULE'] = 'tred.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
When I go to http://tred I get a directory listing rather than the rendered website. I think I have followed the tutorials correctly but it is obviously not right. What can I do to make this work?

What happens if you remove the Alias / directive?

Note that Alias and WSGIScriptAlias directives do not have the same precedence. Thus, they will not be processed in file order as written. Instead, all Alias directives get precedence over WSGIScriptAlias directives. Thus, it wouldn't have mattered if the Alias for '/' appeared after WSGIScriptAlias, it would still have taken precedence.

It works. I have no idea why, but it does.
For future reference:
It works because Apache processes alias directives in order, and uses the first match. It was always hitting Alias /, which will match anything, before WSGIScriptAlias.
From the mod_alias documentation:
First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence.

try following this tutorial - http://singlas.in/5-step-tutorial-for-using-django-with-apache-and-mod_wsgi/
you are trying to host apache /var/www/ folder and the Django app both at root (/). Since Alias directive takes precedence over WSGIScriptAlias, it is rendering apache directory.
you can try to host the django app at /app. Alternatively host the /var/www/ folder at a different location like /public

Related

Apache set-up with Flask and wsgi

I have a small web application that I have built using Flask and python. With the internal server that I used for developing everything runs fine. However now I want to use apache to start using it. But it doesn`t work. Keep in mind that I have never worked with apache or web based stuff before.
I used this guide as my starting point:
http://flask.pocoo.org/docs/deploying/mod_wsgi/
right now I have my application which is in the file called "/rg/server.py" and looks like this:
app=Flask(__name__)
# all app routes...
if __name__ == '__main__':
app.run(
debug=True,
host="127.0.0.1",
port=80
)
than I have a wsgi file as "/rg/wsgi/minerva.wsgi"
import sys
sys.path.insert(0, /rg)
from server import app as minerva
and finally I have an apache config file in "etc/apach2/sites-available/minerva.com":
<VirtualHost *>
ServerName minerva.test
WSGIDaemonProcess minerva threads=10
WSGIScriptAlias / /rg/wsgi/minerva.wsgi
<Directory /rg>
WSGIProcessGroup minerva
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I updated apache with a2ensite minerva.com which succeded. Then I releaded Apache and no errors. However I cannot acces minerva.test in any way...
If I type in apache2ctl -S it does list minerva.test
I have no idea what is going wrong...
system information:
OS: debian 64bit
python 2.7
The WSGI application entry point must be called 'application' for mod_wsgi. You have:
from server import app as minerva
It should be:
from server import app as application
You aren't even getting that far though, else the line:
sys.path.insert(0, /rg)
would give a syntax error.
Going back further, instead of:
<VirtualHost *>
you should have:
<VirtualHost *:80>
and finally, if 'minerva.test' isn't actually a resolvable host, you will not get anywhere.
So fill out your question with the actual URL you are using in the browser and also indicate whether 'minerva.test' is even listed in local hosts file.
The first thing I would check would be to make sure mod_wsgi is installed and loaded by apache. If that's fine, your setup looks pretty similar to mine with a few minor differences:
I had to add WSGISocketPrefix /var/run/wsgi above my VirtualHost definitoin. See here.
I included user and group values on the WSGIDaemonProcess line.

Running two seperate Django site from one apache

I am running Ubuntu 14.04 on my notebook and I am using Django 1.6.4 (with virtualenv) together with Apache 2.4.7.
I would like to access two django projects through my local apache. Therefore, theses projects are called kleyboldt (name of a guy I am writing for) and kleyboldt2. Normally I store my web projects under /home/nick/Workspace/Web. After watching tutorial I ended up with two domains pointing to the same django project. To be sure I rewrote the config and placed my projects under /var/www.
At the beginning I wrote two files in order to configure apache.
/etc/apache/sites-available/kleyboldt.conf
WSGIPythonPath /var/www/kleyboldt:/var/www/kleyboldt/env/lib/python2.7/site-packages
<VirtualHost *:80>
WSGIScriptAlias / /var/www/kleyboldt/kleyboldt.wsgi
ServerName kleyboldt.com
Alias /static /var/www/kleyboldt/static
<Directory /var/www/kleyboldt/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
/etc/apache2/sites-available/kleyboldt2.conf
WSGIPythonPath /var/www/kleyboldt2:/var/www/kleyboldt2/env/lib/python2.7/site-packages
<VirtualHost *:80>
WSGIScriptAlias / /var/www/kleyboldt2/kleyboldt2.wsgi
ServerName kleyboldt2.com
Alias /static /var/www/kleyboldt2/static
<Directory /var/www/kleyboldt2/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Afterwards I enabled both sites via a2ensite. I checked the sites-enabled directory and the links are placed correctly. In order to redirect kleyboldt.com and kleyboldt2.com to my local apache I changed my /etc/hosts to:
127.0.0.1 localhost
127.0.1.1 mars (name of my computer)
127.0.0.1 kleyboldt.com
127.0.0.1 kleyboldt2.com
To use WSGI I wrote for each project a seperated wsgi file in the project folder:
/var/www/kleyboldt/kleyboldt.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
/var/www/kleyboldt2/kleyboldt2.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt2')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt2.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I created two independent python environments using virtualenv that are now located under /var/www/kleyboldt/env and /var/www/kleyboldt2/env.
Afterwarsd I got two django test sites when I typed both domains in the URL bar of my browser. To differ I added some settings to the settings.py files and wrote custom views.
/var/www/kleyboldt/homepage/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("kleyboldt1")
/var/www/kleyboldt2/homepage/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("kleyboldt2")
When I type localhost in my browser then I get kleyboldt2 back. When I type kleyboldt2.com in my browser then I get kleyboldt2 back too. Unfortunately I see kleyboldt2 also after typing kleyboldt.com in my browser.
Maybe the bug is to simple for me to discover :D Does anybody know a possible solution?

How to disable directory browsing for a flask application hosted using apache mod_wsgi for CNAME access from a different domain

Something weird is happening with my flask application that I have hosted using apache mod_wsgi on Ubuntu. I have a virtualhost setup with my flask app imported to it. My virtualhost is present is /etc/apache2/sites-available/ and has been enabled. The file is this:
<VirtualHost *:80>
ServerName domain1.com
WSGIDaemonProcess app user=flask group=www-data threads=5 home=/var/www/app
WSGIScriptAlias / /var/www/app/app.wsgi
<Directory /var/www/app>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
My application works perfectly fine when I access it using domain1.com. However, if I have a domain2.com with a CNAME record pointing to domain1.com, I can view the directories and files when domain2.com is accessed. Similarly, when I change the ServerName to domain2.com, the directory listing is visible from domain1.com.
I've been advised to edit the httpd.conf file and remove the Indexes entry or change it to -Indexes to turn off the directory browsing but the file is empty. I would like the app to work only from the domain specified in the ServerName and not from any other domain name pointing to it. Please advise.
The advice given is correct but I believe on Ubuntu the default virtual host is in /etc/apache2/apache2.conf or /etc/apache2/sites-available/default. what's happening is that because both domains resolve to the same ip, domain2 picks up the default Apache configuration because it doesn't have its own virtual host.
You could also prevent this by moving the document root of domain1 to a different location but its a good idea to turn off the indexes in any case.
For the second domain name, because Apache cannot find a matching VirtualHost, it will fall back to using the very first VirtualHost definition it found when parsing the configuration.
For an explanation of this in a slightly different context see:
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
Read the section 'Fallback to default VirtualHost definition'.
If you want all other domain names blocked, then change the default VirtualHost to:
<VirtualHost _default_:*>
Deny from all
</VirtualHost>

Apache 2 + mod_wsgi + WSGIScriptAlias

I am currently doing research whether Python and Django are fit for a project that I'm going to work on (so far it looks good). As a means of a test, I want to get python running on an actual server (apache2 on ubuntu), using mod_wsgi, but I just can't make it work. Here is my httpd.conf (located at /etc/apache2/httpd.conf):
WSGIScriptAlias /test/tc-test/ /var/www/stage/hello/tc-test/django.wsgi
WSGIPythonPath /var/www/stage/test/tc-test/
<Directory /var/www/stage/test/tc-test>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
My django.wsgi looks as follows:
import os
import sys
sys.path.append('/var/www/stage/test/tc-test')
os_environment['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The url for this directory will be http://stage.website.com/test/tc-test
Strangely enough, when I reboot the apache server, I keep getting 500 internal server errors. Not just for that subdirectory, but for all other subdirectories on the server. I looked at the apache error log, but for some reason no error is being logged at all. I managed to narrow down the culprits to WSGIScriptAlias and WSGIPythonPath: when I remove both of them, the 500 internal server errors disappear, and when I add any of them, they appear again. I have confirmed that mod_wsgi is working like it should, and it's probably something very simple that I'm looking over. I just can't find out what. Any help on this?
Try:
WSGIScriptAlias /test/tc-test /var/www/stage/hello/tc-test/django.wsgi
You shouldn't have trailing slash on first argument.

Django deployment problem in Apache/mod_wsgi. ImportError: Could not import settings 'site.settings'

When I'm executing
django-admin.py startproject site
it works.
But if I'm only copying site folder it doesn't work.
Why?
<VirtualHost *:80>
ServerName django.stanislavfeldman.com
# Django settings
WSGIScriptAlias / /var/www/django/wsgi_handler.py
WSGIDaemonProcess django.stanislavfeldman.com maximum-requests=200 stack-size=524288
ErrorLog /var/www/django/error.log
LogLevel warn
</VirtualHost>
wsgi_handler.py:
import os, sys
sys.path.append('/var/www/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'site.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If you have something like this in apache configs:
WSGIScriptAlias /path /base/path/devel/your_project.wsgi
And this inside your_project.wsgi:
sys.path.append('/base/path')
os.environ['DJANGO_SETTINGS_MODULE'] = 'devel.settings'
Then apache will look at /base/path/devel/settings.py. If you move or copy /base/path/devel to /base/path/production you have to edit DJANGO_SETTINGS_MODULE at your_project.wsgi pointing to 'production.settings'.
Ensure you have read:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
and also watch this presentation:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
Your problem is going to be a sys.path or permissions issue which are both covered by the above.
That you are using 'maximum-requests=200 stack-size=524288' options to WSGIDaemonProcess directive makes me question whether you have referred to the mainstream documentation as basic instructions don't tell you to use them. Instead looks like you have used some arbitrary persons blog post for how to set it up, or relying on some folklore given to you on an IRC channel. :-)
Check your python path to make sure that WSGI can reference it.
I had a problem with a symlink not being followed from the site-packages dir. Double check your apache config and symlinks as well.
This doesn't appear to be the problem in your case, but I ran smack into the same ImportError when I used the WSGIPythonPath directive (instead of the .wsgi file) to set up sys.path. That worked fine until I switched to running WSGI in daemon mode. Once you do that, you have to use the python-path argument to the WSGIDaemonProcess directive instead.

Categories

Resources