Python3环境搭建(uWSGI+Django+Nginx+Python+MySQL)

1、系统环境,必要知识

#cat /etc/RedHat-release
CentOS Linux release 7.5.1804 (Core)
#uname -r
3.10.0-862.3.2.el7.x86_64

暂时关闭防护墙,关闭selinux:

#systemctl stop firewalld.service
#setenforce 0
#getenforce
Permissive

准备知识:

django:一个基于python的开源web框架。

uWSGI:一个基于自有的uwsgi协议,wsgi协议和http服务协议的web网关

nginx:高性能的代理web服务器

wsgi.py:django项目自带的wsgi接口文件(位于:项目/项目名/wsgi.py)

整个项目流程:

首先客户端通过浏览器访问服务器资源;nginx作为对外服务的端口(80),nginx接收到客户端http请求后会解包分析,如果是静态文件就去配置的静态文件中查找资源并返回给客户端,如果是动态资源,nginx就通过配置文件将请求传递给uwsgi处理,并转发给uwsgi,wsgi根据请求调用django工程的文件和函数,处理后django将返回值交给wsgi,wsgi将返回值进行打包,转发给uWSGI,uWSGI接收到数据后转发给nginx,最终返回给客户端。

2、安装python3.6.5

(1)安装python依赖包

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel python-devel

(2)安装python

#wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
#mkdir -p /usr/local/python356
#tar zxvf Python-3.6.5.tgz
#cd Python-3.6.5
#./configure --prefix=/usr/local/python356
#make
#make install
#ln -s /usr/local/python356/bin/python3 /usr/local/bin/python3
#ln -s /usr/local/python356/bin/pip3 /usr/local/bin/pip3
#pip3 install --upgrade pip  #更新pip
#pip3 install ipython  #安装ipython方便调试

测试安装是否正常:

#python3 -V
Python 3.6.5

#pip3 -V
pip 10.0.1 from /usr/local/python356/lib/python3.6/site-packages/pip (python 3.6)

3、安装uWSGI

使用python的pip工具包安装:

#pip3 install uwsgi

#ln -s /usr/local/python356/bin/uwsgi /usr/local/bin/uwsgi  #建立软链接
#uwsgi --version  #检查安装成功
2.0.17

建立uWSGI的配置文件,在django项目中建立uwsgi.ini文件:

[uwsgi]
socket = 127.0.0.1:9090
chdir = /djproject/mysite
module = mysite.wsgi    #这里填的是相对路径
master = true
processes = 2
threads = 2
max-requests = 2000
vacuum = true
daemonize = /djproject/mysite/uwsgi.log
stats = 127.0.0.1:9001
post-buffering = 65535
buffer-size = 65535
harakiri-verbose = true
harakiri = 300
uid = nginx
pidfile = /djproject/mysite/uwsgi.pid

参数说明:

socket:指定监听地址和端口

chdir:指定工程的绝对路径,如Django的项目路径

module:指定web应用的入口模块,如Django项目下的wsgi.py接口文件

master:启动主进程

processes:启动进程数

threads:启动线程数

max-requests:最大请求数

daemonize:指定uWSGI日志文件路径

stats:指定状态查询端口,如:127.0.0.1:9001

wsgi-file:指定启动的文件

post-buffering:设置缓冲区

buffer-size:设置缓冲区文件大小

harakiri-verbose:设置超时true为开启

harakiri:设置超时时间

uid、gid:设置用户和组

pidfile:指定启动时的pid文件路径

uwsgi信号控制:

HUP    :优雅的重新加载所有进程和配置,同(--reload)一样

TERM  :强制重新加载主进程和配置

INT  :立即杀死整个uWSGI;同于:--stop

QUIT  :立即杀死真个uWSGI

重新启动实例:

#kill -HUP `cat /tmp/project-mast.pid`

#uwsgi --reload /tmp/project-master.pid

还可以在python程序中使用uwsgi.reload()重新加载

停止服务器:

#uwsgi --stop /tmp/project-master.pid

#kill -INT `cat /tmp/project-master.pid`

编写启动脚本:

#vim /etc/init.d/uwsgi

#!/bin/bash
#this is uwsgi server script

. /etc/init.d/functions

uwsgi=/usr/local/bin/uwsgi
uwsgi_pid=/djangoproject/mysite/uwsig.pid
uwsgi_conf=/djangoproject/mysite/uwsigconf.ini
uwsgi_pn=`ps -ef|grep -v "grep"|grep -c "uwsgi"`
ERVAL=0
start(){
    $uwsgi --ini $uwsgi_conf >& /dev/unll
    ERVAL=$?
    if [ $ERVAL -eq 0 ];then
        action "uwsgid starting ..." /bin/true
    else
        action "uwsgid start is error" /bin/false
    fi
}

stop(){
    $uwsgi --stop $uwsgi_pid >& /dev/null
    ERVAL=$?
    if [ $ERVAL -eq 0 ];then
        action "uwsgid stoping ..." /bin/true
    else
        action "uwsgid stop is error" /bin/false
    fi
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/75f89ffef06c6c8f05a32037df413a36.html