gpt4 book ai didi

python - apache2 和 mod wsgi : Target WSGI script cannot be loaded as Python module

转载 作者:IT王子 更新时间:2023-10-29 00:37:57 26 4
gpt4 key购买 nike

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

我有一个网站 (languageAnalyz),我正尝试将其与 mod_wsgi 一起使用。当我调用 localhost/languageAnalyz 时,出现错误 500。

在 apache2 日志中,我可以看到:

[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

我用谷歌搜索了这个错误,并找到了很多解决方案(大部分时间是针对 Django 项目)。我想知道的是,我需要创建一个 __init__.py 文件。我做了什么,这是我的 __init__.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

__all__ 列表中,这是三个文件中函数的名称。我只是想启动 test-wsgi.py,并得到相同的错误 500。

这是我的 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()

那怎么了?谢谢,

编辑:这是我的 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>

编辑之二:所以我试着按照我在 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>

我重新启动 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

编辑者:

好的,我刚刚成功启动了 testwsgi.py。我将我的应用程序功能更改为应用程序,并在末尾添加了一些行:

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

我什至不知道,为什么它可以工作,为什么它以前不工作。 ... 现在我在打开文件及其路径(配置文件或模板文件 ...)时遇到了一些问题

[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')

我用绝对路径试了一下,还是不行...

最佳答案

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

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

此外,您不能:

import test-wsgi

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

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

关于python - apache2 和 mod wsgi : Target WSGI script cannot be loaded as Python module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18427766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com