如何使用 nginx 和 gunicorn 让我的 python-flask 网站 24*7 可用
how to make my python-flask website available 24*7 using nginx and gunicorn
如何配置 gunicorn 以确保我的 python-flask 网站 24*7 可用?
我面临的问题是:一旦我关闭终端 window,网站就无法访问了。
我正在使用 rhel7.6 来托管一个使用 python-flask 的网站。
我已将 nginx 配置为 Web 服务器,将 gunicorn 配置为应用程序服务器。
。
如果有人可以在 using/configuring gunicorn 中帮助我确保我的网站 24*7 可用,我将不胜感激。
请给我一些我的代码如下:
[root@syed-dashboard-4 ~]# pwd
/root
[root@syed-dashboard-4 ~]#
[root@syed-dashboard-4 ~]# cat hello.py
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello magentabox!"
#if __name__ == "__main__":
# app.run(host='10.145.29.23',port=5000)
[root@syed-dashboard-4 ~]#
[root@syed-dashboard-4 ~]# gunicorn hello:app
[2019-07-17 10:34:11 +0000] [9346] [INFO] Starting gunicorn 19.9.0
[2019-07-17 10:34:11 +0000] [9346] [INFO] Listening at: http://127.0.0.1:8000 (9346)
[2019-07-17 10:34:11 +0000] [9346] [INFO] Using worker: sync
[2019-07-17 10:34:11 +0000] [9351] [INFO] Booting worker with pid: 9351
我是网络开发的新手,正如我提到的,我一关闭终端就无法访问该网站。如果这有助于解决我的问题,我也可以共享 nginx 配置日志。
非常感谢。
在 Linux,您可以在启动服务器后分离的 tmux 会话中启动您的应用程序。
# Create a new tmux session
tmux new -s server
# Start your gunicorn server
cd /path/to/app
gunicorn hello:app
# Detach the current tmux session using Ctrl - B + D
您可以关闭终端,您的服务器仍将 运行。
你可以使用主管。这是 运行 您的服务器 24*7 的专业方式。
请在您的主管配置中添加以下文件到 运行.
[program:your_project_name]
command=/home/virtualenvpath/your_env/bin/gunicorn --log-level debug run_apiengine:main_app --bind 0.0.0.0:5006 --workers 5 --worker-class gevent
stdout_logfile=/home/your_path_to_log/supervisor_stdout.log
stderr_logfile=/home/your_path_to_log/supervisor_stderr.log
user=your_user
autostart=true
autorestart=true
environment=PYTHONPATH="$PYTHONPATH:/home/path_to_your_project";OAUTHLIB_INSECURE_TRANSPORT='1';
在 supervisor 中配置它会 运行 24*7。每当您的机器重新启动时,它都会自动启动。
如何配置 gunicorn 以确保我的 python-flask 网站 24*7 可用? 我面临的问题是:一旦我关闭终端 window,网站就无法访问了。
我正在使用 rhel7.6 来托管一个使用 python-flask 的网站。 我已将 nginx 配置为 Web 服务器,将 gunicorn 配置为应用程序服务器。
。 如果有人可以在 using/configuring gunicorn 中帮助我确保我的网站 24*7 可用,我将不胜感激。
请给我一些我的代码如下:
[root@syed-dashboard-4 ~]# pwd
/root
[root@syed-dashboard-4 ~]#
[root@syed-dashboard-4 ~]# cat hello.py
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello magentabox!"
#if __name__ == "__main__":
# app.run(host='10.145.29.23',port=5000)
[root@syed-dashboard-4 ~]#
[root@syed-dashboard-4 ~]# gunicorn hello:app
[2019-07-17 10:34:11 +0000] [9346] [INFO] Starting gunicorn 19.9.0
[2019-07-17 10:34:11 +0000] [9346] [INFO] Listening at: http://127.0.0.1:8000 (9346)
[2019-07-17 10:34:11 +0000] [9346] [INFO] Using worker: sync
[2019-07-17 10:34:11 +0000] [9351] [INFO] Booting worker with pid: 9351
我是网络开发的新手,正如我提到的,我一关闭终端就无法访问该网站。如果这有助于解决我的问题,我也可以共享 nginx 配置日志。 非常感谢。
在 Linux,您可以在启动服务器后分离的 tmux 会话中启动您的应用程序。
# Create a new tmux session
tmux new -s server
# Start your gunicorn server
cd /path/to/app
gunicorn hello:app
# Detach the current tmux session using Ctrl - B + D
您可以关闭终端,您的服务器仍将 运行。
你可以使用主管。这是 运行 您的服务器 24*7 的专业方式。 请在您的主管配置中添加以下文件到 运行.
[program:your_project_name]
command=/home/virtualenvpath/your_env/bin/gunicorn --log-level debug run_apiengine:main_app --bind 0.0.0.0:5006 --workers 5 --worker-class gevent
stdout_logfile=/home/your_path_to_log/supervisor_stdout.log
stderr_logfile=/home/your_path_to_log/supervisor_stderr.log
user=your_user
autostart=true
autorestart=true
environment=PYTHONPATH="$PYTHONPATH:/home/path_to_your_project";OAUTHLIB_INSECURE_TRANSPORT='1';
在 supervisor 中配置它会 运行 24*7。每当您的机器重新启动时,它都会自动启动。