Flask部署最详细步骤

Flask部署

  1. 安装zsh工具
yum install -y zsh
yum install -y git
wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh
  1. 新建用户
useradd www -d /home/www -m -s /bin/zsh
passwd www
  1. 安装Virtualenv
pip install virtualenv
pip install flask
yum -y update
  1. 新建python虚拟环境, 虚拟环境内安装flask
cd /home/www
virtualenv venv
source venv/bin/activate
pip install flask
  1. 新建python测试文件 /home/www/hello.py
vim hello.py
#coding=utf-8
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return "<h1 style='color:blue'>Hello There!</h1>"


if __name__ == '__main__':
    app.run() 
  1. 在venv激活的情况下安装gunicorn
source /home/www/venv/bin/activate
pip install gunicorn
  • 测试gunicorn是否成功
gunicorn hello:app
netstat -an | grep 80  
  • 去浏览器输入阿里云的公网ip,就可以看到hello.py已经运行
  1. 退出激活状态,安装nginx
yum install nginx
systemctl enable nginx
systemctl start nginx
  • 激活nginx和启动nginx

  • 测试nginx是否安装成功,去浏览器输入阿里云公ip,看网页输出结果

  • 添加nginx配置文件,新版本打开nginx默认配置文件,在里面添加如下内容

vim /etc/nginx/nginx.conf
server {
    listen 80;
    server_name 47.90.99.88;
    root /home/www;
    access_log /home/www/logs/nginx_access.log;
    error_log /home/www/logs/nginx_error.log;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
    }

  }
  • 检测nginx 配置文件是否正确, -t:检测配置文件, -c:指定配置文件
nginx -t -c /etc/nginx/nginx.conf
  • 重启nginx, (# service nginx restart)-s:send signal to a master process: stop, quit, reopen, reload
nginx -s reload
  1. 安装supervisor到系统, 生成配置文件
pip install supervisor
echo_supervisord_conf > /etc/supervisord.conf
  1. 编辑/etc/supervisord.conf,并在最后一行加入一下字段
    这样配置文件会将/etc/supervisor/conf.d下所有.conf结尾的都会导入进来
[include]
files = /etc/supervisor/conf.d/*.conf
  1. 在创建一个配置文件到/etc/supervisor/conf.d/johnkee.conf
[program:www]
command=/home/www/venv/bin/gunicorn hello:app
directory=/home/www/
user=root
autostart=true
autorestart=true
stdout_logfile=/home/www/logs/supervisor_out.log
stderr_logfile=/home/www/logs/supervisor_err.log
  1. reread来检测修改的配置内容, update来更新
supervisorctl reread
supervisorctl update
  1. 修改falsk文件后重从加载flask文件
supervisorctl restart www
  1. 生成requirements.txt文件(在激活本工程虚拟环境下)
pip freeze > requirements.txt
  1. 在系统自己新建的虚拟环境下安装requirements.txt依赖
pip install -r requirements.txt
pip uninstall urllib3 && pip uninstall requests && pip uninstall chardet && pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3

问题解决办法

  • error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
    

    解决办法

    supervisord
    

certbot证书过期后如何更新证书并生效

certbot renew
nginx -s reload

certbot证书过期后报错解决办法:https://certbot.eff.org/docs/install.html

wget https://dl.eff.org/certbot-auto
sudo mv certbot-auto /usr/local/bin/certbot-auto
sudo chown root /usr/local/bin/certbot-auto
sudo chmod 0755 /usr/local/bin/certbot-auto
/usr/local/bin/certbot-auto --help
/usr/local/bin/certbot-auto --install-only 
/opt/eff.org/certbot/venv/bin/certbot
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于 CentOS 操作系统的 Nginx + Uwsgi + Flask 部署服务的详细操作步骤: 1. 安装必要的软件包 ```bash yum install epel-release yum install nginx yum install python3 python3-pip pip3 install uwsgi flask ``` 2. 创建 Flask 应用程序 在服务器上创建 Flask 应用程序,例如: ```python from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == '__main__': app.run() ``` 3. 创建 uwsgi 配置文件 在服务器上创建一个 uwsgi 配置文件,例如: ```ini [uwsgi] module = wsgi:app master = true processes = 5 socket = /tmp/uwsgi.sock chmod-socket = 660 vacuum = true die-on-term = true ``` 其中,`module` 参数指定 Flask 应用程序所在的模块和变量名,`socket` 参数指定 uwsgi 与 Nginx 通信的 Unix 套接字文件位置。 4. 创建 Nginx 配置文件 在服务器上创建一个 Nginx 配置文件,例如: ```nginx server { listen 80; server_name your-domain.com; location / { include uwsgi_params; uwsgi_pass unix:/tmp/uwsgi.sock; } } ``` 其中,`listen` 参数指定监听的端口,`server_name` 参数指定服务器的域名或 IP 地址,`location` 参数指定请求 URL 的路径,`uwsgi_pass` 参数指定 uwsgi 与 Nginx 通信的 Unix 套接字文件位置。 5. 启动服务 在服务器上启动 uwsgi 和 Nginx 服务: ```bash uwsgi --ini uwsgi.ini & systemctl start nginx ``` 6. 测试服务 在浏览器中输入服务器的域名或 IP 地址,应该能够看到 Hello, World! 的输出。 以上就是在 CentOS 上使用 Nginx + Uwsgi + Flask 部署服务的详细操作步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值