Multiple sites using Django and mod_wsgi on Apache - django

I'm currently using two Django applications (say A & B) hosted on the same domain (but being served on different ports) through Apache. I believe my setup is correct, but I'm randomly getting 500s on both the sites. The 500 on A (say for example) occurs mostly after a request has been served on B (and vice versa).
Upon inspecting the error log (of say A for instance), I see that A's wsgi module is trying to access B's settings.py file (which obviously doesn't happen to be there, since the project paths are different) [and this does happen the other way too, B's wsgi raises an exception complaining of missing A's settings.py file]. I'm not sure why they'd look for the other settings file, the imports (for settings.py) on all views are specific to the respective projects.
Here's my setup:
A is being served on port 8080, B is being served on port 80.
VirtualHost:
<VirtualHost *:8080>
ServerAdmin x#x.net
ServerName string1
Alias /static/ /home/PATH_TO_PROJECT_A/static/
<Directory /home/PATH_TO_PROJECT_A/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /home/PATH_TO_PROJECT_A/wsgi.py
<Directory /home/PATH_TO_PROJECT_A>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
LogLevel warn
ErrorLog /SOME_PATH/errorA.log
CustomLog /SOME_PATH/accessA.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName string1
ServerAdmin x#x.net
Alias /APP_B/static/ /home/PATH_TO_PROJECT_B/static/
<Directory /home/PATH_TO_PROJECT_B/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /APP_B /home/PATH_TO_PROJECT_B/wsgi.py/
<Directory /home/PATH_TO_PROJECT_B>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
ErrorLog /home/SOME_PATH/error2.log
CustomLog /home/SOME_PATH/access2.log combined
# All other files on B:80 (other than /APP_B are served normally
DocumentRoot /home/foo/public_html/xyz/public
</VirtualHost>
ports.conf:
NameVirtualHost *:8080
Listen 8080
Listen 80
<IfModule mod_ssl.c>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
wsgi.py on 'A':
import os, sys
sys.path.append('home/PATH_TO_PROJECT_A') #1
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_A.settings") #2
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
And exactly the same on B, with changes to line #1 and #2.
Error I get from the error.log say for instance from A:
[Sun Aug 26 17:01:49 2012] [error] [client x] Traceback (most recent call last):
[Sun Aug 26 17:01:49 2012] [error] [client x] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
[Sun Aug 26 17:01:49 2012] [error] [client x] self.load_middleware()
[Sun Aug 26 17:01:49 2012] [error] [client x] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
[Sun Aug 26 17:01:49 2012] [error] [client x] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Sun Aug 26 17:01:49 2012] [error] [client x] File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
[Sun Aug 26 17:01:49 2012] [error] [client x] self._setup()
[Sun Aug 26 17:01:49 2012] [error] [client x] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[Sun Aug 26 17:01:49 2012] [error] [client x] self._wrapped = Settings(settings_module)
[Sun Aug 26 17:01:49 2012] [error] [client x] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
[Sun Aug 26 17:01:49 2012] [error] [client x] raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Sun Aug 26 17:01:49 2012] [error] [client x] ImportError: Could not import settings 'PROJECT_B.settings' (Is it on sys.path?): No module named PROJECT_B.settings
As you see A's error log complains that B's settings.py is missing. Please shed some light, I've no idea what's wrong. I don't see why one app would look for the other's settings.py file to import?
Both the applications work and execute as expected, but they do break at random requests serving the 500 (which gets cleared if I refresh again).
Thank you!

Django breaks the geneated wsgi.py for running multiple Django instances in same process in different sub interpreters. Either use mod_wsgi daemon mode and delegate each to a separate daemon process group, is better anyway, or change:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_A.settings")
to:
os.environ["DJANGO_SETTINGS_MODULE"] = "PROJECT_A.settings"
Similarly for other wsgi.py file.

Related

Truncated or oversized response headers in daemon mode

I've a weird error in a flask website.
Important : The first page (a form) is running, but when I submit, I've a 505.
I try severals things thaks to StackOverflow :), but I cannot find the solution.
Here the log :
Mon Feb 25 09:17:37.863313 2019] [wsgi:info] [pid 30874:tid 140525377849088] [remote 192.168.56.1:61092] mod_wsgi (pid=30874, process='flask_my_app', application=''): Loading WSGI script '/var/www/my_app/my_app/flask_my_app/flask_my_app.wsgi'.
[Mon Feb 25 09:17:38.191855 2019] [wsgi:error] [pid 30874:tid 140525377849088] /home/vagrant/.virtualenvs/my_app/lib/python2.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
[Mon Feb 25 09:17:38.191899 2019] [wsgi:error] [pid 30874:tid 140525377849088] """)
[Mon Feb 25 09:17:44.995445 2019] [wsgi:error] [pid 30781:tid 140525239768832] [client 192.168.56.1:3595] Truncated or oversized response headers received from daemon process 'flask_my_app': /var/www/my_app/my_app/flask_my_app/flask_my_app.wsgi, referer: http://vm.my_app/
==> /var/log/apache2/error.log <==
[Mon Feb 25 09:17:45.562808 2019] [core:notice] [pid 30767:tid 140525508945792] AH00051: child pid 30874 exit signal Segmentation fault (11), possible coredump in /etc/apache2
==> /var/log/apache2/my_app-error.log <==
[Mon Feb 25 09:17:45.589899 2019] [wsgi:info] [pid 30956:tid 140525508945792] mod_wsgi (pid=30956): Attach interpreter ''.
[Mon Feb 25 09:17:45.591070 2019] [wsgi:info] [pid 30956:tid 140525508945792] mod_wsgi (pid=30956): Adding '/var/www/my_app/my_app' to path.
[Mon Feb 25 09:17:45.595458 2019] [wsgi:info] [pid 30956:tid 140525508945792] mod_wsgi (pid=30956): Adding '/home/vagrant/.virtualenvs/my_app/lib/python2.7/site-packages' to path.
[Mon Feb 25 09:17:45.608611 2019] [wsgi:info] [pid 30956:tid 140525377849088] [remote 192.168.56.1:61092] mod_wsgi (pid=30956, process='flask_my_app', application=''): Loading WSGI script '/var/www/my_app/my_app/flask_my_app/flask_my_app.wsgi'.
[Mon Feb 25 09:17:45.916197 2019] [wsgi:error] [pid 30956:tid 140525377849088] /home/vagrant/.virtualenvs/my_app/lib/python2.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
[Mon Feb 25 09:17:45.916224 2019] [wsgi:error] [pid 30956:tid 140525377849088] """)
My Virtual Host in Apache 2.7 is here :
<VirtualHost *:80>
ServerName vm.my_app
ServerAlias vm.my_app
DocumentRoot /var/www/my_app/my_app
LogLevel info
WSGIDaemonProcess flask_my_app python-path=/var/www/my_app/my_app:/home/vagrant/.virtualenvs/my_app/lib/python2.7/site-packages display-name=%{GROUP}
WSGIProcessGroup flask_my_app
WSGIScriptAlias / /var/www/my_app/my_app/flask_my_app/flask_my_app.wsgi
WSGIApplicationGroup %{GLOBAL}
#Alias /favicon.ico /var/www/my_app/my_app/static/favicon.ico
Alias /site_media/ /var/www/my_app/my_app/site_media/
Alias /media/ /var/www/my_app/my_app/media/
Alias /static/ /var/www/my_app/my_app/static/
AliasMatch /([/]*\.css) /var/www/my_app/my_app/static/css/$1
<Directory /var/www/my_app/my_app>
Order allow,deny
Allow from all
Require all granted
</Directory>
<Directory /var/www/my_app/my_app/flask_my_app>
Order allow,deny
Allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/my_app-error.log
CustomLog ${APACHE_LOG_DIR}/my_app-access.log combined
</VirtualHost>
The python version is 2.7.12, and I use "virtual env wrapper".
The version of the wsgi module is old I think :
lrwxrwxrwx 1 root root 15 mai 3 2016 mod_wsgi.so -> mod_wsgi.so-2.7*
-rwxrwxrwx 1 root root 223K mai 3 2016 mod_wsgi.so-2.7*
The wsgi file is "classic", and the requirements.txt file :
$ cat ../requirements.txt
click==6.7
Flask==1.0.2
Flask-Assets==0.12
Flask-Caching==1.4.0
Flask-Login==0.4.1
Flask-SQLAlchemy==2.3.2
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
pbr==5.1.1
psycopg2==2.7.5
six==1.12.0
SQLAlchemy==1.2.8
stevedore==1.30.0
webassets==0.12.1
Werkzeug==0.14.1
WTForms==2.2.1
Can anobody helps me ?
Thanks a lot
F.
It's maybe useful to others, that my issue was fixed by desactivating the SSL support !!!, in postgresql.conf :
80 ssl = off # (change requires restart)
the log :
==> /var/log/postgresql/postgresql-9.6-main.log <==
2019-03-07 15:28:28.573 CET [5346] [unknown]#[unknown] LOG: could not accept SSL connection: EOF detected

Error 500 and other website's content

I have some small django projects working on the same server with apache and mod_wsgi. I installed another one project with virtualenvironment and set settings for it in Apache conf file:
<VirtualHost XXX.XXX.XXX.XXX:81 >
ServerName mywebsite.com
CustomLog /var/www/httpd-logs/mywebsite.com.access.log combined
DocumentRoot /var/www/empirik/data/www/mywebsite.com
ErrorLog /var/www/httpd-logs/mywebsite.com.error.log
ServerAlias www.mywebsite.com
SuexecUserGroup empirik empirik
WSGIScriptAlias / /var/www/empirik/data/www/mywebsite.com/myproject/wsgi.py
WSGIDaemonProcess mywebsite.com python-path=/var/www/empirik/data/www/mywebsite.com/env/lib/python2.7/site-packages
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
Order deny,allow
Allow from All
</Directory>
<Directory /var/www/empirik/data/www/mywebsite.com/myproject/static>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
When I try to load website server returns 500 error or another website's content in about 50% times and there are some strange errors in apache log files that I can't understand because it tries to load another website's project settings:
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] mod_wsgi (pid=32361): Exception occurred processing WSGI script '/var/www/empirik/data/www/mywebsite.com/myproject/wsgi.py'.
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] Traceback (most recent call last):
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self.load_middleware()
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self._setup()
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self._wrapped = Settings(settings_module)
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] ImportError: Could not import settings 'anotherproject.settings' (Is it on sys.path?): No module named anotherproject.settings
Another question is why it still uses default system python but not from virtualenv? Please help!
You cannot use different python executeables under the same apache due to the fact that WSGIPythonHome is set for all virtual hosts globally (see docs). If WSGIPythonHome is not set - mod_wsgi will use system python.
It seems like you have not set up wsgi.py correctly. Your goal in wsgi.py is to manipulate sys.path correctly so that all that your project needs is available. Here's what helped me when I had same problems: apache server not using proper virtualenv with WSGI setting.
Hope that helps.
Your configuration is missing:
WSGIProcessGroup mywebsite.com
Without that, your WSGI application isn't even going to be delegated to the daemon process group. You can check with:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Embedded_Or_Daemon_Mode
Once you have daemon mode being used properly, so long as you are using mod_wsgi 3.4, you can set Python home for that specific daemon process group using:
WSGIDaemonProcess mywebsite.com python-home=/var/www/empirik/data/www/mywebsite.com/env
That way you can simply point at the root of the Python virtual environment and it will be picked up.
You still need though to specify the python-path option as well to the parent directory of your Django project so it and the settings module can be found, which is the problem you are having. Thus likely you want:
WSGIDaemonProcess mywebsite.com \
python-home=/var/www/empirik/data/www/mywebsite.com/env \
python-path=/var/www/empirik/data/www/mywebsite.com
Now if running only that site in the daemon process group, set:
WSGIApplicationGroup %{GLOBAL}
to avoid issues with C extension modules that don't work in sub interpreters.
To be safer, instead of:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
Order deny,allow
Allow from All
</Directory>
you should use:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
<Files wsgi.py>
Order deny,allow
Allow from All
</Files>
</Directory>
That way if screw up Apache configuration, less risk of someone downloading your code and settings file.
And where you have:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject/static>
Order deny,allow
Allow from all
</Directory>
you would appear to be missing the corresponding:
Alias /media /var/www/empirik/data/www/mywebsite.com/myproject/static
If you don't have an Alias for static media directory, Apache will not serve up files there.
Finally, since you want to force daemon mode and want to avoid embedded mode, set:
WSGIRestrictedEmbedded On
That way if you screw up configuration and things run in embedded mode by mistake as you currently are, you will get an error.

Apache not getting access to virtual environment set up for Django project

I created a virtualenv named 'pyapps' and installed pinax and django in it.I've installed apache 2 and mod_wsgi.I created a directory named 'apache' inside my django project(testproject) and put 'django.wsgi' file inside that directory.Here is the content of my wsgi file:
import os
import sys
# put the Django project on sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
os.environ['DJANGO_SETTINGS_MODULE'] = 'textpisodes.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Then I created a directory '/check/www' and put my project folder and pyapps folder inside '/check/www'.I chmoded 777 on /check/www.
Finally I created a virtual host,it's content is as follows:
<VirtualHost *:80>
ServerAdmin myemail
ServerName djangoserver
WSGIDaemonProcess textpisodes user=rajat threads=10 python-path=/check/www
WSGIProcessGroup textpisodes
WSGIScriptAlias / /check/www/textpisodes/apache/django.wsgi
<Directory /check/www/textpisodes/apache>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
Now when I try to access 'http://djangoserver',all I'm getting is Internal Server Error.Here are the contents of my apache log file.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] mod_wsgi (pid=4890): Target WSGI script '/check/www/textpisodes/apache/django.wsgi' cannot be loaded as Python module.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] mod_wsgi (pid=4890): Exception occurred processing WSGI script '/check/www/textpisodes/apache/django.wsgi'.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] File "/check/www/textpisodes/apache/django.wsgi", line 9, in <module>
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
You need to add your virtual environment path so that apache/python can find modules there.
Either put this in your apache configuration (outside of VirtualHost entry)
WSGIPythonPath /home/me/virtualenv/env1/lib/python2.7/site-packages
Or put it in the sys.path in your wsgi file.

Apache-Django WSGI exception: ValueError: zero length field name in format

I'm setting up a django server (1.4, python 2.7) with apache2 + mod_wsgi on a Debian system. From the django side, everything works perfect, if I run manage.py runserver, the development server displays the site correctly, however, when I try to set up my apache server to run WSGI, it fails displaying a ValueError.
This is mysite.wsgi
ALLDIRS = ['/usr/local/lib/python2.7/site-packages']
# the above directory depends on the location of your python installation.
# if using virtualenv, it will need to match your projects locale.
import os
import sys
import site
os.environ['DJANGO_SETTINGS_MODULE'] = 'ggDatabase.settings'
prev_sys_path = list(sys.path)
sys.path.append("/usr/local/lib/python2.7")
path = "/home/acad/statsdb/src"
if path not in sys.path:
sys.path.append(path)
sys.path.append("/home/acad/statsdb/src/ggDatabase")
for directory in ALLDIRS:
site.addsitedir(directory)
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
# change this depending on your project.
site.addsitedir("/home/acad/statsdb/src/ggDatabase/")
os.environ['PYTHON_EGG_CACHE'] = '/home/acad/.python-eggs'
os.environ['SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
This is the apache VirtualHost config:
<VirtualHost 127.0.0.1:8000>
ServerName mysite.com
ServerAlias www.mysite.com
<Directory /home/acad/statsdb/src/ggDatabase>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
CustomLog /var/log/apache2/mysite.com.access.log combined
ErrorLog /var/log/apache2/mysite.com.error.log
WSGIDaemonProcess mysite.com user=acad group=acad threads=25 python-path=/usr/local/lib/python2.7
WSGIProcessGroup mysite.com
WSGIScriptAlias / /home/acad/statsdb/src/ggDatabase/apache/mysite.wsgi
</VirtualHost>
This is the exact configuration I have on my test server and it works there, but the new server runs into a problem when trying to run the wsgi script:
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] mod_wsgi (pid=26233): Target WSGI script '/home/acad/statsdb/src/ggDatabase/apache/mysite.wsgi' cannot be loaded as Python module.
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] mod_wsgi (pid=26233): Exception occurred processing WSGI script '/home/acad/statsdb/src/ggDatabase/apache/mysite.wsgi'.
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] File "/home/acad/statsdb/src/ggDatabase/apache/mysite.wsgi", line 31, in <module>
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 8, in <module>
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] from django import http
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/site-packages/django/http/__init__.py", line 11, in <module>
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] from urllib import urlencode, quote
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/urllib.py", line 1228, in <module>
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] _safe_map[c] = c if (i < 128 and c in always_safe) else '%{:02X}'.format(i)
[Fri Jun 29 09:38:14 2012] [error] [client 127.0.0.1] ValueError: zero length field name in format
The error is so generic that I really don't know what could be going wrong. Any ideas?
Thanks in advance!
Bruno
Solved it. If someone has similar errors, make sure to print sys.version, you may find out (as I did) that wsgi was configured under another installed python version.
If that's the case, reinstall mod_wsgi with the --with-python option.

Configure mod_wsgi on Mac OS X?

I have been trying to configure mod_wsgi for two days and still no luck. Here is what I did:
Create a sample django project mysite. Run python manage.py runserver and make sure it's working
Create apache directory under mysite, create apache_django_wsgi.conf, mysite.wsgi and an empty __init__.py
Content of apache_django_wsgi.conf:
WSGIPythonHome /usr/bin
WSGIRestrictStdout Off
WSGIDaemonProcess django
WSGIProcessGroup django
Alias /site_media/ "/Users/Garth/Dev/web-app/mysite/media/"
<Directory "/Users/Garth/Dev/web-app/mysite/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
Alias /media/ "/Library/Python/2.6/site-packages/django/contrib/admin/media/"
<Directory "/Library/Python/2.6/site-packages/django/contrib/admin/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
WSGIScriptAlias /mysite "/Users/Garth/Dev/web-app/mysite/apache/mysite.wsgi"
<Directory "/Users/Garth/Dev/web-app/mysite/apache">
Allow from all
</Directory>
Content of mysite.wsgi
import os
import sys
sys.path.append('/Users/Garth/Dev/web-app/mysite')
sys.path.append('/Users/Garth/Dev/web-app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Download mod_wsgi.so (precompiled binary for Mac OS X) and put it in /usr/libexec/apache2
Edit /etc/apache2/httpd.conf,
add:
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
Include /Users/Garth/Dev/web-app/mysite/apache/apache_django_wsgi.conf
Run sudo apachectl -k start
If I go to localhost, I can see the files and directories list. But if I go to localhost/mysite (which is the WSGIScriptAlias I configured), I got Internal Server Error.
The error_log of apache is:
[Fri May 13 11:10:38 2011] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Fri May 13 11:10:38 2011] [notice] Digest: generating secret for digest authentication ...
[Fri May 13 11:10:38 2011] [notice] Digest: done
[Fri May 13 11:10:38 2011] [notice] Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8l DAV/2 mod_wsgi/3.3 Python/2.6.1 configured -- resuming normal operations
[Fri May 13 11:10:50 2011] [error] [client ::1] mod_wsgi (pid=10921): Target WSGI script '/Users/Garth/Dev/web-app/mysite/apache/mysite.wsgi' cannot be loaded as Python module.
[Fri May 13 11:10:50 2011] [error] [client ::1] mod_wsgi (pid=10921): Exception occurred processing WSGI script '/Users/Garth/Dev/web-app/mysite/apache/mysite.wsgi'.
[Fri May 13 11:10:50 2011] [error] Traceback (most recent call last):
[Fri May 13 11:10:50 2011] [error] File "/Users/Garth/Dev/web-app/mysite/apache/mysite.wsgi", line 1, in <module>
[Fri May 13 11:10:50 2011] [error] import os
[Fri May 13 11:10:50 2011] [error] ImportError: No module named os
Does anyone see where goes wrong? The error_log seems to tell a lot. I'm new to web development, and I might have made some obvious mistakes. Thank you very much!
Don't set WSGIPythonHome for a start it isn't needed. It should only be needed in certain situations and this isn't one of them.
In this case you have set it to a wrong value. By rights the fact it is wrong shouldn't have caused a problem as Python should have fallen back to using correct default value, but it may not do that for some reason.
You can also delete the line:
WSGIRestrictStdout Off
as that isn't needed for mod_wsgi 3.3.