宽屏模式

Nginx+uwsgi+Python3.6.8部署Flask项目

运行需要安装ghostscript 具体要求:https://github.com/deimo/pdf-compress

接口说明

  • 安装python3.6.8 由于是编译安装,所以需要确认相应的依赖包已经安
yum install -y gcc zlib* sqlite-devel libXcomposite libXcursor libXi libXtst libXScrnSaver libXrandr atk at-spi2-atk gtk3 openssl-devel

下载python安装包,wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz 解压并进入解压文件夹,tar -zxvf Python-3.6.8.tgz && cd Python-3.6.8 编译安装,依次输入以下命令

./configure --prefix=/usr/local/python3 --with-ssl --enable-optimizations --enable-loadable-sqlite-extensions

make && make install 创建python3的软连接

ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

输入 python3 ,显示如下说明python3.6.8安装成功

输入 exit() 退出python3.6.8

安装flask和uwsgi 采用豆瓣源安装 pip3 install -i https://pypi.douban.com/simple flask pip3 install -i https://pypi.douban.com/simple uwsgi

创建uwsgi的软连接

ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi (漏了会在执行uwsgi 命令时提示无此命令)

配置 uwsgi 在 flask 项目目录创建 uwsgi.ini 配置文件,内容如下

[uwsgi]

# uwsgi 启动时所使用的地址与端口
socket = 127.0.0.1:5000

# 指向网站根目录
chdir  = /www/wwwroot/python_api

# python 启动程序文件
wsgi-file = app.py 

# python 程序内用以启动的 application 变量名
callable = app 

# 进程数量
processes       = 4
vacuum          = true
master          = true

# 线程数
threads = 2

# add uwsgi log
daemonize = /www/wwwroot/python_api/uwsgi.log

# add uwsgi pid
pidfile = /www/wwwroot/python_api/uwsgi.pid

启动uwsgi uwsgi --ini uwsgi.ini 停止uwsgi uwsgi --stop uwsgi.pid 这里停止出错的话

signal_pidfile()/kill(): No such process [core/uwsgi.c line 1695]

ps -ef | grep uwsgi
# 查看进程为 5065
root      5065     1  0 16:04 ?        00:00:00 uwsgi --ini /data/www/python_api/uwsgi.ini
root      5066  5065  0 16:04 ?        00:00:00 uwsgi --ini /data/www/python_api/uwsgi.ini
root      5067  5065  0 16:04 ?        00:00:00 uwsgi --ini /data/www/python_api/uwsgi.ini
root      5068  5065  0 16:04 ?        00:00:00 uwsgi --ini /data/www/python_api/uwsgi.ini
root      5069  5065  0 16:04 ?        00:00:00 uwsgi --ini /data/www/python_api/uwsgi.ini
root      5299 25257  0 16:05 pts/0    00:00:00 grep --color=auto uwsgi

将 uwsig.pid 里面的值改为 5065 即可;

没有pid停止 uwsgi:

killall -s INT /usr/bin/uwsgi

启动时出现:

[uWSGI] getting INI configuration from /data/www/python_api/uwsgi.ini
*** Starting uWSGI 2.0.19.1 (64bit) on [Sun Aug  2 15:36:39 2020] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 02 August 2020 07:20:22
os: Linux-3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018
nodename: VM_0_9_centos
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /data/www/python_api
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /data/www/python_api
your processes number limit is 65535
your memory page size is 4096 bytes
detected max file descriptor number: 100001
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:5000 fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.6.8 (default, Jun  4 2019, 21:45:17)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xc62ca0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 364600 bytes (356 KB) for 4 cores
*** Operational MODE: preforking ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 32369)
spawned uWSGI worker 1 (pid: 32370, cores: 1)
spawned uWSGI worker 2 (pid: 32371, cores: 1)
spawned uWSGI worker 3 (pid: 32372, cores: 1)
spawned uWSGI worker 4 (pid: 32373, cores: 1)

即为成功;

后台运行:

uwsgi -d --ini /www/wwwroot/python_api/uwsgi.ini

开机启动:

vim /etc/rc.d/rc.local
加入
/usr/local/python3/bin/uwsgi -d --ini /data/www/python_api/uwsgi.ini
即可

#安装nginx 下载nginx安装包 http://nginx.org/download/nginx-1.17.1.tar.gz 解压并进入解压文件夹 tar zxvf nginx-1.17.1.tar.gz && cd nginx-1.17.1 执行 configure 脚本,开启 http_ssl、http_mp4 http_flv 功能 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_mp4_module --with-http_flv_module 编译 make && make install 启动nginx nginx 停止nginx 命令 nginx -s stop 启动nginx后,在浏览器输入服务器地址,看到以下页面说明安装成功

#配置nginx 把 /usr/local/nginx/conf/vhost/python_api.conf 添加以下内容

server {

    listen 8889;

    server_name test.com;
    charset utf-8;

    client_max_body_size 75M;

    location / {

        include uwsgi_params;                       # 导入uwsgi配置

        uwsgi_send_timeout 1060;        # 指定连接到后端uWSGI的超时时间。
        uwsgi_connect_timeout 1060;   # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。
        uwsgi_read_timeout 1060;        # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。
        uwsgi_pass 127.0.0.1:5000;                  # 转发端口,需要和uwsgi配置当中的监听端口一致
        uwsgi_param UWSGI_PYTHON /usr/bin/python3;  # Python解释器所在的路径,如果有虚拟环境可将路径设置为虚拟环境
        uwsgi_param UWSGI_CHDIR /www/wwwroot/python_api;         # 项目根目录
        uwsgi_param UWSGI_SCRIPT app:app;           # 项目的主程序,比如你测试用run.py文件,文件中app = Flask(__name__),那么这里就填run:app
    }
}

启动uwsgi后,在浏览器地址栏输入服务器对应ip即可访问flask项目

Larwas
请先登录后发表评论
  • latest comments
  • 总共0条评论