wsgi_mod错误:在virtualenv中未检测到pymongo

时间:2018-09-02 02:45:10

标签: python python-3.x apache web mod-wsgi

好的,我已经使用pip3在我的virtualenv中安装了pymongo,并且当我尝试在我的flask应用程序中加载pymongo模块时(仅当我使用自己的域运行它时,我仍然无法在我的烧瓶中运行flask公共IP)页面返回500 Internal Server Error。

这是错误

[Sun Sep 02 02:32:52.289075 2018] [wsgi:error] [pid 4722] [remote 172.69.134.106:13286] mod_wsgi (pid=4722): Target WSGI script '/var/www/mainyuk.andra.xyz/app.wsgi' cannot be loaded as Python module.
[Sun Sep 02 02:32:52.289139 2018] [wsgi:error] [pid 4722] [remote 172.69.134.106:13286] mod_wsgi (pid=4722): Exception occurred processing WSGI script '/var/www/mainyuk.andra.xyz/app.wsgi'.
[Sun Sep 02 02:32:52.289229 2018] [wsgi:error] [pid 4722] [remote 172.69.134.106:13286] Traceback (most recent call last):
[Sun Sep 02 02:32:52.289277 2018] [wsgi:error] [pid 4722] [remote 172.69.134.106:13286]   File "/var/www/mainyuk.andra.xyz/app.wsgi", line 5, in <module>
[Sun Sep 02 02:32:52.289286 2018] [wsgi:error] [pid 4722] [remote 172.69.134.106:13286]     from app import app as application
[Sun Sep 02 02:32:52.289299 2018] [wsgi:error] [pid 4722] [remote 172.69.134.106:13286]   File "/var/www/mainyuk.andra.xyz/app.py", line 3, in <module>
[Sun Sep 02 02:32:52.289305 2018] [wsgi:error] [pid 4722] [remote 172.69.134.106:13286]     from pymongo import MongoClient
[Sun Sep 02 02:32:52.289369 2018] [wsgi:error] [pid 4722] [remote 172.69.134.106:13286] ModuleNotFoundError: No module named 'pymongo'

目录结构如下

var/www/mainyuk.andra.xyz
├── venv (Virtual environment)
|   └── etc
├── app.wsgi
└── app.py

app.py

from flask import Flask, request
from pymongo import MongoClient

#Initializing Flask
app = Flask(__name__)

#Initializing MongoDB
#client = MongoClient('localhost', 27017)

#db = client.mainyuk
#dbColDevelopment = db.development

#Routing
@app.route('/')
def index():
    return 'It works!'
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
    #if 'events' in request.form:
    #    dbColDevelopment.insert_one({ "events" : request.form['events'] })
    return ''

if __name__ == "__main__":
    app.run(ssl_context=('../cert.pem', '../cert.private.pem'))

app.wsgi

import sys

sys.path.insert(0, '/var/www/mainyuk.andra.xyz/') #sys.path.append("/var/www/mainyuk.andra.xyz/")
from app import app as application

apache_configuration.conf

<VirtualHost *:80>
    ServerAdmin admin@localhost
    ServerName mainyuk.andra.xyz
    ErrorLog /var/www/mainyuk.andra.xyz/error.log
    CustomLog /var/www/mainyuk.andra.xyz/access.log combined

    #WSGIPythonHome /var/www/mainyuk.andra.xyz/venv
    #WSGIPythonPath /var/www/mainyuk.andra.xyz/venv/lib/python3.6/site-packages

    #WSGIScriptReloading On
    WSGIDaemonProcess app user=www-data group=www-data threads=5 python-home=/var/www/mainyuk.andra.xyz/venv
    WSGIScriptAlias / /var/www/mainyuk.andra.xyz/app.wsgi

    <Directory /var/www/mainyuk.andra.xyz>
        WSGIProcessGroup app
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
#    Alias /static/ /var/www/FLASKAPPS/helloworldapp/static
#    <Directory /var/www/mainyuk.andra.xyz/>
#        Order allow,deny
#        Allow from all
#    </Directory>
</VirtualHost>

1 个答案:

答案 0 :(得分:0)

确保使用python 2的pip而不是pip3来安装依赖项。出于某种原因(默认配置),apache不会使用pip3依赖项来运行应用程序。

$ sudo apt-get install python-pip
$ sudo pip install flask
$ sudo pip install pymongo

您也可以参考本教程来检查您的步骤:

https://medium.com/@jQN/deploy-a-flask-app-on-aws-ec2-1850ae4b0d41