Apache2 and mod wsgi: target WSGI script cannot be loaded as a Python module

I am trying to install mod_wsgi on apache2, on ubuntu. So I install the libapache2-mod-wsgi package, I activate it with a2enmod.

I have a website (languageAnalyz) that I am trying to use with mod_wsgi. When I call localhost / languageAnalyz, I got a 500 error.

In apache2 log, I see this:

[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] mod_wsgi (pid=4993): Target WSGI script '/var/www/languageAnalyz/test-wsgi.py' cannot be loaded as Python module. [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] mod_wsgi (pid=4993): SystemExit exception raised by WSGI script '/var/www/languageAnalyz/test-wsgi.py' ignored. [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] Traceback (most recent call last): [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/var/www/languageAnalyz/test-wsgi.py", line 10, in <module> [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] WSGIServer(app).run() [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi.py", line 112, in run [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] sock = self._setupSocket() [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 997, in _setupSocket [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] req.run() [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 572, in run [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] self._end(appStatus, protocolStatus) [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 601, in _end [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] sys.exit(appStatus) [Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] SystemExit: 0 

I made a mistake in this error and I find many solutions (most of the time for the Django project). What I am undsertand is that I need to create a __init__.py file. What I did, here is my __init__.py file:

 __all__ = ['app','get_size_dir','get_nbrf_dir','getStats'] #name of my functions import index # my three python files import analyz import test-wsgi 

In the __all__ list __all__ this is the name of the three-file functions. I am just trying to run test-wsgi.py and get the same 500 error.

Here is my test-wsgi.py file:

 import os,sys sys.path.append(os.path.dirname(__file__)) from cgi import escape,parse_qs from flup.server.fcgi import WSGIServer def app(environ, start_response): start_response('200 OK',[('Content-Type','text/plain; charset=utf-8')]) yield "hello world!" WSGIServer(app).run() 

So what happened? Thanks,

Edit: here is my apache2 conf:

 WSGIPythonPath /var/www/languageAnalyz <VirtualHost *:80> ... <Directory /var/www/languageAnalyz/> Options +Indexes ExecCGI FollowSymLinks MultiViews AllowOverride None Order allow,deny Allow from all DirectoryIndex index.py SetHandler wsgi-script </Directory> ... </VirtualHost> 

Edit_bis: So I try to do when I read django doc. I am changing my apache2 conf:

 WSGIPythonPath /var/www/languageAnalyz <VirtualHost *:80> ... WSGIScriptAlias /IPA /var/www/languageAnalyz/testwsgi.py <Directory /var/www/languageAnalyz/> Options +Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny Allow from all DirectoryIndex testwsgi.py </Directory> ... </VirtualHost> 

I restart apache2 and I got the same error and another one:

 [Sun Aug 25 12:47:18 2013] [notice] caught SIGTERM, shutting down [Sun Aug 25 12:47:19 2013] [notice] FastCGI: wrapper mechanism enabled (wrapper: /usr/lib/apache2/suexec) [Sun Aug 25 12:47:19 2013] [notice] FastCGI: process manager initialized (pid 7879) [Sun Aug 25 12:47:19 2013] [notice] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads. [Sun Aug 25 12:47:19 2013] [notice] mod_python: using mutex_directory /tmp [Sun Aug 25 12:47:19 2013] [warn] mod_wsgi: Compiled for Python/2.7.3. [Sun Aug 25 12:47:19 2013] [warn] mod_wsgi: Runtime using Python/2.7.4. [Sun Aug 25 12:47:19 2013] [notice] Apache/2.2.22 (Ubuntu) mod_fastcgi/mod_fastcgi-SNAP-0910052141 PHP/5.4.9-4ubuntu2.2 mod_python/3.3.1 Python/2.7.4 mod_ruby/1.2.6 Ruby/1.8.7(2012-02-08) mod_wsgi/3.4 configured$ [Sun Aug 25 12:47:19 2013] [error] WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI! [Sun Aug 25 12:47:19 2013] [error] WSGIServer: missing FastCGI param SERVER_NAME required by WSGI! [Sun Aug 25 12:47:19 2013] [error] WSGIServer: missing FastCGI param SERVER_PORT required by WSGI! [Sun Aug 25 12:47:19 2013] [error] WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI! [Sun Aug 25 12:47:19 2013] [error] Status: 200 OK\r [Sun Aug 25 12:47:19 2013] [error] Content-Type: text/plain; charset=utf-8\r [Sun Aug 25 12:47:19 2013] [error] \r [Sun Aug 25 12:47:19 2013] [error] hello world! [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] mod_wsgi (pid=7884): Target WSGI script '/var/www/languageAnalyz/testwsgi.py' cannot be loaded as Python module. [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] mod_wsgi (pid=7884): SystemExit exception raised by WSGI script '/var/www/languageAnalyz/testwsgi.py' ignored. [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] Traceback (most recent call last): [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/var/www/languageAnalyz/testwsgi.py", line 10, in <module> [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] WSGIServer(app).run() [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi.py", line 112, in run [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] sock = self._setupSocket() [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 997, in _setupSocket [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] req.run() [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 572, in run [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] self._end(appStatus, protocolStatus) [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 601, in _end [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] sys.exit(appStatus) [Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] SystemExit: 0 

Edit_ter:

Ok, I just successfully run testwsgi.py. I change the application function to the application and add a few lines to the end:

 if __name__ == '__main__': from wsgiref.simple_server import make_server server = make_server('localhost', 8080, application) server.serve_forever() 

I donโ€™t know why it works, why it didnโ€™t work before .... And now I have a problem with opening files, their path (configuration files or template files ...)

 [Sun Aug 25 13:10:51 2013] [error] [client 127.0.0.1] File "/var/www/languageAnalyz/analyz.py", line 22, in getStats [Sun Aug 25 13:10:51 2013] [error] [client 127.0.0.1] flangs=open('config/languages.yml') 

I am trying with an absolute path, this did not work either ...

+7
python linux wsgi apache2 mod-wsgi
source share
3 answers

It looks like you are using the wrong documentation for starters. To use mod_wsgi:

It looks like you are using documentation to deploy FASTCGI.

In addition, you cannot:

 import test-wsgi 

Python module name cannot contain '-'.

In short, re-read (or read) the Django documentation on deployment.

0
source share

I had the same problem for the differnt file as for the executable file of the file world. You, of course, want to block it to the required user after you narrowed it down as a problem:

 chmod a+x test-wsgi.py 
0
source share

For me, the problem was the mismatch of the wsgi python version. I used python 3, therefore:

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

All Articles