NGINX+UWSGI部署生产的DJANGO代码

并且NGINX不用ROOT帐户哟。

1,编译安装NGINX及UWSGI及DJANGO,不表述

2,将NGINX文件夹更改为普通用户拥有。但执行文件NGINX仍为ROOT,运行如下命令加入特殊权限标志位,并将NGINX.CONF的USER设置普通用户及组(空格隔开):

1
chmod u+x nginx

  

3,在NGINX里配置UWSGI的PROXY

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
upstream p_host {
    server 127.0.0.1:9090;
}
 
server {
        listen       80;
        server_name  localhost;
         
        location / {           
            include  uwsgi_params;
            uwsgi_pass  prism_host;
            uwsgi_param UWSGI_SCRIPT P.wsgi;  //这里定义wsgi.py文件
            uwsgi_param UWSGI_CHDIR /P/P;    //这里定义DJANGO的MANAGER目录
            index  index.html index.htm;
            client_max_body_size 35m;
        }
    location ^~ /static {
                root /P/P//定义静态资源文件位置
        }
    }

  

4,配置UWSGI9090.INI文件(保证相关文件夹存在且有权限,为什么要多不同的端口呢,因为这样可以在一个服务器上配置多个端口,多个DJANGO应用)

1
2
3
4
5
6
7
8
9
10
11
12
13
[uwsgi]
socket = 127.0.0.1:9090
master = true
vhost = true
no-stie = true
workers = 4
reload-mercy = 10
vacuum = true   
max-requests = 1000
limit-as = 512
buffer-sizi = 30000
pidfile = /usr/local/nginx/run/uwsgi9090.pid  
daemonize = /uwsgi9090.log

  

5,生成执行脚本UWSGI9090文件(保证相关文件夹存在且有权限,这个本来可以放到SERVICE的INIT.D目录下,但为了不污染管理员用户,自己先用普通用户)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'
  
### BEGIN INIT INFO
# Provides:          uwsgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the uwsgi web server
# Description:       starts uwsgi using start-stop-daemon
### END INIT INFO
  
# Author:   licess
# website:  http://lnmp.org
  
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi9090
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/usr/local/nginx/conf/conf.d/$NAME.ini  //定义位置
PIDFILE=/usr/local/nginx/run/$NAME.pid  //定义位置
SCRIPTNAME=/usr/local/nginx/sbin/$NAME  //定义位置
  
set -e
[ -x "$DAEMON" ] || exit 0
  
do_start() {
    $DAEMON $CONFIGFILE || echo -n "uwsgi already running"
}
  
do_stop() {
    $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
    rm -f $PIDFILE
    echo "$DAEMON STOPED."
}
  
do_reload() {
    $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"
}
  
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
 
uwsgi9090

  

6,日常更新重启(可能需要建立软链接),停止操作。

1
2
3
4
5
6
7
8
9
10
11
/usr/local/nginx/sbin/nginx
 
/usr/local/nginx/sbin/nginx -s reload
 
/usr/local/nginx/sbin/nginx -s stop
 
sh /usr/local/nginx/sbin/uwsgi9090 start
 
sh /usr/local/nginx/sbin/uwsgi9090 stop
 
sh /usr/local/nginx/sbin/uwsgi9090 reload
上一篇:为什么说DevCloud是敏捷和DevOps落地神器?


下一篇:IT 行业变化趋势:向云端服务提供商转型