mod_wsgi setup

175 views
Skip to first unread message

tino

unread,
Nov 15, 2013, 4:34:58 AM11/15/13
to django...@googlegroups.com
Hello,

I am trying to setup a python 2.7 + Django + virtualenv + mod_wsgi environment in my centos 6.3 server to run my python application through apache.
But I am getting internal error while trying to access the application through apache. The error log shows the following.

============
[Fri Nov 15 04:20:30 2013] [error] [client 117.201.194.54] mod_wsgi (pid=20361): Target WSGI script '/var/www/html/djangosites/spark.wsgi' cannot be loaded as Python module.
[Fri Nov 15 04:20:30 2013] [error] [client 117.201.194.54] mod_wsgi (pid=20361): Exception occurred processing WSGI script '/var/www/html/djangosites/spark.wsgi'.
Fri Nov 15 04:20:30 2013] [error] [client 117.201.194.54] ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

============

It is working fine when running as a test server.

======
(virtualenv)[root@lampserver spark]# python manage.py runserver 0.0.0.0:8080
Validating models...

0 errors found
November 15, 2013 - 09:15:19
Django version 1.6, using settings 'spark.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.
====


spark.wsgi file looks like the following.

===========

 import sys
import site
import os

vepath = '/var/www/html/virtualenv/lib/python2.7/site-packages'
prev_sys_path = list(sys.path)
site.addsitedir(vepath)
sys.path.append('/var/www/html/djangosites')
new_sys_path = [p for p in sys.path if p not in prev_sys_path]
for item in new_sys_path:
    sys.path.remove(item)
sys.path[:0] = new_sys_path
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'spark.settings'
application = WSGIHandler()

==============

Entries for wsgi in apache conf looks like the following

============

[root@lampserver djangosites]# cat /etc/httpd/conf.d/django.conf 


LoadModule wsgi_module modules/mod_wsgi.so
AddHandler wsgi-script .wsgi

WSGIScriptAlias / /var/www/html/djangosites/spark.wsgi

WSGIDaemonProcess spark processes=5 threads=15 display-name=%{GROUP}

WSGIProcessGroup spark

WSGIApplicationGroup %{GLOBAL}

WSGISocketPrefix /var/run/wsgi

=================

Apache is running as user "apache".

My project files and permissions are shown below.

===========
root@lampserver djangosites]# pwd
var/www/html/djangosites
[root@lampserver djangosites]# ls
spark  spark.wsgi
[root@lampserver djangosites]# ll
total 8
drwxr-xr-x 3 apache apache 4096 Nov 15 02:38 spark
-rwxrwxrwx 1 apache apache  535 Nov 15 03:16 spark.wsgi
[root@lampserver djangosites]# cd spark/
manage.py  spark/     
[root@lampserver spark]# ll
total 8
-rwxr-xr-x 1 apache apache  248 Nov 15 02:38 manage.py
drwxr-xr-x 2 apache apache 4096 Nov 15 03:05 spark
[root@lampserver spark]# cd spark/
[root@lampserver spark]# ll
total 28
-rw-r--r-- 1 apache apache    0 Nov 15 02:38 __init__.py
-rw-r--r-- 1 apache apache  136 Nov 15 02:40 __init__.pyc
-rw-r--r-- 1 apache apache 1969 Nov 15 02:38 settings.py
-rw-r--r-- 1 apache apache 2142 Nov 15 02:40 settings.pyc
-rw-r--r-- 1 apache apache  296 Nov 15 02:38 urls.py
-rw-r--r-- 1 apache apache  416 Nov 15 02:40 urls.pyc
-rwxr-xr-x 1 apache apache  385 Nov 15 02:38 wsgi.py
-rw-r--r-- 1 apache apache  589 Nov 15 02:40 wsgi.pyc

==================

Can anybody please identify the problem with my settings?

Regards

Liam Thompson

unread,
Nov 15, 2013, 5:09:00 AM11/15/13
to django...@googlegroups.com
The "Requested setting DEBUG" says you've requested DEBUG but it's not setup. I don't think you would want DEBUG in production, so try to turn that off in the settings file.

Timothy W. Cook

unread,
Nov 15, 2013, 5:17:20 AM11/15/13
to django...@googlegroups.com
On centos 6 I had (still have) problems similar to this due to SELinux
restricting Apache from accessing the application. Even after
rechecking permissions SELinux doesn't want to allow a user to access
another users (specifically setup for the application) home
directory. You can test if this is the same problem by disabling
SELinux temporarily with:

$setenforce 0

This is really a temporary fix and exploring
http://www.crypt.gen.nz/selinux/disable_selinux.html may help.

HTH,
Tim
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3594a141-c38c-4efa-850a-be2f75dafaf7%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
MLHIM VIP Signup: http://goo.gl/22B0U
============================================
Timothy Cook, MSc +55 21 94711995
MLHIM http://www.mlhim.org
Like Us on FB: https://www.facebook.com/mlhim2
Circle us on G+: http://goo.gl/44EV5
Google Scholar: http://goo.gl/MMZ1o
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook

Timothy W. Cook

unread,
Nov 15, 2013, 5:25:35 AM11/15/13
to django...@googlegroups.com
BTW: HEre is an old tutorial regarding SELinux and WSGI apps. It may
be useful?
http://www.packtpub.com/article/selinux-secured-web-hosting-python-based-web-applications

tino

unread,
Nov 15, 2013, 8:28:04 AM11/15/13
to django...@googlegroups.com
Thank you guys for the suggestions.

But selinux is already disabled in the server.

 [root@lampserver spark]# getenforce 
Disabled

Also I tried to set 
 DEBUG = False  in settings.py. That also did not help.

Fred Stluka

unread,
Nov 15, 2013, 9:33:50 AM11/15/13
to django...@googlegroups.com
tino,

I have a working setup, using Apache mod_wsgi on AWS Linux.

Try adding the location of your virtual env to to your Apache settings.
Something like:

======================

WSGIPythonHome /var/python27/virtualenvs/hhl

======================

If that doesn't help, here are some more thoughts.

My django.wsgi file looks similar to yours.  Differences in red:

======================

import os, sys
sys.path.append('/var/www/django/hhlweb')
sys.path.append('/var/www/django/hhlweb/hhl')
sys.path.append('/var/python27/virtualenvs/hhl/lib/python2.7/site-packages/django/db/backends')
os.environ['DJANGO_SETTINGS_MODULE'] = 'hhl.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

======================

What does your wsgi.py file look like?  Mine is:

======================

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hhl.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

======================

I found these links useful when I set mine up last year:
- http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
- http://code.google.com/p/modwsgi/wiki/ConfigurationIssues
- https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi


--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

tino

unread,
Nov 15, 2013, 11:24:21 AM11/15/13
to django...@googlegroups.com
Thank you Fred for your help.

Finally I got it working. I tried different things and this is how my files looks  now.

spark.wsgi

=================

import os
import sys
sys.stdout = sys.stderr
# Add the virtual Python environment site-packages directory to the path
import site
site.addsitedir('/var/www/html/virtualenv/lib/python2.7/site-packages')

#If your project is not on your PYTHONPATH by default you can add the following
sys.path.append('/var/www/html/djangosites/spark')
os.environ['DJANGO_SETTINGS_MODULE'] = 'spark.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

==========================

Apache entries

==================

WSGIScriptAlias / /var/www/html/djangosites/spark.wsgi

WSGIDaemonProcess spark processes=5 threads=15 display-name=%{GROUP}

WSGIProcessGroup spark

WSGIApplicationGroup %{GLOBAL}

WSGISocketPrefix /var/run/wsgi

====================

On Friday, November 15, 2013 3:04:58 PM UTC+5:30, tino wrote:

Fred Stluka

unread,
Nov 15, 2013, 11:39:09 AM11/15/13
to django...@googlegroups.com
tino,

Didn't need the WSGIPythonHome setting in Apache, eh?
Interesting...


--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages