一、nginx安装部署 |
官方文档:http://nginx.org/
安装步骤:
#!/bin/bash nginx_version=nginx-1.12.0 if [ -f "/usr/bin/wget" ];then echo "开始下载nginx...." wget http://nginx.org/download/$nginx_version.tar.gz if [ -f "./$nginx_version.tar.gz" ]; then echo "$nginx_version下载完毕!" fi echo "开始安装nginx依赖包......" yum install g++ gcc openssl-devel pcre-devel zlib-devel -y echo "解压nginx·......" tar xzvf $nginx_version.tar.gz echo "开始编译........" cd $nginx_version ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module make && make install echo "nginx 安装完毕!" else echo "wget is not found!" fi
启动与停止nginx:
####启动 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ####停止 /usr/local/nginx/sbin/nginx -s stop
二、uwsgi安装配置 |
官方文档:http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/Configuration.html
安装:
###方法一 pip3 install uwsgi ###方法二: python3 -m pip install uwsgi
配置uwsgi:uwsgi可支持命令行启动、json格式配置文件启动、ini格式配置文件启动、xml配置文件启动
命令行启动:可以使用uwsgi --help查看启动选项
uwsgi --http :8000 --chdir /opt/app/devops --wsgi-file /opt/app/devops/devops/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
ini配置文件启动:
常用配置参数
http : #协议类型和端口号 processes : #开启的进程数量 workers : #开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes) chdir : #指定运行目录(chdir to specified directory before apps loading) wsgi-file : #载入wsgi-file(load .wsgi file) stats : #在指定的地址上,开启状态服务(enable the stats server on the specified address) threads : #运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads) master : #允许主进程存在(enable master process) daemonize : #使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。 pidfile : #指定pid文件的位置,记录主进程的pid号。 vacuum : #当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
log-maxsize :#记录日志配置最大多少,超过这个数会切割,单位kb logto :#指定输出日志到文件logto = /tmp/uwsgi.log
在django项目中与manage.py同级目录创建配置文件,这里命名为uwsgi.ini
# uwsgi 配置文件 [uwsgi] #端口 socket = :8000 # django项目绝对路径 chdir = /opt/app/devops # 模块路径(项目名称.wsgi)可以理解为wsgi.py的位置 module = devops.wsgi # 允许主进程 master = true #最多进程数 processes = 4 # 退出时候回收pid文件 vacuum = true #日志大小配置500M log-maxsize = 500000000 #记录日志配置 logto = /tmp/uwsgi.log
启动uwsgi服务
uwsgi --ini /opt/app/devops/uwsgi.ini --daemonize /tmp/uwsgi.log
三、配置nginx |
nginx主要位置是代理转发作用
nginx.conf
#user nobody; worker_processes 2; #error_log logs/error.log; #error_log logs/error.log notice; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; access_log logs/access.log ; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name 10.193.15.50; charset UTF-8; error_log logs/devops_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; uwsgi_read_timeout 5; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /opt/app/devops/static/; } } }
启动nginx,访问项目url若出现Exception Value: Invalid HTTP_HOST header异常 ,需要在settings.py中ALLOWED_HOSTS = ['*']
重启脚本centos6.x
#!/bin/bash if [ ! -n "$1" ] then echo "Usages: sh uwsgiserver.sh [start|stop|restart]" exit 0 fi if [ $1 = start ] then psid=`ps aux | grep "uwsgi" | grep -v "grep" | wc -l` if [ $psid -gt 4 ] then echo "uwsgi is running!" exit 0 else uwsgi /xxx/www/uwsgi.ini --daemonize /var/log/uwsgi.log --post-buffering 32768 --buffer-size 32768 echo "Start uwsgi service [OK]" fi elif [ $1 = stop ];then killall -9 uwsgi echo "Stop uwsgi service [OK]" elif [ $1 = restart ];then killall -9 uwsgi uwsgi --ini /xxx/www/uwsgi.ini --daemonize /var/log/uwsgi.log --post-buffering 32768 --buffer-size 32768 --touch-reload "/xxx/www/reload.set" echo "Restart uwsgi service [OK]" else echo "Usages: sh uwsgiserver.sh [start|stop|restart]" fi