uwsgi+nginx部署Django项目操作示例

yipeiwu_com6年前Python基础

本文实例讲述了uwsgi+nginx部署Django项目操作。分享给大家供大家参考,具体如下:

uWSGI概述

uWSGI 是一个全功能的 HTTP 服务器,可以把 HTTP 协议转化成语言支持的网络协议。

安装uwsgi

使用pip安装即可

pip install uwsgi

安装完成后可测试

#vim test.py
def application(env, start_response):
  start_response('200 OK', [('Content-Type','text/html')])
  return [b"Hello World"]

然后运行:

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

现在访问 ip:9090,如果出现Hello World说明安装成功

创建一个django项目

django-admin.py startproject hello

然后你的目录是这样的

hello /
├── hello
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

确保Django应用能正常启动

配置uwsgi.ini

#] vim /etc/uwsgi.ini #此文件可放在其它地方
[uwsgi]
chdir=/work/hello/ #项目目录
uid=root #可换成其它用户和组
gid=root
module=hello.wsgi:application
socket=/work/hello/uwsgi.sock
master=true
workers=5
pidfile=/work/hello/uwsgi.pid
vacuum=true
thunder-lock=true
enable-threads=true
harakiri=30
post-buffering=4096
daemonize=/work/hello/uwsgi.log

uwsgi相关命令

启动uwsgi:uwsgi --ini /path/uwsgi.ini
停止uwsgi:uwsgi --stop /path/uwsgi.pid
重新加载配置:uwsgi --reload /path/uwsgi.pid

nginx配置

server {
  listen 8080;
  location /static {
    alias /work/hello/static; #Django project's static files
  }
  location / {
    include uwsgi_params;
    uwsgi_connect_timeout 30;
    uwsgi_pass unix:/work/hello/uwsgi.sock;
  }
}

先启动uwsgi,启动nginx,一个简单的nginx + uwsgi + django就完成了

希望本文所述对大家基于Django框架的Python程序设计有所帮助。

相关文章

Python实现的金山快盘的签到程序

复制代码 代码如下:__author__ = 'clownfish'#coding:utf-8import urllib2,urllib,cookielib,json username...

python用模块zlib压缩与解压字符串和文件的方法

python中zlib模块是用来压缩或者解压缩数据,以便保存和传输。它是其他压缩工具的基础。下面来一起看看python用模块zlib压缩与解压字符串和文件的方法。话不多说,直接来看示例代...

Python使用面向对象方式创建线程实现12306售票系统

目前python 提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,而threading模块是对thread做了一...

Numpy与Pytorch 矩阵操作方式

Numpy 随机矩阵: np.random.randn(d0, d1, d2, ...) 矩阵大小与形状: np.ndarray.size 与 np.dnarray.shape Pyto...

python实现跨excel的工作表sheet之间的复制方法

python,将test1的Sheet1通过“跨文件”复制到test2的Sheet2里面。 包括谷歌没有能搜出这种问题答案。 我们贴出代码。 我们加载openpyxl这个包来解决:...