apache2和mod wsgi:目标wsgi脚本不能作为Python模块加载

时间:2022-10-06 10:28:08

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

我试图在apache2上安装mod_wsgi,在ubuntu上。所以我安装了libapache2-mod-wsgi包,用a2enmod激活他。

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

我有一个网站(languageAnalyz),我正在尝试使用mod_wsgi。当我调用localhost/languageAnalyz时,我得到了一个错误500。

In apache2 log, I can see this :

在apache2 log中,我可以看到:

[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 googled this error, and I find lot of solutions (most of the times for Django project). What I undsertand, is that I need to create a __init__.py file. What I did, here is my __init__.py file :

我在谷歌上搜索了这个错误,并找到了许多解决方案(大部分时间用于Django项目)。我想说的是,我需要创造一个。py文件。我所做的,这是我的简历。py文件:

__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, this is the name of the functions in the three files. I am just trying to launch test-wsgi.py, and get the same error 500.

在__all__列表中,这是三个文件中函数的名称。我正在尝试启动test-wsgi。py,得到相同的误差500。

Here is my test-wsgi.py file :

这是我的test-wsgi。py文件:

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's wrong ? Thank you,

所以怎么了?谢谢你!

Edit : here is my apache2 conf :

编辑:这是我的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 as I read on django doc. I change my apache2 conf to :

所以我试着按照我读到的django doc去做。我将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 one more before :

我重新启动apache2,得到了相同的错误,还有一个错误:

[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 :

Edit_ter:

Ok, I just successfully launch testwsgi.py. I change my app function into application, and I add some lines at the end :

好的,我刚刚成功地启动了testwsgi.py。我把我的app功能改成application,最后加了几行:

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

I event don't know, why it's working, why it was not working before. ... And now I got some problem to open files, with their path (config files, or templates 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 try with absolute path, it didn't work too ...

我试着用绝对路径,它也不管用……

3 个解决方案

#1


0  

You would appear to be following the wrong documentation for starters. For mod_wsgi use:

对于初学者来说,您似乎遵循了错误的文档。mod_wsgi使用:

You appear to be using documentation for FASTCGI deployment.

您似乎在使用FASTCGI部署的文档。

Also, you cannot do:

同样,你不能做的:

import test-wsgi

A Python module name cannot have '-' in it.

Python模块的名称不能包含“-”。

In summary, go reread (or read) the Django documentation on deployment.

总之,请重新阅读(或阅读)关于部署的Django文档。

#2


0  

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

对我来说,问题是wsgi python版本不匹配。我使用的是python 3,所以:

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

#3


0  

I had the same problem for a differnt file and making the file world executable fixed it for me. You would of course want to lock it down to the required user after you have narrowed this down as your issue:

我对一个不同的文件有相同的问题,并使文件世界可执行文件为我修复。你当然会想把它锁定在需要的用户之后,你已经把这个缩小为你的问题:

chmod a+x test-wsgi.py

#1


0  

You would appear to be following the wrong documentation for starters. For mod_wsgi use:

对于初学者来说,您似乎遵循了错误的文档。mod_wsgi使用:

You appear to be using documentation for FASTCGI deployment.

您似乎在使用FASTCGI部署的文档。

Also, you cannot do:

同样,你不能做的:

import test-wsgi

A Python module name cannot have '-' in it.

Python模块的名称不能包含“-”。

In summary, go reread (or read) the Django documentation on deployment.

总之,请重新阅读(或阅读)关于部署的Django文档。

#2


0  

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

对我来说,问题是wsgi python版本不匹配。我使用的是python 3,所以:

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

#3


0  

I had the same problem for a differnt file and making the file world executable fixed it for me. You would of course want to lock it down to the required user after you have narrowed this down as your issue:

我对一个不同的文件有相同的问题,并使文件世界可执行文件为我修复。你当然会想把它锁定在需要的用户之后,你已经把这个缩小为你的问题:

chmod a+x test-wsgi.py