I'm putting my django site in production for the first time so please forgive for my ignorance.
I'm trying to put my django site on apache. I've read documentation about mod_wsgi and tried that simple Hello world so it is configured OK. The problem I'm having seems to be with using virtualenvs with it. I wanna set things up properly including virtualenvs and everything so I'm ready for future sites.
To the problem now.
The error I'm getting in apache log is:
No module named django.core.handlers.wsgi
So it seems that it is not reading my virtualenvs properly.
This is my wsgi script:
import os
import sys
import site
site.addsitedir('/home/user/.virtualenvs/myapp/lib/python2.7/site-packages')
path = '/home/user/django/myapp/myapp'
if path not in sys.path:
sys.path.append(path)
sys.stdout = sys.stderr
print sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
And this is the error log from apache. I printed out the sys.path so you can see what it looks like.
[Tue Jun 05 14:54:07 2012] [error] ['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/PIL', '/usr/lib/python2.7/site-packages/setuptools-0.6c11.egg-info', '/home/user/.virtualenvs/myapp/lib/python2.7/site-packages', '/home/user/django/myapp/myapp']
[Tue Jun 05 14:54:07 2012] [error] [client 127.0.0.1] mod_wsgi (pid=1039): Target WSGI script '/srv/http/wsgi_scripts/myapp.wsgi' cannot be loaded as Python module.
[Tue Jun 05 14:54:07 2012] [error] [client 127.0.0.1] mod_wsgi (pid=1039): Exception occurred processing WSGI script '/srv/http/wsgi_scripts/myapp.wsgi'.
[Tue Jun 05 14:54:07 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Tue Jun 05 14:54:07 2012] [error] [client 127.0.0.1] File "/srv/http/wsgi_scripts/myapp.wsgi", line 17, in <module>
[Tue Jun 05 14:54:07 2012] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Jun 05 14:54:07 2012] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
If you have any suggestions or already had similar issue please help.
Thanks
You haven't added your actual virtualenv site-packages directory to the mix. Try:
import site
site.addsitedir('/path/to/your/virtualenv/lib/python2.X/site-packages')
# Where `X` is the specific version
For anyone that might have similar problem as I did. You need to check the whole path privileges to the directory where your virtualenv is stored.
I checked the home directory and changed privileges, but forgot to change privileges to my user directory and that fixed the thing.
Hope this helps.
Related
I have looked at every similar question about this and still can't figure out what's wrong. I'm try to set up a python REST service on my VPS alongside my wordpress site. I can get a "Hello world" wsgi file to run no problem, but when I point to my django rest wsgi file, I get a 500 server error.
My wordpress site is in /var/www/html/mysite/public_html, and the rest service is in /var/www/html/mysite/myrest. www-data has ownership of everything in the mysite folder. I've installed all dependencies, and started the project with python manage.py runserver just to make sure it's not missing anything.
This is my wsgi file.
import os
import sys
import site
site.addsitedir('/var/www/html/mysite/myrest/lib/python2.7/site-packages')
sys.path.append('/var/www/html/mysite/myrest')
os.environ['PYTHON_EGG_CACHE'] = '/var/www/html/mysite/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Before it didn't have the site.addsitedir stuff, that got added as I tried different solutions.
There's some weird stuff going on in the apache logs, but I couldn't make sense of it.
[Sun Feb 19 20:39:33.697146 2017] [core:notice] [pid 26048] AH00094: Command line: '/usr/sbin/apache2'
[Sun Feb 19 20:41:30.804083 2017] [mpm_prefork:notice] [pid 26048] AH00169: caught SIGTERM, shutting down
[Sun Feb 19 20:41:31.707014 2017] [wsgi:warn] [pid 26721] mod_wsgi: Compiled for Python/2.7.11.
[Sun Feb 19 20:41:31.707037 2017] [wsgi:warn] [pid 26721] mod_wsgi: Runtime using Python/2.7.12.
[Sun Feb 19 20:41:31.709784 2017] [mpm_prefork:notice] [pid 26721] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Sun Feb 19 20:41:31.709801 2017] [core:notice] [pid 26721] AH00094: Command line: '/usr/sbin/apache2'
I'm pulling my hair out. Your help is greatly appreciated.
The problem was in my settings.py file. I managed to debug it by putting
import django
django.setup()
in various places throughout settings.py.
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.
i tried to set up apache + mod_wsgi + django on my snow leopard, but it kept gave me the same errors which listed below. i tried to follow every tutorial on the web that i could found, but still couldn't make it work :( . (im sure all the paths are added into sys.path, but don't know why it had the import error)
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4333): Target WSGI script '/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi' cannot be loaded as Python module.
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4333): Exception occurred processing WSGI script '/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi'.
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] File "/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi", line 8, in <module>
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
Here is the apache configuration file:
<VirtualHost *:80>
WSGIDaemonProcess localdjango processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup localdjango
ServerName localdjango
WSGIScriptAlias / /Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi
<Directory /Users/kyle/wsgi_source/django-tutorial/mysite/apache>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
here is /Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi
1 import sys
2 import os
3
4 sys.path.append('/Users/eookoo/wsgi_source/django-tutorial')
5
6 os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
7
8 import django.core.handlers.wsgi
9 application = django.core.handlers.wsgi.WSGIHandler()
Thanks in advance.
It is import to know whether you are using the operating system supplied Python or not.
If you are and that is the only Python version on the system, then Django wasn't installed into it. That or if you are using a Python virtual environment, then you haven't set up your WSGI script file or mod_wsgi to use the Python virtual environment.
If you have installed a separate Python version, and Django is installed into it, then likely that mod_wsgi was compiled against the system Python version and not your version.
Alternative to the latter is that you are hitting a problem that occurs with some Python installations that caused framework linking under MacOS X to not work properly and at run time, even though mod_wsgi was compiled against your separate Python version, it is still using the system wide Python framework. In this latter case you need to rebuild mod_wsgi against your separate Python version, but this time use the '--disable-framework' option to the 'configure' script for mod_wsgi before building it.
For a general resource on MacOS X issues for mod_wsgi, ensure you read the documentation at:
http://code.google.com/p/modwsgi/wiki/InstallationOnMacOSX
I'm developing application on Django, and ready to move to deployment server. I'm able to run a new django project on apache(I can see the welcome page).
However, when I copy my original project's files to apache project directory, I start getting errors. Here is a sample from apache/error.log.
[Wed Jan 26 19:22:08 2011] [error] [client 127.0.0.1] TemplateSyntaxError: Caught ImportError while rendering: No module named charts
[Wed Jan 26 19:22:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4670): Exception occurred processing WSGI script '/srv/www/enpass/apache/django.wsgi'.
[Wed Jan 26 19:22:36 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Wed Jan 26 19:22:36 2011] [error] [client 127.0.0.1] File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__
[Wed Jan 26 19:22:36 2011] [error] [client 127.0.0.1] response = self.get_response(request)
[Wed Jan 26 19:22:36 2011] [error] [client 127.0.0.1] File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 141, in get_response
My django.wsgi file looks like
import os
import sys
path = '/srv/www'
if path not in sys.path:
sys.path.insert(0, '/srv/www')
os.environ['DJANGO_SETTINGS_MODULE'] = 'enpass.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I've changed all the paths in files to suit the new directory structure, so I'm pretty sure thats not the problem. FIY, I followed the steps given in this tutorial http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/
I figured it out myself. We need to add the project directory path to sys.path. The following code solved the problem in django.wsgi
path = '/srv/www/enpass'
if path not in sys.path:
sys.path.append(path)
Looks like you are missing the charts module..
install pip
sudo easy_install pip
list packages
pip freeze
do the same on the development machine and look for a chart module, then install it on the production server with
sudo pip install <packagename>
I'm trying to get Django and Apache working together using Mod_wsgi and currently I'm getting the following errors:
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] mod_wsgi (pid=4803): Target WSGI script '/home/webdev/websites/virtualenvs/polaris/polaris_project.py' cannot be loaded as Python module.
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] mod_wsgi (pid=4803): Exception occurred processing WSGI script '/home/webdev/websites/virtualenvs/polaris/polaris_project.py'.
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] Traceback (most recent call last):
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] File "/home/webdev/websites/virtualenvs/polaris/polaris_project.py", line 8, in <module>
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] import django.core.handlers.wsgi
[Thu Jul 15 12:52:38 2010] [error] [client 10.100.50.73] ImportError: No module named django.core.handlers.wsgi
My apache conf looks like
Alias /polaris_django/media/ "/home/webdev/websites/virtualenvs/polaris/polaris/static/"
WSGIScriptAlias /polaris_django /home/webdev/websites/virtualenvs/polaris/polaris_project.py
WSGIApplicationGroup %{GLOBAL}
<Directory "/home/webdev/websites/virtualenvs/polaris">
Order deny,allow
Allow from all
</Directory>
My Mod_WSGi file looks like
import os, sys
sys.path.append('/home/webdev/websites/virtualenvs/polaris')
sys.path.append('/home/webdev/websites/virtualenvs/polaris/polaris/apps')
sys.path.append('/home/webdev/websites/virtualenvs/polaris/polaris/extra_settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'polaris.settings'
print >> sys.stderr, sys.path
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
How can I get apache serving Django properly?
It looks like you're using a virtualenv - you need to activate that within your WSGI script to set up the paths correctly.
activate_this = os.path.join("path/to/my/virtualenv", "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
I'm guessing that apache is using either a different version of python or is using a different sys.path. What do you get as output for the sys.path?
Where is Django installed? From command line Python do:
import django
print django.__file__
If it isn't installed in appropriate directory on sys.path under /usr/lib/python2.6 or /usr/local/lib/python2.6, then that is the problem.
Presuming you actually installed Django, this may occur because you have multiple installed Python versions and you used different one to what mod_wsgi is using to install Django. Or you used a virtual environment and haven't told mod_wsgi where that is. Or you managed to install Django with permissions that Apache cannot read.
Go watch the video of talk and read through slides referenced at:
http://blog.dscpl.com.au/2010/06/sydney-pycon-modwsgi-talk-slides.html
They cover this sort of issue as well as a lot of other stuff related to permissions etc.