我试图通过Apache2 Web服务器在AWS EC2(标准Ubuntu AMI)上运行Flask应用程序。我的应用内部使用Numpy。我测试了以下内容:
如果没有Numpy,App会运行Apache2。使用Numpy,它会在import numpy
上跳闸并引发500 Server错误(请参阅下面的日志)。
使用Numpy,直接从命令行(i.e. python app.py
)调用时,应用程序运行正常。 numpy位按预期工作,我可以在外部查询app端点。
我还在print sys.path
之前打印了系统路径(import numpy as np
),并确保路径中有相应的site-packages目录。我还手动指定WSGIPythonPath
以包含site-packages目录。我尝试将线程数设置为1并编辑apache2.conf文件。这些努力都没有取得成功。
以下是我的目录结构和相关文件的内容。
根文件夹/var/www/html/webserver_mockup
根文件夹内容
/var/www/html/webserver_mockup/app.wsgi
/var/www/html/webserver_mockup/mockup/__init__.py
/var/www/html/webserver_mockup/mockup/app.py
#!/usr/bin/python
import sys
import site
site.addsitedir('/home/ubuntu/.local/lib/python2.7/site-packages')
sys.path.insert(0, "/var/www/html/webserver_mockup")
from mockup.app import app as application
WSGIPythonPath /usr/local/lib/python2.7/site-packages/
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
WSGIDaemonProcess webserver_mockup threads=5
WSGIScriptAlias / /var/www/html/webserver_mockup/app.wsgi
<Directory />
WSGIProcessGroup %{GLOBAL}
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
[Fri Apr 07 04:09:41.062282 2017] [mpm_event:notice] [pid 7221:tid
140325391972224] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0
Python/2.7.12 configured -- resuming normal operations
[Fri Apr 07 04:09:41.062358 2017] [core:notice] [pid 7221:tid
140325391972224] AH00094: Command line: '/usr/sbin/apache2'
[Fri Apr 07 04:09:46.770159 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] mod_wsgi (pid=7225): Target
WSGI script '/var/www/html/webserver_mockup/app.wsgi' cannot
be loaded as Python module.
[Fri Apr 07 04:09:46.770300 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] mod_wsgi (pid=7225):
Exception occurred processing WSGI script
'/var/www/html/webserver_mockup/app.wsgi'.
[Fri Apr 07 04:09:46.770370 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] Traceback (most recent
call last):
[Fri Apr 07 04:09:46.770432 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] File
"/var/www/html/webserver_mockup/app.wsgi", line 7, in <module>
[Fri Apr 07 04:09:46.770534 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] from mockup.app import
app as application
[Fri Apr 07 04:09:46.770612 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] File
"/var/www/html/webserver_mockup/mockup/app.py", line 12, in
<module>
[Fri Apr 07 04:09:46.770693 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] import numpy as np
[Fri Apr 07 04:09:46.770755 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] ImportError: No module
named numpy
我的问题是:尽管适当的网站包在其路径中,为什么Apache2找不到Numpy?
答案 0 :(得分:2)
此:
site.addsitedir('/home/ubuntu/.local/lib/python2.7/site-packages/numpy')
应该是:
site.addsitedir('/home/ubuntu/.local/lib/python2.7/site-packages')
但是,无论如何,设置Python虚拟环境的方式都不是最佳实践。建议你阅读:
同时尝试切换到守护进程模式,最好使用守护进程模式而不是嵌入模式。
答案 1 :(得分:2)
numpy via pip未安装在/home/ubuntu/.local/lib/python2.7/site-packages,但应该在/home/ubuntu/.local/lib64/python2.7/site-packages或类似的东西。
尝试将app.wsgi中的另一个site.addsitedir添加到该文件夹中。像这样:
site.addsitedir(&#39; /home/ubuntu/.local/lib64/python2.7/site-packages')