Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target WSGI script cannot be loaded as Python module

I am trying to deploy mod_wsgi with apache to run a django application but I am getting an error 500 internal server error The apache logs shows:

[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] mod_wsgi (pid=16142): Exception occurred processing WSGI script '/home/user/bms/apache/django.wsgi'. [Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] Traceback (most recent call last): [Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64]   File "/home/user/bms/apache/django.wsgi", line 13, in <module> [Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64]     import django.core.handlers.wsgi [Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] ImportError: No module named django.core.handlers.wsgi 

My apache virtual host is as follows:

<VirtualHost *:80>      DocumentRoot /home/user/bms      <Directory /home/user/bms>         Order allow,deny         Allow from all     </Directory>  WSGIDaemonProcess bms user=user group=user processes=2 threads=25 python-path=/usr/local/lib/python2.7/site-packages       WSGIProcessGroup bms      WSGIScriptAlias / /home/user/bms/apache/django.wsgi  </VirtualHost> 

And the referenced wsgi file in my app directory with 0777 permissions:

import os import sys  path = '/home/user/bms' if path not in sys.path:     sys.path.append(path)  os.environ['DJANGO_SETTINGS_MODULE'] = 'bms.settings'  import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() 

I heard that this may be because the apache user does not have the correct permissions. However I have no idea how to fix this. I also tried starting the deamon with the www-data user and this did not solve the issue.

EDIT:

I solved this by copying the virtual hosts file into the default one and then disabling the old one with a2dissite. I have no idea how I can do it "properly" and set it so apache goes to the virtual host I want it to though.

like image 224
Adam Thomas Avatar asked Jun 23 '11 13:06

Adam Thomas


People also ask

What is WSGI file in Python?

The Web Server Gateway Interface (WSGI, pronounced whiskey or WIZ-ghee) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. The current version of WSGI, version 1.0. 1, is specified in Python Enhancement Proposal (PEP) 3333.

Is WSGI a module?

Therefore the Python community came up with WSGI as a standard interface that modules and containers could implement. WSGI is now the accepted approach for running Python web applications.

Does Apache support WSGI?

mod_wsgi is an Apache module which can host any Python WSGI application, including Django. Django will work with any version of Apache which supports mod_wsgi. The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi.


2 Answers

For me the problem was wsgi python version mismatch. I was using python 3, so:

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi $ sudo apt-get install libapache2-mod-wsgi-py3 

Warning from @alxs before you copy/paste these commands:
If there are python 2 projects running on the server that use wsgi and apache, the above commands will effectively shut them down.

like image 84
nima Avatar answered Sep 19 '22 16:09

nima


For me the issue was that the WSGI script wasn't executable.

sudo chmod a+x django.wsgi 

or just

sudo chmod u+x django.wsgi 

so long as you have the correct owner

like image 23
Gerry Avatar answered Sep 22 '22 16:09

Gerry