手记

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

[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

1.3 配置环境变量

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

[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

2.2 安装 uWSGI

[root@iZbp1ejocu3f7z7p55v87kZ home]# pip3 install uwsgi

3 安装 Nginx

[root@iZbp1ejocu3f7z7p55v87kZ Python-3.8.5]# yum install nginx -y
[root@iZbp1ejocu3f7z7p55v87kZ Python-3.8.5]# systemctl start nginx
浏览器访问服务器地址:

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 项目

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

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

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

1人推荐
随时随地看视频
慕课网APP