簡體   English   中英

通過 virtualenv 使用 mod_Wsgi 運行 Flask

[英]Running Flask with mod_Wsgi through virtualenv

在工作中的服務器(Apache 2.2.3)上,我試圖用 Flask 激活我的第一個 Hello World。 我在任何典型的推薦位置都找不到錯誤日志。

我在/home/nrDee/public_html/rrfexpire了一個 virtualenv

這是我的 rrfexpire.wsgi 腳本:

activate_this = 
'/home/nrDee/public_html/rrfexpire/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from rrfexpire import app as application

這已添加到 Apache httpd.conf:

WSGIDaemonProcess rrfexipre user=apache group=apache threads=5
WSGIScriptAlias /blog /home/nrDee/public_html/rrfexpire/rrfexpire.wsgi

<Directory /home/nrDee/public_html/rrfexpire>
WSGIProcessGroup rrfexpire
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>

這是我的 Hello World 測試 (rrfexpire.py):

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def hello_world():
  return "Hello, World!"

print hello_world()

if __name__ == '__main__':
  app.run(host='0.0.0.0', port=8082, debug=True)

當我在http://departmentServer.company.com/rrfexpire/使用 Chrome 瀏覽器時,我希望能看到結果,但我什么也沒得到。 整個 nrDee 目錄設置為 777 權限,以消除阻止其運行的權限。 我試圖確認 Python 版本 (2.7.13) 和 mod_wsgi 與命令的兼容性

#ldd venv/lib/python2.7/site-packages/mod_wsgi/server/mod_wsgi-py27.so

該結果中的一行內容為“libpython2.7.so.1.0 => /usr/local/bin/anaconda/lib/libpython2.7.so.1.0”,我將其解釋為表示 mod_wsgi 已針對 Python2.7 正確編譯

我對此很陌生,並且一直在大量使用 Flask 和 mod_wsgi 文檔並感到卡住了。
您能否就接下來要檢查的內容提供指導?
謝謝! -nrDee

我有一個可行的解決方案,但它並不完美,因為除非重啟httpd服務,否則我的URL不會更新。 但是至少我現在可以看到我的HelloWorld!

我當前的rrfexpire.wsgi腳本令我感到困惑,因為它在py2.7 virtualenv中使用了Python3語法:

activate_this = 
'/home/nrDee/public_html/rrfexpire/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

import sys
sys.path.insert(0, '/home/nrDee/public_html/rrfexpire')
from rrfexpire import app as application

這是我的httpd.conf

<VirtualHost *>
  ServerName company.server.com:80

  WSGIDaemonProcess rrfexpire user=apache group=apache
  WSGIScriptAlias /rrfexpire /home/nrDee/public_html/rrfexpire/rrfexpire.wsgi

<Directory /home/nrDee/public_html/rrfexpire>
WSGIProcessGroup rrfexpire
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

不使用“晦澀腳本”的最簡單解決方案是在 .wsgi 文件的開頭簡單地引用 venv 中的 python:

#!/path/to/your/venv/bin/python

簡單的。 合乎邏輯。 沒有魔法。 希望我比我更早想到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM