Python代码部署

编译安装python3

# 安装依赖包

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

# 查看Linux默认安装的Python位置
whereis python


# 下载python3 安装包
wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz
xz -d Python-3.6.7.tar.xz  #解压xz
tar -xf Python-3.6.7.tar   #解压tar包

# 环境监测,和安装目录指定,进入安装包输入下列内容(目录自行选择)
./configure --prefix=/usr/local/python3/


#上一步生成makefile   linux使用gcc工具进行编译
make
 
# 进行安装
make install

# 安装完,环境变量配置
vim /etc/profile

'''
#/usr/local/python3 放在最前面,这样当使用django-admin会默认使用python3项目的
        
export PATH=/usr/local/python3/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/python3/bin/:/root/bin
'''

# 更新环境变量
source /etc/profile

 

source执行

在当前bash环境下读取并执行FileName中的命令。
*注:该命令通常用命令“.”来替代。

 

通过pip查看python版本

centos7自带 python2 不要写在,不然会报错

(app01) [root@nb-qic ~]# whereis pip
pip: /root/Envs/app01/bin/pip /root/Envs/app01/bin/pip3.6 /usr/local/python3/bin/pip3.6

(app01) [root@nb-qic ~]# which pip3
/root/Envs/app01/bin/pip3

(app01) [root@nb-qic ~]# pip3 --version
pip 20.2.3 from /root/Envs/app01/lib/python3.6/site-packages/pip (python 3.6)

 

 

部署虚拟化环境

安装命令

#装virtualenv wrapper
pip3 install -i https://pypi.douban.com/simple virtualenvwrapper

 

centos7

# 进入vim ~/.bash_profile

export WORKON_HOME=~/Envs   #设置virtualenv的统一管理目录
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'     #生成干净隔绝的环境
export VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3 #指定python解释器
source /usr/local/python3/bin/virtualenvwrapper.sh                  #执行安装脚本

读取文件,使得生效,此时已经可以使用virtalenvwrapper
source ~/.bash_profile

 

ubuntu14

export WORKON_HOME=~/Envs
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /home/moluo/.local/bin/virtualenvwrapper.sh

读取文件,使得生效,此时已经可以使用virtalenvwrapper
source ~/.bashrc 

 

常用命令方法

mkvirtualenv mkdjango2   #创建虚拟环境目录
lsvirtualenv             #列出虚拟环境
workon mkdjango2         #激活或切换虚拟环境
lssitepackages           #列出当前虚拟环境安装的模块 
cdvirtualenv             #进入虚拟环境家目录
deactivate               #退出

 

虚拟环境问题

使用virtualenv --version,看到自己的版本大于20,就可以将如下这段删除:

export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'

因为从版本20开始,默认就是’--no-site-packages‘了。

 

安装数据库导入数据

# cmd备份数据
mysqldump -uroot -p crm_login > crm_login.sql

# 命令导入数据
  # 先创建数据库crm_login
  # 导入数据:mysql -uroot -p crm_login < crm_login.sql
# 登录数据库导入:
 use crm_login
 source /opt/crm_login.sql

 

创建虚拟环境安装项目依赖环境

mkvirtualenv crm
# 依赖环境
  # 启动命令监测环境所缺依赖:python manage.py  runserver 0.0.0:8000
  pip3 install -i https://pypi.douban.com/simple django==1.11.23
  pip3 install -i https://pypi.douban.com/simple pymysql
  pip3 install -i https://pypi.douban.com/simple django-multiselectfield
  pip3 install -i https://pypi.douban.com/simple django==1.11.23
# 便捷方法,导出python模块到文件
  pip3 freeze > requirements.txt 

 

安装uwsgi

# pip3安装
pip3 install -i https://pypi.douban.com/simple   uwsgi 


#uwsgi通过配置文件启动
#在项目中创建uwsgi.ini
touch uwsgi.ini

uwsgi --ini uwsgi.ini
netstat -lntp#显示网络连接,路由表,接口状态,伪装连接,网络链路信息

 

uwsgi配置文件

uwsgi启动django--------》settings---allowhost=["*"]

socket与http协议(存在一个,否则冲突)

在虚拟机中访问项目,如是127.0.0.1的地址想在本地浏览器访问需

# vim /etc/hosts 新增
127.0.0.1 www.aaa.com # 域名和nginx中配置的是同一个

 

socket反向代理配置

当uwsgi配置socket时,外部浏览器将无法直接访问,会提示类似于“缓存区(buffer)超过最大长度”类似的问题
此时使用nginx进行代理时,需要如下设置项:

[uwsgi]

# 填写crm项目的绝对路径,第一层 
chdir           = /home/zrq/Desktop/celery1

# Django's wsgi file 
#填写crm项目第二层目录中的wsgi文件 
module          = celery1.wsgi

#填写解释器的安装绝对路径(虚拟环境)
# home            = /root/Envs/crm
# master          = true

#指定uwsgi的多进程数量,指定为cpu的核数即可(填cpu的4倍数量)
processes       = 4

#指定crm启动的协议,当你和nginx结合进行反向代理,就用unix-socket协议 ,
#这种方式无法直接访问,只能通过nginx反代,还有一种http协议,可以直接启动项目
socket          = 0.0.0.0:8000

#指定http协议启动,不安全,没有意义,只是自己调试使用
#http =  0.0.0.0:8000
vacuum          = true
# 数据包大小
buffer-size = 65536
# 进程和日志
pidfile=uwsgi.pid
#(主管理进程的编号位子)
daemonize=uwsgi.log
#(记录日志文件)

 

nginx中的配置

# 注意这里的uwsgi_param是自己创建的,如果不存在请 
# touch uwsgi_param   创建
server{
  litsen 80;
  server_name www.aaa.com # 这里配置域名也可配置ip
  location / {
    include uwsgi_param;
    uwsgi_pass 127.0.0.1:8000; // socket对应的本地端口,注意socket不需要写http:// 因为socket不是HTTP的具体实现
  }
}

在外部访问时请访问80,80为监听端口 例如 127.0.01:80

 

 

htpp

[uwsgi]

# 填写crm项目的绝对路径,第一层 
chdir           = /home/zrq/Desktop/celery1

# Django's wsgi file 
#填写crm项目第二层目录中的wsgi文件 
module          = celery1.wsgi

#填写解释器的安装绝对路径(虚拟环境)
# home            = /root/Envs/crm
# master          = true

#指定uwsgi的多进程数量,指定为cpu的核数即可(填cpu的4倍数量)
processes       = 4

#指定crm启动的协议,当你和nginx结合进行反向代理,就用unix-socket协议 ,
#这种方式无法直接访问,只能通过nginx反代,还有一种http协议,可以直接启动项目
http          = 0.0.0.0:8000

#指定http协议启动,不安全,没有意义,只是自己调试使用
#http =  0.0.0.0:8000
vacuum          = true
# 数据包大小
buffer-size = 65536
# 进程和日志
pidfile=uwsgi.pid
#(主管理进程的编号位子)
daemonize=uwsgi.log
#(记录日志文件)

 

nginx中的配置

一般我们在配置python flask应用时都是使用http,让Nginx直接使用proxy_pass就把它从本地代理到前端服务器
当uwsgi配置http=127.0.0.1:8000时,我们可以如下设置Nginx进行代理

server{
  listen 80;
  location / {
    proxy_pass http://127.0.0.1:8000/  // 必须加http://
  }
}

 

uwsgi启动报错

'''onError('<urllib3.connection.HTTPConnection object at '''

# 解决办法 环境变量指定
vim ~/.bashrc
export LD_LIBRARY_PATH=/application/search/anaconda3/lib:$LD_LIBRARY_PATH
source ~/.bashrc 

 

 

nginx

nginx优点

支持高并发,能支持几万并发连接
资源消耗少,在3万并发连接下开启10个nginx线程消耗的内存不到200M
可以做http反向代理和负载均衡
支持异步网络i/o事件模型epoll

 

nginx安装依赖包

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

#依赖包说明
gcc         源码进行编译
pcre-devel  http模块使用pcre解析正则表达式
zlib        nginx使用zlib对http包进行gzip压缩
OpenSSL     密钥证书管理,nginx支持https 需要使用openssl

 

yum安装nginx

# yum中默认是不存在nginx的我们需要更换源
# 需要创建一个“/etc/yum.repos.d/nginx.repo”的文件,其实就是新增一个 yum 源
vim /etc/yum.repos.d/nginx.repo

#然后将下面的内容复制进去:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

#然后保存“/etc/yum.repos.d/nginx.repo”文件后,我们就使用 yum 命令查询一下我们的 nginx 的 yum 源配置好了没有。
例如:
[root@niaoyun~]# yum list |grep nginx
nginx.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx
nginx-debuginfo.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-module-geoip.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-module-image-filter.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-module-njs.x86_64 1:1.10.1.0.0.20160414.1c50334fbea6-1.el7.ngx
nginx
nginx-module-perl.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-module-xslt.x86_64 1:1.10.1-1.el7.ngx nginx
nginx-nr-agent.noarch 2.0.0-9.el7.ngx nginx
pcp-pmda-nginx.x86_64 3.10.6-2.el7 base

# 如果执行命令是这样的显示效果,那么我们的 nginx 的 yum 源就配置成功啦!
# 然后要安装我们的 nginx 就直接执行
yum -y install nginx

# 如果操作正确到这里 nginx 环境就已经安装 OK 了
# 当然你还可以使用命令查一下究竟是否安装完成。
[root@niaoyun~]# rpm -q nginx
nginx-1.10.1-1.el7.ngx.x86_64

 

编辑配置文件nginx.conf

注意server{}多虚拟主机是自上而下的加载顺序
因此修改第一个server{}虚拟主机,修改为如下反代的写法

且添加nginx处理django的静态文件方式
        server {
            #nginx监听的地址 
            listen       80;
            #定义网站的域名
            server_name  www.s23lol.com;

            #charset koi8-r;
            #nginx的url匹配 , /这个斜杠就代表这样的请求:  192.168.13.117:85/   
            #这个是最低级匹配,所有的请求都会进入location,进行处理
            #好比 http://192.168.13.117/crm/login/
            location / {
                #基于uwsgi协议的请求转发,给后端django的启动地址
                uwsgi_pass 0.0.0.0:8000;    #这个是请求转发
                include  uwsgi_params;      #这个是添加一个文件,添加请起头信息的
            }
            #我现在想处理这样的请求              
            #我可以这样做
            #当以后请求是从static开始,我就让他去这个目录去找
            #http://192.168.13.117/static/css/reset.css
            #不加斜杠
            location  /static  {
                    # 这里没有写http或者socket的配置,依据uwsgi情况而定
                    alias  /opt/crms23/crmstatic;       # 静态文件目录                                
            }
           
            
        }

 

nginx常用命令

1、帮助命令查看全部参数

[root@localhost opt]# nginx -h
nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /etc/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file
2、常用命令

#重载配置文件,修改配置文件必须重载才能生效
#nginx -s reload 
#优雅的停止服务
#nginx -s quit
#立刻停止服务
#nginx -s stop
#查看版本
#nginx -v 或者nginx -V
#执行配置文件启动
# nginx -c /etc/nginx/nginx.conf

 

负载均衡

两台服务器

192.168.13.181    Nginx负载均衡反向代理服务器
192.168.13.182    资源地址(模拟django程序)
192.168.13.183    资源地址(模拟django程序)

负载均衡机制

#默认轮训机制,平均分配

#weight权重机制
server   192.168.13.192 weight=5;

#ip_hash机制
upstream  home{
    server 192.168.13.182;
  server 192.168.13.183;
    ip_hash;
}

192.168.13.181 负载均衡实现代码

upstream home{                              #设置代理的ip地址
        server 192.168.13.182;
        server 192.168.13.183;
}

    server {
        listen       80 default_server;
        server_name  _;

        location / {                     #访问到虚拟主机,通过proxy_pass跳转分发到upstream
        proxy_pass http://home;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

 

启动uwsgi和nginx

uwsgi启动:uwsgi --ini uwsgi.ini
nginx启动 三种方法:
找到nginx下的sbin
./nginx # 启动
第一种:

# nginx -c /etc/nginx/nginx.conf
第二种:

# service nginx start
第三种(centos7)

# systemctl start nginx.service

 

vue项目部署

前端首页部署

# 获取首页代码
# wget https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip
# 解压缩后,修改/opt/luffys23/07-luffy_project_01/src/restful/api.js文件 地址
  # 替换内容 -- 替换后的内容
  # sed -i "s/127.0.0.1:8000/192.168.13.182:8001/g"  api.js  

 

配置nodejs环境

yum install nodejs

# 源码安装
#下载node源码
wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
#解压缩,由于这是编译好的二进制命令压缩包,直接配置环境变量即可使用
vim /etc/profile
 PATH="/opt/python367/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/opt/tngx232/sbin:/opt/luffys23/node-v8.6.0-linux-x64/bin"
source /etc/profile 

#查看node和npm的版本,是否正常
node -v
npm -v

 

# 开始安装vue项目所需的node模块,默认找到当前文件夹的package.json文件 ,使用淘宝的npm源
# npm --registry https://registry.npm.taobao.org install     #安装vue的 模块 
# 编译vue代码,生成dist
# npm run build 
# 此时生成dist文件夹之后,可以丢给nginx去解析了
              
# 配置nginx.conf如下
# 配置第二个虚拟主机
#第一个虚拟主机,返回路飞的首页内容 
    server {
        listen       81;
        server_name  _;
    
        #当我访问 81的时候,就进入如下的配置
        location / {
                #定义网页根目录 ,填写vue的dist绝对路径
                root /opt/07-luffy_project_01/dist;
                index   index.html;
                try_files $uri $uri/ /index.html;    #这个参数是解决vue刷新404问题的参数 
        }
    }
                                     
#第二个虚拟主机,反向代理将请求转发给drf后台
    server {
        listen       8001;
        server_name  _;

        location / {
                include uwsgi_params;
                uwsgi_pass 0.0.0.0:8002;
               
        }
    }

 

 

Supervisor

# 安装supervisor
yum install supervisor  -y 

# 生成配置文件
echo_supervisord_conf > /etc/supervisord.conf

# 编辑supervisor  添加项目启动路径
[program:s23_crm]  #启动项目名称
                           
; 启动s23crm的完整绝对路径命令
command=/root/Envs/s23_crm/bin/uwsgi --ini /opt/crms23/se_crm/uwsgi.ini  
                           
; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
autorestart=true     
                           
;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
stopasgroup=true
                           
;默认为false,向进程组发送kill信号,包括子进程
killasgroup=true   

#项目配置
[program:s23_crm]
command=/root/Envs/crm/bin/uwsgi --ini /opt/untitled_crm/uwsgi.ini
autorestart=true
stopasgroup=true
killasgroup=true

[program:luffys]
command=/root/Envs/luffys/uwsgi --ini /opt/luffy_boy/uwsgi.ini
autorestart=true
stopasgroup=true
killasgroup=true

 

supervisor 常用命令

- 指定配置文件启动命令
  - supervisord -c /etc/supervisord.conf
- 查看正在守护的进程
  - supervisorctl
- 在supervisorctl中关闭某个项目
  - stop luffys
- 更新配置文件到supervisor
  - supervisorctl update
- 重新启动配置中的所有程序
  - supervisorctl reload
- 停止全部进程
  - supervisorctl stop all
- 启动所有进程
  - supervisorctl start all 
- 停止某一进程 (program_name=你配置中写的程序名称
  - spervisorctl stop program_name

 

posted @ 2020-12-25 14:52  驰念  阅读(300)  评论(0编辑  收藏  举报