django+uwsgi+nginx部署网站

django+uwsgi+nginx部署网站

django+uwsgi+nginx部署网站_第1张图片
静态:nginx直接处理
动态:wsgi或者uwsgi给python处理

uwsgi

web协议,发布python网站。
uwsgi是服务器和服务端应⽤程序的通信协议,规定了怎么把请求转发给应⽤程序和返回
uWSGI实现WSGI协议(应用程序网关接口)

安装uwsgi

pip3 install uwsgi

安装报错:

 In file included from plugins/python/pyutils.c:1:0:
    plugins/python/uwsgi_python.h:2:20: 致命错误:Python.h:没有那个文件或目录
     #include 

安装python-devel 如果是python3则安装python3-devel
yum install python3-devel -y

配置uwsgi

mkdir /etc/uwsgi
vim /etc/uwsgi/uwsgi.ini  

配置信息如下

[uwsgi]
uid = root
gid = root
socket = 127.0.0.1:9090
master = true //启动主进程
vhost = true //多站模式
no-site = true //多站模式时不设置⼊⼝模块和⽂件
workers = 2 //⼦进程数
reload-mercy = 10 //平滑的重启
vacuum = true //退出、重启时清理⽂件
max-requests = 1000 //开启10000个进程后, ⾃动respawn下
limit-as = 512 // 将进程的总内存量控制在512M 
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid //pid⽂件,⽤于下⾯的脚本启动、停⽌该进程
daemonize = /var/log/uwsgi9090.log  
pythonpath = /root/web/lib/python3.6/site-packages/

启动uwsgi

uwsgi --ini /etc/uwsgi/uwsgi.ini

关闭uwsgi

cat /var/run/uwsgi9090.pid   查看uwsgi的pid
kill -9 101445  杀掉uwsgi

编写uwsgi启动脚本

设置uwsgi服务文件,让uwsgi归systemctl管
创建脚本文件

vim /etc/init.d/uwsgi
chmod 755 /etc/init.d/uwsgi

编写脚本

#!/bin/sh
 DESC="uwsgi daemon"
 NAME=uwsgi
 DAEMON=/usr/local/bin/uwsgi
 CONFIGFILE=/etc/uwsgi/$NAME.ini
 PIDFILE=/var/run/${NAME}9090.pid
 SCRIPTNAME=/etc/init.d/$NAME
 FIFOFILE=/tmp/uwsgififo
 set -e
 [ -x "$DAEMON" ] || exit 0
 do_start() {
 if [ ! -f $PIDFILE ];then
 $DAEMON $CONFIGFILE || echo -n "uwsgi running"
 else
 echo "The PID is exist..."
 fi
 }
 do_stop() {
 if [ -f $PIDFILE ];then
 $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
 rm -f $PIDFILE
 echo "$DAEMON STOPED."
 else
 echo "The $PIDFILE doesn't found"
 fi
 }
 do_reload() {
 if [ -p $FIFOFILE ];then
 echo w > $FIFOFILE
 else
 $DAEMON --touch-workers-reload $PIDFILE || echo -n "uwsgi
can't reload"
 fi
 }
 do_status() {
 ps aux|grep $DAEMON
 }
 case "$1" in
 status)
 echo -en "Status $NAME: \n"
 do_status
 ;;
 start)
 echo -en "Starting $NAME: \n"
 do_start
 ;;
 stop)
 echo -en "Stopping $NAME: \n"
 do_stop
 ;;
 reload|graceful)
 echo -en "Reloading $NAME: \n"
 do_reload
 ;;
 *)
 echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
 exit 3
 ;;
 esac
 exit 0

使用脚本

/etc/init.d/uwsgi status  查看uwsgi服务状态  
/etc/init.d/uwsgi start   启动uwsgi服务
/etc/init.d/uwsgi stop    关闭uwsgi服务
/etc/init.d/uwsgi restart  重启uwsgi服务

nginx配置

vim /usr/local/nginx/conf/nginx.conf
即可优化uwsgi服务

server {
      include uwsgi_params;
      access_log   logs/abc.log main;
      uwsgi_cache_valid 1m;
      uwsgi_temp_file_write_size 64k;
      uwsgi_busy_buffers_size 64k;
      uwsgi_buffers 8 64k;
      uwsgi_buffer_size 64k;
      uwsgi_read_timeout 300;
      uwsgi_send_timeout 300;
      uwsgi_connect_timeout 300;
      listen       80;
      server_name  localhost;
      location / {
          include  uwsgi_params;
          uwsgi_pass  127.0.0.1:9090;         
          uwsgi_param UWSGI_SCRIPT baism_web.wsgi;
          uwsgi_param UWSGI_CHDIR  /usr/local/nginx/html/baism_web;
          index  index.html index.htm;
          client_max_body_size 35m;
           }
}

代表django web项目,myweb.wsgi代表myweb/myweb/wsgi.py地址
uwsgi_param UWSGI_SCRIPT baism_web.wsgi;

代表网站位置
uwsgi_param UWSGI_CHDIR /usr/local/nginx/html/baism_web;

django配置

将django文件放到nginx的html目录下
mv myweb /usr/local/nginx/html/

启动

启动nginx   /usr/local/nginx/sbin/nginx
启动uwsgi    /etc/init.d/uwsgi start
netstat -ntpl  检查端口是否开启

注意:
修改nginx配置文件后要重启nginx
修改django,uwsgi配置文件后要重启uwsgi

systemctl restart nginx  重启nginx 
systemctl restart uwsgi  重启uwsgi

你可能感兴趣的:(Django学习)