Django上线部署之uWSGI
环境:
1.CentOS 7.2 64位
2.SQL Server 2016 Enterprise 64位
3.Python 3.6.5 64位
4.root用户
要求:
按照顺序部署
1.Windows Server 2016 Datacenter 64位 操作系统下安装数据库
2.CentOS 7.2 下 Python环境搭建

1 yum -y install gcc gcc-c++ zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel tree 2 wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz && tar xf Python-3.6.5.tar.xz 3 cd Python-3.6.5 && ./configure --prefix=/opt/python365 --enable-shared && make && make install 4 /opt/python365/bin/python3.6 -V && ln -sf /opt/python365/bin/python3.6 /usr/local/bin/python3 && ln -sf /opt/python365/bin/pip3.6 /usr/local/bin/pip3 5 pip3 -V && pip3 install virtualenv && ln -sf /opt/python365/bin/virtualenv /usr/local/bin/virtualenv && pip3 install virtualenvwrapper 6 pip3 -V && pip3 install ipython && ln -sf /opt/python365/bin/ipython3 /usr/local/bin/ipython 7 echo "PS1=\"[\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[33;40m\]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \[\e[36;40m\]\w\[\e[0m\]]\\\\$ \n\"" >> /etc/bashrc 8 echo "set nu" >> /etc/vimrc 9 echo "export WORKON_HOME=~/envs" >> /etc/bashrc 10 echo "export VIRTUALENVWRAPPER_HOOK_DIR=~/envs" >> /etc/bashrc 11 echo "export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3" >> /etc/bashrc 12 echo "source /opt/python365/bin/virtualenvwrapper.sh" >> /etc/bashrc
3.安装ODBC for SQL Server

1 # 安装ODBC for SQL Server 驱动 2 # 实际上是安装如下三个包 3 # https://packages.microsoft.com/rhel/7/prod/unixODBC-2.3.7-1.rh.x86_64.rpm 4 # https://packages.microsoft.com/rhel/7/prod/msodbcsql-13.1.9.2-1.x86_64.rpm 5 # https://packages.microsoft.com/rhel/7/prod/unixODBC-devel-2.3.7-1.rh.x86_64.rpm 6 curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo 7 yum remove unixODBC-utf16 unixODBC-utf16-devel 8 ACCEPT_EULA=Y yum -y install msodbcsql && yum -y install unixODBC-devel 9 10 # 安装连接数据库的工具,一般项目上用不到,可不安装 11 ACCEPT_EULA=Y yum install mssql-tools && ln -sf /opt/mssql-tools/bin/bcp /usr/local/bin/bcp && ln -s /opt/mssql-tools/bin/sqlcmd /usr/local/bin/sqlcmd 12 13 # 以下是Django的settings.py中数据库配置 14 DATABASES = { 15 'default': { 16 'ENGINE': 'sql_server.pyodbc', 17 'NAME': 'db', 18 'USER': 'user', 19 'PASSWORD': 'pwd', 20 'HOST': 'ip', 21 'PORT': '1433', 22 'OPTIONS': { 23 'driver': 'ODBC Driver 13 for SQL Server', 24 }, 25 }, 26 } 27 28 DATABASE_CONNECTION_POOLING = True
4.安装uwsgi和uwsgitop(查看uwsgi状态的工具,如果使用securecrt 连接centos,终端需设置成 xterm)

1 pip install uwsgi uwsgitop && ln -sf /opt/python365/bin/uwsgi /usr/local/bin/uwsgi && ln -sf /opt/python365/bin/uwsgitop /usr/local/bin/uwsgitop 2 # uwsgi 配置如下: 3 [uwsgi] 4 #用户属组ID 5 gid=1000 6 #用户ID 7 uid=1000 8 #启用主进程 9 master=true 10 #主进程以root用户启动 11 master-as-root=true 12 #4个进程 13 processes=4 14 #每个进程启用5个线程 15 threads=5 16 #停止运行后清理动态生成的文件 17 vacuum=true 18 #修改当前工作路径,即项目根目录 19 chdir=/srv/www/ 20 #wsgi文件路径,可以是相对路径(相对chdir),可以是绝对路径 21 wsgi-file=www/wsgi.py 22 #采用unixsocket通信 23 socket=%(chdir)uwsgi.sock 24 #采用unixsocket通信时,文件权限 25 chmod-socket=666 26 #配合uwsgitop使用,命令:uwsgitopuwsgi.stats查看uWSGI运行状态 27 stats=%(chdir)uwsgi.stats 28 #uwsgi --[reload|stop] uwsgi.pid 29 pidfile=%(chdir)uwsgi.pid 30 #以后台模式运行,并且将日志写入文件 31 daemonize=%(chdir)uwsgi.log 32 #设置日志文件最大为10MB,最后一次日志输出超过这个值则分割 33 log-maxsize=10000000 34 #打印日志添加日期时间前缀 35 log-date=%%F%%H:%%M:%%S 36 #禁用请求日志,只记录交互日志和错误日志,请求日志可在nginx中记录 37 disable-logging=true 38 #请求日志添加内存和虚拟内存信息,disable-logging是false时生效 39 #memory-report=true 40 #Python启用线程,由于GIL的限制,可能没啥用,未测试 41 enable-threads=true 42 #socket监听数,不能超过系统中net.core.somaxconn值 43 listen=1024 44 #主进程死了,其它进程一起死 45 no-orphans=true 46 #进程请求总数累计超过这个值则重启,用reload-on-as和reload-on-rss替换 47 #max-requests=1024 48 #(单位:MB)进程虚拟内存超过限制则重启 49 reload-on-as=1024 50 #(单位:MB)进程物理内存超过限制则重启 51 reload-on-rss=1024 52 #超时重启进程 53 harakiri=60 54 #超时重启进程后打印日志 55 harakiri-verbose=true 56 #(单位:B) 57 buffer-size=32768 58 #(单位:B) 59 post-buffering=32768 60 #(单位:B)限制HTTP请求体60MB 61 limit-post=60000000 62 #静态文件路由 63 static-map=/static=%(chdir)static 64 static-map=/media=%(chdir)media
5.安装nginx

1 wget http://nginx.org/download/nginx-1.16.1.tar.gz 2 tar -zxvf nginx-1.16.1.tar.gz 3 cd nginx-1.16.1/ && ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_gzip_static_module && make && make install 4 /opt/nginx/sbin/nginx -V && ln -sf /opt/nginx/sbin/nginx /usr/local/bin/nginx 5 6 # nginx.conf配置如下: 7 pid logs/nginx.pid; 8 user chimoph chimoph; 9 10 worker_processes 4; 11 worker_rlimit_nofile 65535; 12 13 events { 14 use epoll; 15 worker_connections 1024; 16 } 17 18 http { 19 include mime.types; 20 default_type application/octet-stream; 21 22 server_tokens off; 23 24 sendfile on; 25 tcp_nopush on; 26 keepalive_timeout 65; 27 28 limit_conn_zone $binary_remote_addr zone=conn_ip:10m; 29 limit_req_zone $binary_remote_addr zone=req_ip:10m rate=5r/s; 30 limit_conn_log_level info; 31 limit_req_status 503; 32 33 gzip on; 34 gzip_vary on; 35 gzip_static on; 36 gzip_min_length 10k; 37 gzip_comp_level 1; 38 gzip_http_version 1.0; 39 gzip_types *; 40 41 log_format main '$time_iso8601|$http_x_forwarded_for|$remote_addr|$remote_user|' 42 '$request_length|$body_bytes_sent|$request_time|$upstream_response_time|' 43 '$status|"$request"|"$http_referer"|"$http_user_agent"'; 44 45 server { 46 listen 80; 47 server_name 192.168.70.110; 48 charset utf-8; 49 client_max_body_size 60M; 50 51 location /media { 52 alias /srv/www/media; 53 access_log logs/static.log main; 54 expires 30d; 55 } 56 57 location /static { 58 alias /srv/www/static; 59 access_log logs/static.log main; 60 expires 30d; 61 } 62 63 location / { 64 uwsgi_pass unix:///srv/www/uwsgi.sock; 65 include uwsgi_params; 66 access_log logs/access.log main; 67 limit_conn conn_ip 10; 68 limit_req zone=req_ip burst=5; 69 } 70 71 error_page 500 502 503 504 /50x.html; 72 location = /50x.html { 73 root html; 74 } 75 } 76 }
6.安装Redis

1 wget http://download.redis.io/releases/redis-4.0.14.tar.gz 2 tar -zxvf redis-4.0.14.tar.gz && cd redis-4.0.14 && make && make PREFIX=/opt/redis install 3 /opt/redis/bin/redis-cli -v && ln -sf /opt/redis/bin/redis-cli /usr/local/bin/redis-cli 4 /opt/redis/bin/redis-server -v && ln -sf /opt/redis/bin/redis-server /usr/local/bin/redis-server 5 6 # [redis.conf] 如下 7 dir /opt/redis/ 8 loglevel notice 9 logfile redis.log 10 pidfile redis.pid 11 timeout 0 12 protected-mode yes 13 bind 127.0.0.1 14 port 6379 15 tcp-backlog 128 16 tcp-keepalive 0 17 databases 8 18 daemonize yes 19 supervised no 20 save 900 1 21 save 300 10 22 save 60 10000 23 stop-writes-on-bgsave-error yes 24 rdbcompression yes 25 rdbchecksum yes 26 dbfilename dump.rdb 27 maxclients 128 28 maxmemory 1gb 29 maxmemory-policy noeviction 30 slowlog-log-slower-than 10000
7.启动
1 redis-server /opt/redis/redis.conf && uwsgi /srv/www/uwsgi.ini && nginx
8.安装supervisor
1 yum install epel-release && yum install -y supervisor && systemctl enable supervisord && systemctl start supervisord && systemctl status supervisord