Django + uWSGI + Nginx

ps:

#项目目录
/opt/mtrop/

#项目静态文件地址

/opt/mtrop/statics

一,安装uwsgi

pip install uwsgi

二,安装nginx

groupadd www && useradd www -g www

yum -y install pcre-devel openssl openssl-devel wget gcc-c++ wget net-tools

wget http://nginx.org/download/nginx-1.14.0.tar.gz

mkdir /usr/local/nginx
tar -zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0
./configure --prefix=/usr/local/nginx/ --user=www --group=www
make && make install

echo 'PATH="/usr/local/nginx/sbin:$PATH"'>> /etc/profile;source /etc/profile

mkdir /usr/local/nginx/conf/vhost

nginx 启动文件(/etc/init.d/nginx)

#! /bin/sh
# chkconfig: 2345 55 25
# Author:   xiaoer
# website:  https://www.cnblogs.com/xiao2er/

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid

case "$1" in
    start)
        echo -n "Starting $NAME... "

        if netstat -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if netstat -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped"
            exit 0
        fi
        ;;

    force-quit)
        echo -n "Terminating $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if netstat -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
        ;;

esac

添加配置文件(/usr/local/nginx/conf/vhost/mtrops.conf)

server {
    listen 8080;
    server_name 127.0.0.1 192.168.1.126;

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:9000;
    }
    location /static{
        alias /opt/mtrop/statics;
    }
}

三,配置uwsgi(/opt/mtrop/uwsgi.ini)

[uwsgi]
# 通过uwsgi访问django需要配置成http
# 通过nginx请求uwsgi来访问django 需要配置成socket
# 9000 是django的端口号
socket = :9000

# web项目根目录
chdir = /opt/mtrop

# module指定项目自带的的wsgi配置文件位置
module = mtrops_v2.wsgi

# 允许存在主进程
master = true

# 开启进程数量
processes = 3

# 服务器退出时自动清理环境
vacuum = true

四,启动

nohup /opt/venv/bin/uwsgi --ini uwsgi.ini &
service nginx start
原文地址:https://www.cnblogs.com/xiao2er/p/10314443.html