虚拟环境介绍

virtualenvwrapper

对virtualemv的包装

  • mkvirtualenv 创建虚拟环境
  • rmvirtualenv    删除虚拟环境
  • workon    进入虚拟环境
  • deactivate 退出虚拟环境

    virtualenv

  • virtualenv 创建虚拟环境
  • source /xx/xx/activate 激活/进入虚拟环境
  • deavtivate 退出虚拟环境
  • rm -rf 虚拟环境名字 删除虚拟环境

指令在哪调用,虚拟环境就在哪里生成:
优点:可将依赖环境直接放在项目内部,这样将项目迁移时不需要重新安装环境
缺点:对于已有运行环境的开发者本地之间传输项目,会将环境依赖包一起传输,导致文件编码,而核心需要文件只占一小部分,产生比较大的冗余。

部署云服务器大致流程

  • 从0开始搭建
  • 安装云服务器系统

    • ccentos8.0
  • 一套开发环境

    • python3
    • pip
    • virtualenv
    • mysql

      • dnf直接安装
    • redis

      • 源码安装
    • nginx
  • 准备进行部署

    • 安装项目所需依赖

      • pip3 install -r xxx.txt
    • 修改配置文件到指定路径
    • 从静态文件开始部署
    • 动态资源

      • 处理好数据库
      • 创建库,创建表
      • 导入数据
  • 坑点

    • 使用SSL465端口发送邮件,注意邮箱不能再使用之前的25端口,因为不安全,所以大部分云计算厂商都禁止25端口发送邮件,所以需要使用安全模式发送邮件。

    导出环境依赖信息

    pip freeze> requirements.txt

部署需要先将环境配置好,python,mysql,redis

python安装

python

先看看系统有没有自带python3.

yum install python36

安装虚拟环境

教程:https://www.cnblogs.com/st-st/p/10251449.html

sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pbr
sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-deps stevedore
sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-deps virtualenvwrapper
pip3 install virtualenvwrapper

查看python3的文件和virtualenvwrapper.sh的路径

[root@localhost ~]# whereis python3
python3: /usr/bin/python3 /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3.6-config /usr/bin/python3.6m-config /usr/bin/python3.6m-x86_64-config /usr/lib/python3.6 /usr/lib64/python3.6 /usr/local/bin/python3 /usr/include/python3.6m /usr/local/python3 /usr/share/man/man1/python3.1.gz
[root@localhost ~]# find / -name virtualenvwrapper.sh
/usr/local/python3/bin/virtualenvwrapper.sh

创建虚拟环境

[root@localhost ~]# pwd
/root
[root@localhost ~]# mkdir .virtualenvs

配置环境变量参数

先找到刚刚下载的virtualenvwrapper文件路径

[root@localhost ~]# find / -name virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper.sh

然后打开环境变量文件

vim .bashrc 

添加参数

# python virtualenv
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=/root/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

然后保存退出
激活环境变量source .bashrc

[root@localhost bin]# source ~/.bashrc
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details

使用virtualenvwrapper

使用mkvirtualenv venv1 -p /usr/bin/python3创建python3的虚拟环境

[root@localhost ~]# mkvirtualenv venv1 -p /usr/bin/python3
created virtual environment CPython3.6.8.final.0-64 in 699ms
  creator CPython3Posix(dest=/root/Envs/venv1, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, wheel=bundle, setuptools=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==20.2.2, setuptools==49.6.0, wheel==0.35.1
  activators PythonActivator,FishActivator,XonshActivator,CShellActivator,PowerShellActivator,BashActivator
virtualenvwrapper.user_scripts creating /root/Envs/venv1/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/Envs/venv1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/Envs/venv1/bin/preactivate
virtualenvwrapper.user_scripts creating /root/Envs/venv1/bin/postactivate
virtualenvwrapper.user_scripts creating /root/Envs/venv1/bin/get_env_details
(venv1) [root@localhost ~]#

终于OJBK了,WDNMD环境问题就NM离谱。

MYSQL安装

文档:
https://www.cnblogs.com/kasnti/p/11929030.html

Redis安装

Installation

Download, extract and compile Redis with:

yum -y install gcc gcc-c++ kernel-devel tcl
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar xzf redis-4.0.11.tar.gz
cd redis-4.0.11
make

然后使用make test测试编译:

接着我们进入redis的utils文件夹:运行install_server.sh文件

[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

如果遇到以下问题:

[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [] 
Mmmmm...  it seems like you don't have a redis executable. Did you run make install yet?

看这个文档:https://blog.csdn.net/weixin_33913377/article/details/89010112

测试:

[root@localhost src]# ./redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit

项目部署在var文件夹中,先新建/var/www

[root@localhost redis-4.0.11]# mkdir /var/www/

Nginx安装

dnf install nginx

修改项目中的nginx配置文件

config.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
    listen       80;
    server_name  localhost;

    root   /var/www/PeiQi1;

    location /static {

        alias /var/www/PeiQi1/static;
    }
   
    location / {
    include /etc/nginx/uwsgi_params;
    uwsgi_pass 127.0.0.1:8888;
    }

}

}

启动项目的nginx:

[root@localhost PeiQi1]# nginx -t -c /var/www/PeiQi1/config.conf 
nginx: the configuration file /var/www/PeiQi1/config.conf syntax is ok
nginx: configuration file /var/www/PeiQi1/config.conf test is successful
[root@localhost PeiQi1]# nginx -c /var/www/PeiQi1/config.conf 

访问对应的静态资源:

安装项目的环境包

[root@localhost PeiQi1]# workon venv1
(venv1) [root@localhost PeiQi1]# pip3 install -r requirements.txt -i https://pypi.douban.com/simple/

uwsgi

下载uwsgi:

dnf install python36-devel
pip3 install uwsgi

根据进程名关闭进程 sudo kill -9 $(pidof uwsgi)

先修改uwsgi.ini参数:

[uwsgi]
# 使用nginx连接时 使用
socket=0.0.0.0:8888

# 直接作为web服务器使用
#http=0.0.0.0:8888
# 配置工程目录
chdir=/var/www/PeiQi1

# 配置项目的wsgi目录。相对于工程目录
wsgi-file=PeiQi1/wsgi.py

#配置进程,线程信息
processes=4

threads=10

enable-threads=True
# 主从结构
master=True
# 进程id存储文件
pidfile=uwsgi.pid
# 日志文件
daemonize=uwsgi.log

启动uwsgi:

(venv1) [root@localhost PeiQi1]# uwsgi --uwsgi --ini uwsgi.ini 
[uWSGI] getting INI configuration from uwsgi.ini
(venv1) [root@localhost PeiQi1]# ps -ef | grep uwsgi
root      23871      1  0 15:44 ?        00:00:00 uwsgi --uwsgi --ini uwsgi.ini
root      23873  23871  0 15:44 ?        00:00:00 uwsgi --uwsgi --ini uwsgi.ini
root      23874  23871  0 15:44 ?        00:00:00 uwsgi --uwsgi --ini uwsgi.ini
root      23875  23871  0 15:44 ?        00:00:00 uwsgi --uwsgi --ini uwsgi.ini
root      23876  23871  0 15:44 ?        00:00:00 uwsgi --uwsgi --ini uwsgi.ini
root      23927  22940  0 15:44 pts/0    00:00:00 grep --color=auto uwsgi

注意:在linux系统中的pymysql需要从__init__.py中写进去。

数据库

云服务器安全组要放行端口

建数据库表

mysql> create database PeiQi1 charset=utf8;
Query OK, 1 row affected (0.00 sec)

迁移数据库:

(venv1) [root@localhost PeiQi1]# python manage.py migrate

远程连接mysql数据库,插入数据:

卡了三天的BUG总结

日志系统要常看

uwsgi部署的log日志没注意细看,导致错误不知道怎么排查

python版本混乱

由于多个python版本杂糅在物理机上,导致启动项目时使用时很混乱

运维常用命令不熟

ps -ef |grep nginx
防火墙
selinux
vim

真实生产环境

公司开发中标配四套环境。

  • 开发环境
  • 测试环境
  • 演示环境
  • 线上环境

    简单压力测试

    使用apachebench来对网站进行压力测试。
    yum install httpd-tools  

ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。
ab命令对发出负载的计算机要求很低,既不会占用很高CPU,也不会占用很多内存,但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也须注意,否则一次上太多的负载,可能造成目标服务器因资源耗完,严重时甚至导致死机。

我们输入ab:

[root@localhost ~]# ab
ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make at a time
    -t timelimit    Seconds to max. to spend on benchmarking
                    This implies -n 50000
    -s timeout      Seconds to max. wait for each response
                    Default is 30 seconds
    -b windowsize   Size of TCP send/receive buffer, in bytes
    -B address      Address to bind to when making outgoing connections
    -p postfile     File containing data to POST. Remember also to set -T
    -u putfile      File containing data to PUT. Remember also to set -T
    -T content-type Content-type header to use for POST/PUT data, eg.
                    'application/x-www-form-urlencoded'
                    Default is 'text/plain'
    -v verbosity    How much troubleshooting info to print
    -w              Print out results in HTML tables
    -i              Use HEAD instead of GET
    -x attributes   String to insert as table attributes
    -y attributes   String to insert as tr attributes
    -z attributes   String to insert as td or th attributes
    -C attribute    Add cookie, eg. 'Apache=1234'. (repeatable)
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
                    Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes
                    are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
                    are a colon separated username and password.
    -X proxy:port   Proxyserver and port number to use
    -V              Print version number and exit
    -k              Use HTTP KeepAlive feature
    -d              Do not show percentiles served table.
    -S              Do not show confidence estimators and warnings.
    -q              Do not show progress when doing more than 150 requests
    -l              Accept variable document length (use this for dynamic pages)
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -r              Don't exit on socket receive errors.
    -m method       Method name
    -h              Display usage information (this message)
    -I              Disable TLS Server Name Indication (SNI) extension
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL2, TLS1, TLS1.1, TLS1.2 or ALL)
    -E certfile     Specify optional client certificate chain and private key

可以看到可以附加这么多参数

ab [网址]:可以测试访问这个页面的具体情况

[root@localhost ~]# ab http://47.99.192.206/peiqi1/home/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 47.99.192.206 (be patient).....done


Server Software:        nginx/1.14.1
Server Hostname:        47.99.192.206
Server Port:            80

Document Path:          /peiqi1/home/
Document Length:        21336 bytes

Concurrency Level:      1
Time taken for tests:   0.101 seconds
Complete requests:      1
Failed requests:        0
Total transferred:      21525 bytes
HTML transferred:       21336 bytes
Requests per second:    9.88 [#/sec] (mean)
Time per request:       101.236 [ms] (mean)
Time per request:       101.236 [ms] (mean, across all concurrent requests)
Transfer rate:          207.64 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       30   30   0.0     30      30
Processing:    71   71   0.0     71      71
Waiting:       42   42   0.0     42      42
Total:        101  101   0.0    101     101

-n参数可以多次访问网页

[root@localhost ~]# ab -n 100 http://47.99.192.206/peiqi1/home/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 47.99.192.206 (be patient)...apr_pollset_poll: The timeout specified has expired (70007)
Total of 62 requests completed

可以看到怼了100次,只有62次成功了(辣鸡)。

最后修改:2024 年 03 月 13 日
如果觉得我的文章对你有用,请随意赞赏