继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

CentOS系统部署flask项目

好帮手慕燕燕
关注TA
已关注
手记 4
粉丝 24
获赞 1

系统: 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

[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
http://img2.sycdn.imooc.com/61cadf8200011aa409660275.jpg
http://img2.sycdn.imooc.com/61cadf820001bf4809620106.jpgimage-20211228144142566

1.3 配置环境变量

[root@iZbp1ejocu3f7z7p55v87kZ bin]# vim ~/.bash_profile
http://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
http://img1.sycdn.imooc.com/61cae005000147e909680415.jpg

2.2 安装 uWSGI

[root@iZbp1ejocu3f7z7p55v87kZ home]# pip3 install uwsgi
http://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
浏览器访问服务器地址:
http://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 项目

http://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
http://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

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

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

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP