我写了一个简单的hello.py作为API,并希望通过端口8081使用它,但遇到问题: 目标WSGI脚本' /var/www/wsgi/hello.wsgi'无法作为Python模块加载。 在Ubuntu16.04上,它是一个全新的安装。这台机器上只安装了apache2,flask,php和mysql。 Python版本默认为2.7.12。
我已经尝试过几个搜索过的解决方案,但到目前为止,这些解决方案都没有。如果有人能提供帮助,那将是非常感激的
[wsgi:info] [pid 28286] [client 127.0.0.1:53384] mod_wsgi (pid=28286, process='', application='test.test123|'): Loading WSGI script '/var/www/wsgi/hello.wsgi'.
[wsgi:error] [pid 28286] [client 127.0.0.1:53384] mod_wsgi (pid=28286): Target WSGI script '/var/www/wsgi/hello.wsgi' cannot be loaded as Python module.
[wsgi:error] [pid 28286] [client 127.0.0.1:53384] mod_wsgi (pid=28286): Exception occurred processing WSGI script '/var/www/wsgi/hello.wsgi'.
[wsgi:error] [pid 28286] [client 127.0.0.1:53384] Traceback (most recent call last):
[wsgi:error] [pid 28286] [client 127.0.0.1:53384] File "/var/www/wsgi/hello.wsgi", line 5, in <module>
[wsgi:error] [pid 28286] [client 127.0.0.1:53384] from hello import app as application
[wsgi:error] [pid 28286] [client 127.0.0.1:53384] File "/var/www/html/hello/hello.py", line 1, in <module>
[wsgi:error] [pid 28286] [client 127.0.0.1:53384] from flask import Flask
[wsgi:error] [pid 28286] [client 127.0.0.1:53384] ImportError: No module named flask
大多数帖子都在谈论python版本,我用ldd验证了我的mod_wsgi版本,认为它是Python 2.7:
linux-vdso.so.1 => (0x00007fff93ebe000)
libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007f270e359000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f270e13c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f270dd72000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f270db58000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f270d954000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f270d750000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f270d447000)
/lib64/ld-linux-x86-64.so.2 (0x0000557eb7f65000)
同时打印sys.version和sys.path,似乎也是默认的2.7:
[wsgi:error] [pid 29129] 2.7.12 (default, Nov 19 2016, 06:48:10)
[wsgi:error] [pid 29129] [GCC 5.4.0 20160609]
[wsgi:error] [pid 29129] ['/var/www/html/hello', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
有几个帖子在讨论 https://code.google.com/archive/p/modwsgi/wikis/CheckingYourInstallation.wiki ,但我不明白这一个
顺便说一下,在我的ubuntu 16.04上没有/ etc / apache2 / httpd存在,但我发现有一篇文章说/ usr / sbin / apache2是一样的。对于我的apache2版本信息:/usr/sbin/apache2 -V
Server version: Apache/2.4.18 (Ubuntu)
Server built: 2016-07-14T12:32:26
Server's Module Magic Number: 20120211:52
Server loaded: APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"
任何暗示都会令人感激。
我的机器在路由器后面,端口正确转发(在应用wsgi之前在apache2上使用phpInfo()测试)。
文件结构:/var/www/
--------/html/
-------------/hello/hello.py
-------------/index.html
-------------/index.php
--------/wsgi/hello.wsgi
/etc/apache2/
------------/site-available/
---------------------------/test.test123.conf
---------------------------/000-default.conf
---------------------------/default-ssl.conf
------------/site-enabled/
---------------------------/000-default.conf
---------------------------/test.test123.conf
hello.py:
from flask import Flask
app = Flask(__name__)
@app.route('/<sInput>')
def hello_world(sInput):
return sInput
hello.wsgi:
import sys
sys.path.insert(0, '/var/www/html/hello')
print(sys.version)
print(sys.path)
from hello import app as application
test.test123.conf:
<virtualhost *:80>
ServerName test.test123
WSGIDaemonProcess hello user=www-data group=www-data threads=5 home=/var/www/html/hello/
WSGIScriptAlias / /var/www/wsgi/hello.wsgi
<directory /var/www/html/hello>
WSGIProcessGroup hello
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Allow from all
</directory>
</virtualhost>
/ etc / hosts中:
127.0.0.1 localhost
127.0.1.1 test123-Ubuntu16.04 test123-Ubuntu16
127.0.0.1 test.test123
/etc/apache2/ports.conf:
Listen 80
Listen 8081
如果还有其他信息需要提供,请告知我们。谢谢。