使用uwsgi+nginx部署django项目

时间:2021-10-23 20:50:05

环境搭建

关于nginx、python、django、pip、setuptools、uwsgi安装,查看文章
http://blog.csdn.net/win_turn/article/details/52613390

开始部署

图解

为了方便理解,画了一个nginx、uwsgi、django之间的相互联系图(看不清可以下载到本地,或鼠标右键“查看图片”)
使用uwsgi+nginx部署django项目

背景介绍

在目录/root/django/目录下,使用django-admin startproject demo创建项目
目录结构:

/root/django
└── demo
├── demo
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py

创建uwsgi配置文件

在manage.py的同级目录下,创建文件uwsgi.conf,内容如下:

<uwsgi>
<socket>demo.sock</socket>
<chmod-socket>664</chmod-socket>
<chdir>/root/django/demo</chdir>
<module>demo.wsgi</module>
<processes>5</processes>
<daemonize>uwsgi.log</daemonize>
</uwsgi>

备注:

<socket>demo.sock</socket> nginx和uwsgi之间用socket连接
<chmod-socket>664</chmod-socket>修改socket文件的权限
<chdir>/root/django/demo</chdir> manage.py所在目录
<module>demo.wsgi</module> 表示django的uwsgi文件,也就是/root/django/demo/demo/wsgi.py文件。这是用django-admin创建项目时生成的文件。
<processes>5</processes> 表示5个进程
<daemonize>uwsgi.log</daemonize> 表示uwsgi日志文件

创建uwsgi启动脚本

创建文件run,内容如下

#! /usr/bin/env bash

NAME="uwsgi.conf"
stop_uwsgi(){
if [ ! -n "$NAME" ];then
echo "no arguments"
exit;
fi

echo $NAME
ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $ID
echo "################################################"
for id in $ID
do
kill -9 $id
echo "kill $id"
done
echo "################################################"
}

if [ $# -ne 1 ]
then
echo "parameter err. must only 1."
exit
fi

if [ "$1" == "start" ]
then
uwsgi -x "$NAME"
exit
fi

if [ "$1" == "stop" ]
then
stop_uwsgi
exit
fi

if [ "$1" == "restart" ]
then
stop_uwsgi
uwsgi -x "$NAME"
exit
fi

备注:

脚本参数:
start:启动
stop:停止
restart:重新启动

配置nginx

修改uwsgi_params文件

vi /usr/local/nginx/conf/uwsgi_params

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;

uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;

uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

修改nginx配置文件

vi /usr/local/nginx/conf/nginx.conf
    server {

listen 80;
# server_name localhost;
# nginx访问日志
access_log /root/2017/logs/access.log;
# nginx错误日志
error_log /root/2017/logs/error.log;

# charset koi8-r;
location / {
include uwsgi_params;
# 用于和uwsgi通讯的socket
uwsgi_pass unix:///root/django/demo/demo.sock;
}

# 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;
}

# 静态文件配置。当访问静态文件时,不通过uwsgi和django
# location /static/ {
# alias /home/work/src/sites/testdjango1/testdjango/collectedstatic/;
# index index.html index.htm;
# }

# 静态文件配置
# location /media/ {
# alias /home/work/src/sites/testdjango1/testdjango/public/media/;
# }
}

启动

重启nginx

sudo /usr/local/nginx/sbin/nginx -s reload

修改/root/django/demo/run权限

chmod 755 run

启动uwsgi

./run start

文件结构

配置完的文件结构:

/root/django
├── access.log
├── demo
│ ├── demo
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── manage.py
│ ├── run
│ ├── uwsgi.conf
│ └── uwsgi.log
└── error.log

参考文档:

五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)
五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(下)
通过Nginx部署Django
基于nginx和uWSGI在Ubuntu上部署Django