prácticas de proyectos Python - preparar 02 Centos entorno del entorno de despliegue Python + Frasco

Un ambiente Python

1, las dependencias de instalación

# Yum -y instalar zlib zlib-devel bzip2-devel openssl-devel ncurses ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel XZ-devel libuuid-devel libffi-devel

2, descargar el código

Descomprimir y entrar en el directorio del código
tar zxvf xxx

3, Configuración

./Configure antes de la operación, primero configure
export LANG = zh_CN.UTF-8
exportación LANGUAGE = zh_CN.UTF-8
 
A continuación, añadir la configuración
./configure --prefix = / usr / pitón

4, la instalación

hacer
make install
 
Vuelva a instalar la limpieza
hacer limpia 
hacer distclean

5, además de enlaces de software

ln -s / usr / python / bin / python3 / usr / bin / python3
ln -s / usr / python / bin / PIP3 / usr / bin / PIP3

En segundo lugar, el entorno virtual

pip instalar virtualenv
pip instalar virtualenvwrapper
 
Sin el PIP
PIP3 instalar virtualenv
PIP3 instalar virtualenvwrapper
 
variable de entorno de configuración
Con vim Open ~ / .bashrc
WORKON_HOME exportación = ~ / .virtualenvs
/usr/bin/virtualenvwrapper.sh fuente 
o
/usr/python/bin/virtualenvwrapper.sh fuente
ubicación diferentes sistemas virtualenvwrapper.sh puede no ser la misma
Nota: Desde que he instalado cuando la máquina no está pitón, así que tienen que instalar el siguiente python3
virtualenv camino y es virtualenvwrapper
/ Usr / python / bin / virtualenv
/usr/python/bin/virtualenvwrapper.sh
 
Además de enlaces blandos
ln -s / usr / python / bin / virtualenv / usr / bin / virtualenv
Ln -s /usr/python/bin/virtualenvwrapper.sh /usr/bin/virtualenvwrapper.sh
 
source ~ / .bashrc
Ejecutar el archivo .bashrc surtan efecto
 
PPCMs crean un entorno virtual (de acuerdo con las necesidades reales de nomenclatura para un entorno virtual)
mkvirtualenv --python = / usr / bin / PPCMs python3

Tres, el medio ambiente Frasco

Cambiar al entorno virtual
PPCMs workon
 
instalación Frasco
pip instalar Frasco
pip instalar pymysql
pip instalar matraz de sqlalchemy
pip instalar pyjwt # jwt 包
pip instalar solicitudes
 
Se puede combinar en
pip instalar Frasco  pymysql matraz de SQLAlchemy solicitudes pyjwt

En cuarto lugar, el despliegue de servicios

instalación Gunicorn
pip instalar gunicorn

Cinco, escritura Gunicorn inicio

gunicorn comando para iniciar
简单地,gunicorn可以通过gunicorn -w 4 -b 127.0.0.1:4000 run:app启动一个Flask应用。其中,
-w 4是指预定义的工作进程数为4,
-b 127.0.0.1:4000指绑定地址和端口
run是flask的启动python文件,app则是flask应用程序实例
 
对应的gunicorn.py配置文件
# gunicorn.py

debug = True

daemon = True

bind = '127.0.0.1:8001'      # 绑定ip和端口号
# backlog = 512                # 监听队列
chdir = '/data/ppcms/ppcms'  # gunicorn要切换到的目的工作目录
timeout = 30      # 超时
# worker_class = 'gevent' # 使用gevent模式,还可以使用sync 模式,默认的是sync模式

# workers = multiprocessing.cpu_count() * 2 + 1    # 进程数
# threads = 2 # 指定每个进程开启的线程数
loglevel = 'debug'   # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'    # 设置gunicorn访问日志格式,错误日志无法设置

"""
其每个选项的含义如下:
h          remote address
l          '-'
u          currently '-', may be user name in future releases
t          date of the request
r          status line (e.g. ``GET / HTTP/1.1``)
s          status
b          response length or '-'
f          referer
a          user agent
T          request time in seconds
D          request time in microseconds
L          request time in decimal seconds
p          process ID
"""

pidfile = "/data/log/ppcms.pid"
accesslog = "/data/log/gunicorn_ppcms_access.log"      # 访问日志文件
errorlog = "/data/log/gunicorn_ppcms_error.log"        # 错误日志文件
View Code
 

六、nginx配置

upstream到本地指定端口即可。
location / {
    proxy_pass http://127.0.0.1:8000 ; # 这里是指向gunicorn host的服务地址
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
}
 

Supongo que te gusta

Origin www.cnblogs.com/mazhiyong/p/12579671.html
Recomendado
Clasificación