【问题标题】:unable to access .env files while running with uwsgi使用 uwsgi 运行时无法访问 .env 文件
【发布时间】:2020-08-09 07:17:44
【问题描述】:

我正在使用 smtplib 发送简单的电子邮件以在烧瓶应用程序中进行预订我正在使用谷歌邮件并拥有应用程序密码以及允许的不太安全的应用程序。我在我的个人电脑上运行了预订系统,但是一旦我将它移植到 VPS 上它就停止工作,除了用户名和密码之外的未知原因不被接受,但它们绝对是正确的,它会运行本身,但在 wsgi 和 nginx 中运行时不会。

Nginx 配置

    server {
    listen 80;
    server_name example.com;
    # return 301 https://$server_name$request_uri;
    location / {
        uwsgi_pass unix:/path/too/chatbot.sock;
    include uwsgi_params;
    }
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;`

ssl_certificate /path/too/keys.pem;
ssl_certificate_key /path/too//primarykey.pem;
ssl_trusted_certificate /path/too//keys.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions

# curl https://ssl-config.mozilla.org/ffdhe2048.txt > /path/to/dhparam
#ssl_dhparam /path/to/dhparam;

# intermediate configuration
ssl_protocols TLSv1.2;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;

# HSTS (ngx_http_headers_module is required) (63072000 seconds)
add_header Strict-Transport-Security "max-age=63072000" always;

# replace with the IP address of your resolver
resolver 8.8.8.8;
location /  {
    include uwsgi_params;
    uwsgi_pass unix:/path/too/chatbot.sock;
}

}

UWSGI.ini 文件

[uwsgi]

module=wsgi:app

master = true
processes = 5
enable-threads = true



socket = chatbot.sock
chmod.socket = 666
vacuum = true

die-on-term = true

.env

DIALOGFLOW_PROJECT_ID=projectid
GOOGLE_APPLICATION_CREDENTIALS=Ajsonfile.json
RESTFUL_CREDENTIALS=restful_credentials.json
MAIL_USERNAME=example@gmail.com
MAIL_PASSWORD=apasswordforemailaddress

我目前的想法是,由于某种权限问题,wsgi 或 nginx 也无法找到该文件,但我已经删除了所有相关文件,我的 google api 密钥现在也遇到了同样的问题.

所有信息都存储在具有正确组访问权限的 .env 文件中,以及站点上已经运行的所有其他文件。

除了我使用 nginx 和 wsgi 来公开烧瓶应用程序之外,我不知道在这里发帖会有什么帮助,有些项目存储在一个似乎没有被读取的 .env 文件中。

【问题讨论】:

标签: python ubuntu flask wsgi dotenv


【解决方案1】:

要在 WSGI 中运行时加载它们,您需要使用 dot-env 包

from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())

【讨论】:

    【解决方案2】:

    [uwsgi]

    base = /var/www/html/poopbuddy-api

    chdir = %(base)

    app = app

    我不确切知道chdir 做了什么,但我认为它至少将默认位置设置为应用程序的根目录。 load_dotenv() 从那里开始工作。

    【讨论】:

      猜你喜欢
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      相关资源
      最近更新 更多