在VPS上的生产模式下,在Ubuntu 16.04 LTS的NginX / Passenger服务器上部署Flask应用

时间:2020-01-29 14:07:25

标签: nginx flask ubuntu-16.04 passenger vps

问题1:有什么问题?: 在this tutorial之后,我在Ubuntu 16.04上安装了NginxPassenger来运行Python Flask应用,并且遇到了这个问题:当我访问位于http://hXXXXXXX.stratoserver.net/的服务器时,浏览器中出现此错误:

很抱歉,出了点问题。

问题2:乘客版本和集成方式: 客运开源6.0.4 + Nginx

问题3:OS或Linux发行版,平台(包括版本): Ubuntu 16.04 LTS

$ uname -a
Linux hXXXXXXX.stratoserver.net 4.4.0-042stab141.3 #1 SMP Fri Nov 15 22:45:34 MSK 2019 x86_64 x86_64 x86_64 GNU/Linux

问题4:乘客的安装方法: Nginx + Phusion APT回购

问题5:您应用的编程语言: Python 3.7.6 + Flask 1.1.1

问题6:您是否正在使用PaaS和/或容器化?如果是这样的话?

问题7:我们还应该了解关于您的设置的其他信息吗? 我在strato.nl上安装了Ubuntu 16.04,并在其中安装了VPS,主机地址为http://hXXXXXXX.stratoserver.net/,并且我按照“将带有Passenger的Python应用程序部署到生产环境” tutorial进行了本教程中的以下选择:

1. Linux/Unix
2. Nginx
3. Passenger open source
4. Python installed via LinuxBrew (Python v3.7.6)
5. Ubuntu 16.04 LTS
6. Demo Flask app from github

演示Flask应用程序是从Phusio Passenger github中克隆的,如下所示:

git clone https://github.com/phusion/passenger-python-flask-demo.git

运行passenger-memory-stats给出:

$ sudo /usr/sbin/passenger-memory-stats
Version: 6.0.4
Date   : 2020-01-29 13:12:15 +0100
------------- Apache processes -------------
*** WARNING: The Apache executable cannot be found.
Please set the APXS2 environment variable to your 'apxs2' executable's filename, or set the HTTPD environment variable to your 'httpd' or 'apache2' executable's filename.


---------- Nginx processes -----------
PID    PPID   VMSize    Private  Name
--------------------------------------
23320  1      174.9 MB  0.8 MB   nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
23325  23320  174.9 MB  0.8 MB   nginx: worker process
### Processes: 2
### Total private dirty RSS: 1.54 MB


----- Passenger processes -----
PID    VMSize    Private  Name
-------------------------------
23309  445.7 MB  2.5 MB   Passenger watchdog
23312  672.3 MB  7.5 MB   Passenger core
### Processes: 2
### Total private dirty RSS: 9.98 MB

当我在服务器上本地运行该应用程序时,一切正常:

$ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

然后在另一个终端:

$ curl localhost:5000
<!DOCTYPE html>
<html>
<head>
  <title>Hello</title>
  <style>
    html, body {
      font-family: sans-serif;
      background: #f0f0f0;
      margin: 4em;
    }

    .main {
      background: white;
      border: solid 1px #c0c0c0;
      border-radius: 8px;
      padding: 2em;
    }
  </style>
</head>
<body>

  <section class="main">
    <h1>Hello world!</h1>
    <p>Welcome to the Passenger Flask example app.</p>
  </section>

</body>
</html>

所以在本地看起来一切正常。 但是,当我在浏览器中访问http://hXXXXXXX.stratoserver.net/时,出现了如上所述的错误页面。 nginx错误/var/log/nginx/error.log包含以下内容:


App 29730 output: from flask import Flask, render_template
App 29730 output: ImportError
App 29730 output: :
App 29730 output: No module named flask

因此,该脚本似乎找不到我先前为Python3安装的软件包,并且它使用的是Python2软件包。在服务器响应中打印sys.version会得到:

sys.version: 2.7.17 (default, Dec 24 2019, 17:49:09)

我为Python3安装了所有软件包,因此我需要服务器上的脚本使用Python3。 如何在NginX / Passenger中设置Python版本和Python库?

我的demoapp配置为:

$ vi /etc/nginx/sites-enabled/demoapp.conf
server {
    listen 80;
    server_name hXXXXXXX.stratoserver.net;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/demoapp/code/public;

    # Turn on Passenger
    passenger_enabled on;
}

要成功运行Flask应用程序,需要设置哪些passenger / Nginx设置? 如何设置Passenger / Nginx使用Python3和Python3网站包?

1 个答案:

答案 0 :(得分:0)

解决方案是在应用程序的配置文件中(在服务器上下文中)添加Python二进制文件的路径。所以现在我的配置文件demoapp.conf看起来像:

$ vi /etc/nginx/sites-enabled/demoapp.conf
server {
    listen 80;
    server_name hXXXXXXX.stratoserver.net;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/demoapp/code/public;

    # Tell Nginx and Passenger which Python executable to use
    passenger_python /home/linuxbrew/.linuxbrew/bin/python3;

    # Turn on Passenger
    passenger_enabled on;
}

一切正常。请注意这一行:

passenger_python /home/linuxbrew/.linuxbrew/bin/python3;

指向我随linuxbrew安装的Python v3.7.6二进制文件。

另一种选择是使用Python虚拟环境,您还可以在其中指定要使用的Python版本。