您的位置:首页 > 运维架构 > Nginx

nginx + uwsgi 部署python django web服务

2017-12-20 14:35 656 查看
1、nginx 配置

在nginx.conf中添加下面配置

server
{
listen       80;
server_name  webchat.com;

location / {
root /var/www/webchat;					#django项目路径
include  uwsgi_params;
uwsgi_pass  127.0.0.1:9090;				#必须和uwsgi中的设置一致
uwsgi_param UWSGI_SCRIPT webchat.wsgi;	#入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
uwsgi_param UWSGI_CHDIR /var/www/webchat;	#项目根目录
index  index.html index.htm;
client_max_body_size 35m;
}
}

server {
listen       80;
server_name webchat.com;
#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
include        uwsgi_params;
uwsgi_pass     127.0.0.1:8000;
uwsgi_read_timeout 300;
}

location /static/ {
alias  /var/www/webchat/static/;
index  index.html index.htm;
}

location /media/ {
alias  /var/www/webchat/media/;
}
#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}


2、uwsgi配置

在项目/var/www/webchat下面创建uwsgi9090.ini,内容如下:

[uwsgi]
socket=:9090
chdir=/var/www/webchat
module=webchat.wsgi
master=true
processes=4
threads=2
py-autoreload=1
daemonize=/var/log/uwsgi9090.log


3、编写uwsgi启动、停止脚本uwsgi9090.sh,内容如下:

#!/bin/bash
### BEGIN INIT INFO
# Provides:             uwsgi9090
# Required-Start:       $network $remote_fs $syslog
# Required-Stop:        $network $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    uwsgi
# Description:          uwsgi is a high-performance web and proxy server
### END INIT INFO
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 /var/www/webchat/uwsgi9090.ini
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 /var/www/webchat/uwsgi9090.ini
echo "Restart uwsgi service [OK]"
else


4、启动及停止

/bin/bash  uwsgi9090.sh start //启动

/bin/bash  uwsgi9090.sh stop //停止

/bin/bash  uwsgi9090.sh restart //重启
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx uwsgi django