Django with multiple sites in apache - django

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 :-)

Related

Django user login works with dev server but not with apache2 on bitnami djangostack

I have worked through the Learning Website Development with DJango book, which takes you through the creation of a basic bookmark-sharing site, and this all works as described in the book as long as I run it on the Django development server. I am now trying to set it up using apache on a virtual machine running Ubuntu 14.04 on which I have installed the bitnami django stack (https://bitnami.com/stack/django).
I had to modify httpd-app.conf, and settings.py to take account of the fact that the project files are not in the top-level folder for the project (which the development server appears to take account of automatically), and I can access individual pages of the site.
The site includes a simple user login system through django.contrib.auth which means that a user has to login before being able to access some pages, and this works fine on the development server, but it does not appear to be working with apache and I am just returned back to the same page after hitting the submit button on the login page. I did find one reference to this via a Google search, but there was no solution provided.
The django documentation does include information on authorization (https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/apache-auth/) but a) this appears to refer to blocking access to site folders rather than authorization for individual views (which is what the application from the book does), and b) I have made the changes that are described there, and it appears to make no difference.
I have also gone through many web pages unearthed via google that refer to user authorization, but most of these refer to earlier versions of django and reference functions or settings that do not appear to be in django 1.7 (which I am using) or don't directly address my problem. After having spent quite a few hours on this, as a django newbie I am pretty thoroughly confused. I think it must be a trivial problem, and am pretty sure that there is a combination of settings that will solve it, but I just can't seem to figure it out, and any help or links to tutorials / guides on how to do so would be much appreciated. Most of the training materials I can find for django all use the built-in development server (not unreasonably) and even when they refer to deployment on a production server basically just say it's outside their scope and don't provide anything of use.
I am posting the what I think are the relevant files below:
httpd-app.conf (from the django project conf folder):
<IfDefine !IS_DJANGOSTACK_LOADED>
Define IS_DJANGOSTACK_LOADED
WSGIDaemonProcess wsgi-djangostack processes=2 threads=15 display-name=%{GROUP}
</IfDefine>
<Directory "/home/george/djangostack-1.7.4-0/apps/django/django_projects/django_webtest_02/django_webtest_02">
Options +MultiViews
AllowOverride All
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
WSGIProcessGroup wsgi-djangostack
WSGIApplicationGroup %{GLOBAL}
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
Alias /static "/home/george/djangostack-1.7.4-0/apps/django/lib/python2.7/site-packages/django/contrib/admin/static"
WSGIScriptAlias /django_webtest_02 '/home/george/djangostack-1.7.4-0/apps/django/django_projects/django_webtest_02/django_webtest_02/wsgi.py'
httpd-vhosts.conf
<VirtualHost *:8080>
ServerName djangostack.example.com
ServerAlias www.djangostack.example.com
DocumentRoot "/home/george/djangostack-1.7.4-0/apps/django/django_projects/django_webtest_02/django_webtest_02"
Include "/home/george/djangostack-1.7.4-0/apps/django/django_projects/django_webtest_02/conf/httpd-app.conf"
</VirtualHost>
<VirtualHost *:8443>
ServerName djangostack.example.com
ServerAlias www.djangostack.example.com
DocumentRoot "/home/george/djangostack-1.7.4-0/apps/django/django_projects/django_webtest_02/django_webtest_02"
SSLEngine on
SSLCertificateFile "/home/george/djangostack-1.7.4-0/apps/django/django_projects/django_webtest_02/conf/certs/server.crt"
SSLCertificateKeyFile "/home/george/djangostack-1.7.4-0/apps/django/django_projects/django_webtest_02/conf/certs/server.key"
Include "/home/george/djangostack-1.7.4-0/apps/django/django_projects/django_webtest_02/conf/httpd-app.conf"
</VirtualHost>
From the apache httpd.conf file (the file is very long to post in its entirety, and so I am showing here just the final section where I have added content based on the various web sites I have looked at):
Include "conf/deflate.conf"
Include conf/pagespeed.conf
Include conf/pagespeed_libraries.conf
WSGIScriptAlias / /home/george/.virtualenvs/django_webtest_02/wsgi.py
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /home/george/djangostack-1.7.4-0/python
# virtualenv
WSGIPythonPath /home/george/.virtualenvs/django_webtest_02/lib/python2.7/site-packages
WSGIPassAuthorization On
# The following lines prevent .user.ini PHP settings files from being viewed by Web clients.
<Files ".user.ini">
<IfVersion < 2.3 >
Order allow,deny
Deny from all
</IfVersion>
<IfVersion >= 2.3>
Require all denied
</IfVersion>
</Files>
# Security
ServerSignature Off
ServerTokens Prod
TraceEnable Off
Include "/home/george/djangostack-1.7.4-0/apache2/conf/ssi.conf"
Include "/home/george/djangostack-1.7.4-0/apache2/conf/bitnami/bitnami.conf"
Include "/home/george/djangostack-1.7.4-0/apps/django/conf/django.conf"
A final point is that I am using virtualenv but have followed the instructions here (https://community.bitnami.com/t/how-to-get-virtualenv-working-on-djangostack/765) and as far as I can tell, there are not any problems with this.
Any help or guidance would be greatly appreciated.
I had been using MySQL Workbench to look at the users table and that looked OK, which suggested that the database was functioning as expected, BUT when I tried to do a migration, it came up with an error message indicating that python-mysqldb was not installed. The bitnami django stack includes mysql but a couple of comments I found online suggest that python-mysqldb is not included by default, so I installed it with pip.
I had previously managed to create several users through my django app, so I'm not sure I understand how I did that if python-mysqldb was required but missing.
I still couldn't login though, so I checked the Apache error log (which I should have done before) and it said that the user that I had been trying to logon with was not authorized, and so I cleared the users table from the database and created some new users, and it let me login with one of the new users without a problem.
This underlines for me the value of just having someone to bounce things off - I could have spent another day (or more) trying to figure it out, but the comments got me doing something different and looking at the problem a different way, which got me to the solution. Thanks again.

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.

Apache Permission for a bat file w/ Django

I have seen a lot of similar questions, but still i am failing. I have win XP, Apache 2.2, Django 1.4, Python 2.7 and mod_wsgi 3.3. All i am trying to do is when you hit the page the bat file executes and which echoes hi. This works when i run it on the django internal dev server. But when i got to Apache, it fails and in the error.log i get the message
The system cannot write to the specified device.
The system cannot write to the specified device.
The system cannot write to the specified device.
The system cannot write to the specified device.
The system cannot write to the specified device.
I have not seen that error in many other places. Most people seem to get "Permission denied". I still think the permissions are wrong. So when i run it is the django internal server i get in the console:
Validating models...
0 errors found
Django version 1.4, using settings 'testsite.settings'
Development server is running at http:`//127.0.0.1:8000/
Quit the server with CTRL-BREAK.
C:\Sites\cprm>echo hi
hi
[18/Sep/2012 14:58:45] "GET / HTTP/1.1" 200 63
That seems fine. The only thing throwing me off that I'm running from /testsite and not /cprm. I could just cd .. before. Anyway, so since in the internal server it is writing to the console, i guess i need to do the equivalent in apache. I'm not sure where the apache console is. I tried adding permission to the apache log file as that is where the error gets generated. I'm not sure that is equivalent to the regular console.
My Apache file looks like this (ignore back ticks):
<`VirtualHost *>
ServerName http://example.com:80
WSGIScriptAlias /cprm "C:/sites/cprm/wsgi.py"
WSGIScriptAlias /testsite "C:/sites/testsite/wsgi.py"
<Directory "C:/sites">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/logs">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
<`/VirtualHost>
My view just looks like this:
from django.http import HttpResponse
import datetime, os
def home(request):
os.system('C:/Sites/testsite/testsite/test.bat')
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
It's basically just a quick test I tried doing.
EDIT
Hi pacha. Thanks for the response. I have done some reading on the topic now. So I made the changes you indicated. My wsgi.py file looks like such:
import os, sys
path = 'C:/Sites/testsite'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings")
sys.stdout = sys.stderr
print 'hi'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Then when i go to error.log, i do in fact see 'hi' printed. So that is good. However, the test.bat file w/ the echo hello does not execute and i still get the same message. And thanks for the permission advice. I am now no longer using virtual. I am not ready for that yet. Any other ideas for what I could try?
Things to check
Permissions what are the user permissions on the file
C:/Sites/testsite/testsite/test.bat?
Are they read for the same user that is running the Apache Process?
Have you installed python for all logged in users not just for?
Have you reviewed your Apache conf directives?
User/Group: The name (or #number) of the user/group to run httpd as.
It is usually good practice to create a dedicated user and group for
running httpd, as with most system services.
User carlos
Group Administrators
Does that same user have permissions to read the file on the file system?
<Directory "C:/Sites/testsite/testsite/">
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>
Your problem seems to be related to how the standard output is handled. WSGI disables standard output by default. That's why your program works fine in the development server (which redirects the output to the terminal where the server was launched) but doesn't work with Apache.
You can enable stdout in WSGI using a configuration directive that you put in your virtual host conf file (together with the other WSGI directives):
WSGIRestrictStdout Off
However, take into account that Apache isn't associated to any terminal. If you want to see the output of your program then one thing you can do is to redirect the standard output to the standard error stream by adding this to your WSGI file:
sys.stdout = sys.stderr
Restart Apache an you should see any output of your application (or any child process, as it is your case) in the error.log file of Apache.
Note: Don't add the Apache log files directory to your virtual host configuration. You aren't granting permissions to Apache over those files but allowing anyone to see them, which is as you can imagine a non-trivial security risk.
Update
Avoiding permission problems
You may want to try to use WSGI in daemon mode. That way the web application can be executed under your username (much like when using the internal development server). In your case, the configuration directives should be something along the lines:
WSGIDaemonProcess site1 user=<USERNAME> group=<GROUP> processes=1 threads=1
WSGIScriptAlias /testsite "C:/sites/testsite/wsgi.py"
<Directory "C:/sites/testsite/">
WSGIProcessGroup site1
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
You have to substitute <USERNAME> and <GROUP> by your username and group in your Windows machine (it may work by just setting the username though). This is usually a nice way of avoiding permission issues since your application will be executed in a very similar way to how it is executed by the development server. (Notice that we're using just one process and one thread just to make Apache behave even more closely to how the development server does. You surely want to change those values in production to higher values).
You can check the daemon mode documentation here
Executing your script
To call external program from Python code, the module subprocess is usually preferred over os.system calls. The former allows you to have much more control over how the new process has to be executed whereas the later just passes the call to the operating system.
You can try the subprocess.check_output function. It executes a script/program and allows you to capture its output:
print subprocess.check_output(['C:\Sites\testsite\testsite\test.bat'], shell=True)
It has the additional advantage that you can capture the output in a string variable. (Btw, you may or may not need the shell=True param in Windows when calling a bat script, I'm not sure about that).

django + wsgi + multiple sites

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.

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