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
Related
I have been trying forever to get my django api to deploy via apache. I have installed mod_wsgi for python 3.7 and my venv is using python 3.7.15. Trying to go to my django app url I am getting a 500 error.
Error log shows:
[Tue Dec 20 21:31:30.690951 2022] [:error] [pid 19216] /usr
[Tue Dec 20 21:31:30.691287 2022] [:error] [pid 19216] mod_wsgi (pid=19216): Target WSGI script '.../project/project/wsgi.py' cannot be loaded as Python module.
[Tue Dec 20 21:31:30.691323 2022] [:error] [pid 19216] mod_wsgi (pid=19216): Exception occurred processing WSGI script '.../project/project/wsgi.py'.
[Tue Dec 20 21:31:30.691393 2022] [:error] [pid 19216] Traceback (most recent call last):
[Tue Dec 20 21:31:30.691423 2022] [:error] [pid 19216] File ".../project/project/wsgi.py", line 19, in <module>
[Tue Dec 20 21:31:30.691428 2022] [:error] [pid 19216] from django.core.wsgi import get_wsgi_application
[Tue Dec 20 21:31:30.691444 2022] [:error] [pid 19216] ModuleNotFoundError: No module named 'django'
[Tue Dec 20 21:31:51.190670 2022] [:error] [pid 19217] 3.7.15 (default, Oct 31 2022, 22:44:31)
[Tue Dec 20 21:31:51.190707 2022] [:error] [pid 19217] [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]
apache conf file:
<VirtualHost *:80>
ServerName api.project.com
DocumentRoot path/to/project/root
WSGIScriptAlias / /path/to/wsgi.py
WSGIDaemonProcess project-name processes=4 threads=1 display-name=%{GROUP} python-path=path/to/lib/python3.7/site-packages:/path/to/project/root
WSGIProcessGroup project-group
<Directory "/path/to/project/root">
Require all granted
</Directory>
#SSL stuff...
</VirtualHost>
wsgi.py:
import os
import sys
from django.core.wsgi import get_wsgi_application
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
application = get_wsgi_application()
I originally had the wrong mod_wsgi installed (built for python2). I removed that and installed python3-mod_wsgi.x86_64 and changed module path in httpd.conf. I then realized that my mod_wsgi was for python version 3.7, but my application venv was running python 3.6.8. I then wiped the venv and created a new one with python 3.7.15. Changed the conf WSGIDAEMONPROCESS path to the correct 3.7 site-packages. Still same error.
Just installing a virtual environment is not enough, then you'll have to activate it.
Run pip install -r requirements.txt to install the required packages for the project.
The Error
The first error line states Target WSGI script '.../project/project/wsgi.py' cannot be loaded as Python module.
This means your environment variable DJANGO_SETTINGS_MODULE is not set properly. In order to do it, go to .bashrc and set it to the same value as in the asgi.py/wsgi.py file.
Run source .bashrc to reload the configuration file, and restart the bash terminal (or open a new one) where you're trying to run the application.
This should work.
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.
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'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.
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.