1.首先在Linux部署Flask项目
1.1 使用virtualenv给Flask项目添加虚拟环境
进入到flask目录
$ virtualenv -p python3.7 venv
或
$ virtualenv -p ~/.pyenv/versions/3.7.11/bin/python venv
-p为给虚拟环境指定解释器,其中解释器python3.7等(或是指定pyenv的版本)可以使用pyenv安装不同版本的
1.2 安装包
进入到虚拟环境
$ source venv/bin/activate
安装包个gunicorn
$ pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
$ pip3 install gunicorn
-i 为指定国内镜像源
1.3 虚拟环境运行flask
启动项目
$ python start.py
成功启动,代表成功将flask部署到Linux
2.使用supervisor管理flask
安装好后在/etc/会生成一个supervisord.conf文件及一个supervisord.d文件目录
在supervisord.conf中添加自己配置文件的路径
[include]
files = supervisord.d/*.ini
即可在supervisord.d文件夹下定制配置文件
我的配置文件:
program.ini
#django项目
[program:yibai_ad_web]
command=/.pyenv/versions/3.7.11/bin/gunicorn --worker-class=gevent mysite.wsgi:application --bind 0.0.0.0:8888 --workers 7 --timeout 120 --threads 15 --log-level=info --access-logfile=/data/AD_WEB/web_log/web_access.log --access-logformat="%%(h)s %%(u)s %%(t)s %%(T)s \"%%(r)s\" %%(s)s %%(b)s \"%%(f)s\" \"%%(a)s\"" --error-logfile=/data/AD_WEB/web_log/web_error.log
directory=/data/AD_WEB
autostart=false
autorestart=false
stderr_logfile=/data/AD_WEB/web_log/web_stderr.log
stdout_logfile=/data/AD_WEB/web_log/web_stdout.log
user=root
#flask项目
[program:lazada_flask]
command=/data/my_flask/lazada/venv/bin/gunicorn --worker-class=gevent run:app --bind 0.0.0.0:8003 --workers 1 --timeout 120 --threads 2 --log-level=info --access-logfile=/data/my_flask/lazada/web_log/web_access.log --access-logformat="%%(h)s %%(u)s %%(t)s %%(T)s \"%%(r)s\" %%(s)s %%(b)s \"%%(f)s\" \"%%(a)s\"" --error-logfile=/data/my_flask/lazada/web_log/web_error.log
directory=/data/my_flask/lazada
autostart=false
autorestart=false
stderr_logfile=/data/my_flask/lazada/web_log/web_stderr.log
stdout_logfile=/data/my_flask/lazada/web_log/web_stdout.log
user=root
#fastapi项目
[program:fastapi]
command=/data/fastapi/venv/bin/gunicorn --worker-class="uvicorn.workers.UvicornWorker" main:app --bind 0.0.0.0:8040 --workers 1 --timeout 120 --threads 2 --log-level=info --access-logfile=/data/fastapi/web_log/web_access.log --access-logformat="%%(h)s %%(u)s %%(t)s %%(T)s \"%%(r)s\" %%(s)s %%(b)s \"%%(f)s\" \"%%(a)s\"" --error-logfile=/data/fastapi/web_log/web_error.log
directory=/data/fastapi
autostart=false
autorestart=false
stderr_logfile=/data/fastapi/web_log/web_stderr.log
stdout_logfile=/data/fastapi/web_log/web_stdout.log
user=root
其中program为自定义项目名
command 为gunicorn启动
浙公网安备 33010602011771号