C/C++教程

CentOS系统部署flask项目

本文主要是介绍CentOS系统部署flask项目,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

系统: CentOS 7(阿里云)

版本: Python 3.8.5

1 安装 Python3

1.1 安装依赖环境

# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel lrzsz tar make gcc gcc-c++
# yum install libffi-devel -y

1.2 安装 Python

下载:https://www.python.org/downloads/release/python-385/

[root@iZbp1ejocu3f7z7p55v87kZ home]# tar -zxvf Python-3.8.5.tgz
[root@iZbp1ejocu3f7z7p55v87kZ home]# cd ~
[root@iZbp1ejocu3f7z7p55v87kZ ~]# mkdir python
[root@iZbp1ejocu3f7z7p55v87kZ ~]# cd /home/Python-3.8.5
[root@iZbp1ejocu3f7z7p55v87kZ Python-3.8.5]# ./configure --prefix=’/root/python’
[root@iZbp1ejocu3f7z7p55v87kZ Python-3.8.5]# make
[root@iZbp1ejocu3f7z7p55v87kZ Python-3.8.5]# make install
https://img2.sycdn.imooc.com/61cadf8200011aa409660275.jpg
https://img2.sycdn.imooc.com/61cadf820001bf4809620106.jpgimage-20211228144142566

1.3 配置环境变量

[root@iZbp1ejocu3f7z7p55v87kZ bin]# vim ~/.bash_profile
https://img1.sycdn.imooc.com/61cadfe20001057b09640387.jpg

[root@iZbp1ejocu3f7z7p55v87kZ bin]# source ~/.bash_profile

1.4 升级 pip

[root@iZbp1ejocu3f7z7p55v87kZ home]# pip3 install --upgrade pip

2 安装 uWSGI

2.1 安装依赖库

[root@iZbp1ejocu3f7z7p55v87kZ home]# yum install python3-devel
https://img1.sycdn.imooc.com/61cae005000147e909680415.jpg

2.2 安装 uWSGI

[root@iZbp1ejocu3f7z7p55v87kZ home]# pip3 install uwsgi
https://img1.sycdn.imooc.com/61cae02200016ec709690079.jpg

3 安装 Nginx

[root@iZbp1ejocu3f7z7p55v87kZ Python-3.8.5]# yum install nginx -y
[root@iZbp1ejocu3f7z7p55v87kZ Python-3.8.5]# systemctl start nginx
浏览器访问服务器地址:
https://img1.sycdn.imooc.com/61cae038000183c708560260.jpg

Nginx 常用命令

# 设置 nginx 开机自启动  
systemctl enable nginx  
  
# 开启 nginx  
systemctl start nginx  
  
# 查看 nginx 运行状态  
systemctl status nginx  
  
# 关闭 nginx  
systemctl stop nginx  
  
# 重启 nginx  
systemctl restart nginx  
  
# 重载配置文件  
systemctl reload nginx

4 启动 flask 项目

4.1 安装 flask

[root@iZbp1ejocu3f7z7p55v87kZ Python-3.8.5]# pip3 install flask

4.2 安装项目依赖包

开发环境下,项目环境依赖写入 requirement.txt
pip freeze >requirements.txt

将flask项目拷贝到服务器,安装 requirement.txt 所包含的依赖
[root@iZbp1ejocu3f7z7p55v87kZ moive_web]# pip3 install -r requirement.txt

安装 mysqlclient
[root@iZbp1ejocu3f7z7p55v87kZ moive_web]# yum install gcc mariadb-devel
[root@iZbp1ejocu3f7z7p55v87kZ moive_web]# pip3 install mysqlclient

4.3 Python 启动 flask 项目

https://img2.sycdn.imooc.com/61cae0540001245e09640409.jpg

4.4 uwsgi 启动 flask 项目

在项目根目录下创建 uwsgi 配置文件
[root@iZbp1ejocu3f7z7p55v87kZ moive_web]# vim uwsgi.ini

# /var/www/conf/config.ini  
  
[uwsgi]  
  
# uwsgi 启动时所使用的地址与端口  
socket = 127.0.0.1:8000  
  
# python 调用的模块  
module = app  
  
# python 启动程序文件  
wsgi-file = /www/movie_web/app.py  
  
# python 程序内用以启动的 application 变量名  
callable = app  
  
# 处理器数  
processes = 4  
  
# 线程数  
threads = 8  
  
# 输出日志文件  
daemonize = /www/movie_web/log/server.log

启动 uwsgi
https://img1.sycdn.imooc.com/61cae08100010c3c09700078.jpg

4.5 使用 nginx 启动 uwsgi

[root@iZbp1ejocu3f7z7p55v87kZ nginx]# cd /etc/nginx/conf.d
[root@iZbp1ejocu3f7z7p55v87kZ nginx]# vim default.conf

server {  
listen       80;  
server_name  localhost;  
access_log  /var/log/nginx/host.access.log  main;  

    location / {  
        include uwsgi_params;  
        uwsgi_pass 127.0.0.1:8000;  
        uwsgi_param UWSGI_CHDIR /www/moive_web;  
    }  
}

[root@iZbp1ejocu3f7z7p55v87kZ moive_web]# systemctl reload nginx
[root@iZbp1ejocu3f7z7p55v87kZ moive_web]# systemctl restart nginx

浏览器访问服务器地址,大功告成~

https://img2.sycdn.imooc.com/61cae0e90001993007340584.jpg

这篇关于CentOS系统部署flask项目的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!