如何在Apache的Flask上使用conda env 我要做什么我一直在努力的问题我在Apache日志/var/log/apache2/error.log中遇到的错误到目前为止我做了什么 /var/www/中的目录结构我想问什么

问题描述

我已经看到了与此相关的一些问题,但是我还没有找到任何简洁而有用的答案,所以请允许我问你。

要做什么

在具有conda环境的Ubuntu 18.04上的Apache2上部署Flask应用。

我一直在努力的问题

mod_wsgi使用除我定义要使用的conda环境以外的其他python。

我在Apache日志/var/log/apache2/error.log中遇到的错误

[Mon Aug 17 06:44:00.036518 2020] [mpm_event:notice] [pid 4509:tid 140114341411776] AH00491: caught SIGTERM,shutting down
[Mon Aug 17 06:44:00.114859 2020] [mpm_event:notice] [pid 4765:tid 140587342384064] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Mon Aug 17 06:44:00.114979 2020] [core:notice] [pid 4765:tid 140587342384064] AH00094: Command line: '/usr/sbin/apache2'
[Mon Aug 17 06:44:03.788601 2020] [wsgi:error] [pid 4768:tid 140587013428992] [client 175.177.41.29:17031] mod_wsgi (pid=4768): Target Wsgi script '/var/www/MyApp/wsgi.py' cannot 
be loaded as Python module.
[Mon Aug 17 06:44:03.788743 2020] [wsgi:error] [pid 4768:tid 140587013428992] [client 175.177.41.29:17031] mod_wsgi (pid=4768): Exception occurred processing Wsgi script '/var/www/MyApp/wsgi.py'.
[Mon Aug 17 06:44:03.789027 2020] [wsgi:error] [pid 4768:tid 140587013428992] [client 175.177.41.29:17031] Traceback (most recent call last):
[Mon Aug 17 06:44:03.789058 2020] [wsgi:error] [pid 4768:tid 140587013428992] [client 175.177.41.29:17031]   File "/var/www/MyApp/wsgi.py",line 7,in <module>
[Mon Aug 17 06:44:03.789063 2020] [wsgi:error] [pid 4768:tid 140587013428992] [client 175.177.41.29:17031]     from DeepColors.request import app as application
[Mon Aug 17 06:44:03.789072 2020] [wsgi:error] [pid 4768:tid 140587013428992] [client 175.177.41.29:17031]   File "/var/www/MyApp/MyApp/request.py",line 1,in <module>
[Mon Aug 17 06:44:03.789076 2020] [wsgi:error] [pid 4768:tid 140587013428992] [client 175.177.41.29:17031]     from flask import Flask,request
[Mon Aug 17 06:44:03.789092 2020] [wsgi:error] [pid 4768:tid 140587013428992] [client 175.177.41.29:17031] ModuleNotFoundError: No module named 'flask'

到目前为止我做了什么

  • 已安装Apache2
  • libapache2-mod-wsgi-py3安装了pip install mod_wsgi到Ubuntu。有人说安装它们两个都会导致问题,所以我卸载了libapache2-mod-wsgi-py3,现在apachectl configtest可以打印出来了
AH00526: Syntax error on line 1 of /etc/apache2/sites-enabled/DeepColors.conf:
Invalid command 'WsgiDaemonProcess',perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' Failed.
The Apache error log may have more @R_495_4045@ion.

所以我重新安装了libapache2-mod-wsgi-py3

  • MyApp.conf中创建了/etc/apache2/sites-available/
WsgiDaemonProcess DeepColors python-path=/home/username/anaconda3/envs/myenv/lib/python3.7/site-packages
WsgiScriptAlias / /var/www/MyApp/wsgi.py
<VirtualHost *:80>
                ServerName <global ip>
                ServerAdmin [email protected]
                <Directory /var/www/MyApp/MyApp/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/MyApp/MyApp/static
                <Directory /var/www/MyApp/MyApp/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • wsgi.py中创建了/var/www/MyApp/
#!/home/username/anaconda3/envs/myenv/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/MyApp/")
from MyApp.request import app as application
application.secret_key = 'deepcolors'

/var/www/中的目录结构

DeepColors
├── DeepColors
│   ├── README.md
│   ├── data
│   ├── download.py
│   ├── env.yml
│   ├── images
│   ├── lib
│   ├── models
│   ├── options
│   ├── request.py
│   ├── result
│   ├── scripts
│   └── util
└── wsgi.py

我想问什么

  • 如何在我创建的conda环境中将正在使用的python切换为该python,或者有什么方法可以解决此问题?
  • 有人说使用vertualenv比conda env更好,为什么?

如果您需要更多信息,我们将很乐意提供。 期待您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)