在 Ubuntu 16.04 LTS 上以生产模式在 VPS 上的 NginX/Passenger 服务器上部署 Flask 应用程序
Deploying Flask app on NginX / Passenger server on Ubuntu 16.04 LTS in production mode on VPS
问题一:问题是什么?:
在浏览器中关注 this tutorial I installed Nginx
and Passenger
on Ubuntu 16.04 to run Python Flask
app and I run into this issue: When I visit my server at http://hXXXXXXX.stratoserver.net/ 我收到此错误:
We're sorry, but something went wrong.
问题二:Passenger版和集成模式:
Passenger 开源 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
问题四:客运安装方法:
Nginx + Phusion APT 存储库
问题 5:您的应用的编程语言:
Python 3.7.6 + 烧瓶 1.1.1
问题 6:您是否使用 PaaS and/or 容器化?如果有的话是哪一个?
否
问题 7:关于您的设置还有什么我们应该知道的吗?
我在 strato.nl 有一个 VPS 安装了 Ubuntu 16.04,主机地址是:http://hXXXXXXX.stratoserver.net/ and I was following "Deploying a Python app with Passenger to production" 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 站点包?
解决方案是在应用程序的配置文件(在服务器上下文中)中添加 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 /Users/<username>/.pyenv/versions/3.9.2/envs/v3.9.2/bin/python3;
# Turn on Passenger
passenger_enabled on;
}
一切都按预期进行。请注意这一行:
passenger_python /home/linuxbrew/.linuxbrew/bin/python3;
它指向我用 linuxbrew 安装的 Python v3.7.6 二进制文件。
另一种选择是使用 Python 虚拟环境,您还可以在其中指定要使用的 Python 版本。
问题一:问题是什么?:
在浏览器中关注 this tutorial I installed Nginx
and Passenger
on Ubuntu 16.04 to run Python Flask
app and I run into this issue: When I visit my server at http://hXXXXXXX.stratoserver.net/ 我收到此错误:
We're sorry, but something went wrong.
问题二:Passenger版和集成模式: Passenger 开源 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
问题四:客运安装方法: Nginx + Phusion APT 存储库
问题 5:您的应用的编程语言: Python 3.7.6 + 烧瓶 1.1.1
问题 6:您是否使用 PaaS and/or 容器化?如果有的话是哪一个? 否
问题 7:关于您的设置还有什么我们应该知道的吗? 我在 strato.nl 有一个 VPS 安装了 Ubuntu 16.04,主机地址是:http://hXXXXXXX.stratoserver.net/ and I was following "Deploying a Python app with Passenger to production" 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 站点包?
解决方案是在应用程序的配置文件(在服务器上下文中)中添加 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 /Users/<username>/.pyenv/versions/3.9.2/envs/v3.9.2/bin/python3;
# Turn on Passenger
passenger_enabled on;
}
一切都按预期进行。请注意这一行:
passenger_python /home/linuxbrew/.linuxbrew/bin/python3;
它指向我用 linuxbrew 安装的 Python v3.7.6 二进制文件。
另一种选择是使用 Python 虚拟环境,您还可以在其中指定要使用的 Python 版本。