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

Django Nginx+uwsgi 安装配置

2017-11-15 08:49 465 查看
[TOC]

我们使用 python manage.py runserver 来运行服务器。这只适用测试环境中使用。

正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttpd等,本文将以 Nginx 为例。

1 安装基础开发包

1.1 Centos 下安装步骤如下:

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

1.2 python包管理工具

pip 包: https://pypi.python.org/pypi/pip

安装 pip 的好处是可以用 pip list、pip uninstall 管理 Python 包, easy_install 没有这个功能,只有 uninstall。

2 安装 uwsgi

uwsgi:https://pypi.python.org/pypi/uWSGI

uwsgi 参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html

pip install uwsgi

uwsgi --version # 查看 uwsgi 版本

测试 uwsgi 是否正常:

新建 test.py 文件,内容如下:

def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"

然后在终端运行:

uwsgi --http :8001 --wsgi-file test.py

在浏览器内输入:http://xxx.xxx.xxx.xxx:8001,查看是否有"Hello World"输出,若没有输出,请检查你的安装过程。

3 uwsgi 配置

uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 首在/ect/uwsgi目录下uwsgi.ini,如果目录不存在自己创建

touch /etc/uwsgi/uwsgi.ini

添加如下配置:

[uwsgi]
chdir=/data/PyLearn
uid=nobody
gid=nobody
module=PyLearn.wsgi:application
socket=/etc/uwsgi/uwsgi.sock
master=true
workers=5
pidfile=/etc/uwsgi/uwsgi.pid
vacuum=true
thunder-lock=true
enable-threads=true
harakiri=30
post-buffering=4096
daemonize=/var/log/uwsgi.log

4 与Nginx结合

修改nginx的配置文件,由配置文件可以看出uwsgi和php-fpm使用很像

server{
server_name www.zhixin8.club zhixin8.club;
listen 80;
charset UTF-8;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

location / {
include uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass unix:/etc/uwsgi/uwsgi.sock;
}

}

设置完成后,在终端运行:

uwsgi --ini /etc/uwsgi/uwsgi.ini
service nginx start

在浏览器输入:http://127.0.0.1,你就可以看到 django 的 "It work" 了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: