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

django+nginx+uwsgi+virtualenv部署站点示例

2017-12-07 20:05 961 查看
按照uwsgi官方文档部署nginx-uwsgi-django的过程中遇到了很多问题,官方文档可能存在错误或不全,比如:
nginx配置文件没有写http{}和event{}部分,是无法启动nginx服务的
nginx配置文件中也没有指定用户和MIME格式,这会导致网页不能正确加载静态文件(css/js);
官方给出的例中使用了virtualenv,如果是在真实环境中运行uwsgi,将出现ImportError, 需要先将虚拟环境中安装的python site-packages先添加到真实环境的python环境变量中去

参数:

CentOS主机地址192.168.0.105

环境拓扑:



整个web通信关系为:客户端与nginx之间通过http通信,nginx通过socket与uwsgi服务器通信,uwsgi服务器与Django通过wsgi协议通信。因此,整个部署过程就是完成这些部件之间的通信配置。

Step1: 确认Django项目已经可以自己正常运行:



python manage.py runserver 0.0.0.0:9000

确认可以正常访问网站

Step2:将虚拟环境中的python路径添加到真实环境的环境变量中(因为本例中Django是在virtualenv中安装的,如果不存在virtualenv则跳过这一步)

在project目录下创建wsgi.py文件,内容如下:(注意)

import os,sys

sys.path.append("/virtualenv/mezzanine/lib/python2.7/site-packages")

sys.path.append("/virtualenv/mezzanine/mezzsite")from django.core.wsgi import get_wsgi_application

from mezzanine.utils.conf import real_project_name

os.environ.setdefault("DJANGO_SETTINGS_MODULE","%s.settings" % real_project_name("mezzsite"))

application = get_wsgi_application()

Step3:使用uwsgi访问网站,确保uwsgi配置正确,可以正常工作:



首先在Linux真实环境(不是virtualenv)中安装好uwsgi

pip install uwsgi

在wsgi.py同级目录下,运行命令开启uwsgi服务器(也可以使用--wsgi-file来运行,点击查看更多参数):

[root@192 mezzsite]# uwsgi --http :9000 --module wsgi

*** Starting uWSGI 2.0.15 (64bit) on [Thu Dec 7 05:48:11 2017] ***

compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-16) on 06 December 2017 23:35:53

os: Linux-3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017

nodename: 192.168.0.105

machine: x86_64

clock source: unix

detected number of CPU cores: 2

current working directory: /virtualenv/mezzanine/mezzsite

detected binary path: /usr/bin/uwsgi

!!! no internal routing support, rebuild with pcre support !!!

uWSGI running as root, you can use --uid/--gid/--chroot options

*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 

*** WARNING: you are running uWSGI without its master process manager ***

your processes number limit is 7271

your memory page size is 4096 bytes

detected max file descriptor number: 1024

lock engine: pthread robust mutexes

thunder lock: disabled (you can enable it with --thunder-lock)

uWSGI http bound on :9000 fd 4

spawned uWSGI http 1 (pid: 21615)

uwsgi socket 0 bound to TCP address 127.0.0.1:45292 (port auto-assigned) fd 3

Python version: 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

*** Python threads support is disabled. You can enable it with --enable-threads ***

Python main interpreter initialized at 0x1361ff0

your server socket listen backlog is limited to 100 connections

your mercy for graceful operations on workers is 60 seconds

mapped 72760 bytes (71 KB) for 1 cores

*** Operational MODE: single process ***

WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1361ff0 pid: 21614 (default app)

*** uWSGI is running in multiple interpreter mode ***

spawned uWSGI worker 1 (and the only) (pid: 21614, cores: 1)

此时在浏览器中打开192.168.0.105:9000即可访问网站,但此时css和js等静态文件都无法加载,所以网站没有样式。注意: 在这一步中,我们用--http选项指定了可以直接通过9000端口对外提供http服务,稍后会将uwsgi与nginx进行关联,就不是用--http了,而是通过socket通信。

Step4: 配置nginx,使nginx与uwsgi用socket相关联,并对外提供http服务:



首先安装nginx: yum -y install nginx

安装完成后,修改nginx的配置文件如下:

user root;                    #nginx需要对Django项目及其所有父目录有执行权限

worker_processes 8;

http{

include /etc/nginx/mime.types;           #点击查看这两行的作用

default_type application/octet-stream;

server {

listen 8080;    #监听端口8080

server_name 192.168.0.105; #nginx服务器地址或主机名(在浏览器中访问192.168.0.105:8080)

charset utf-8;  #设置编码格式为utf-8

client_max_body_size 75M; # adjust to taste

#设置nginx的url与django项目中的文件对应关系:

location /media {    

alias /virtualenv/mezzanine/mezzsite/static/media; 

}

location /static {

alias /virtualenv/mezzanine/mezzsite/static; # Django静态文件目录在系统中的绝对路径

}

location / {                                                     #对除了上面两个(/media和/static)url外的所有请求,都由下面定义

uwsgi_pass 192.168.0.105:9000;       #交由uwsgi服务器进行处理,socket为192.168.0.105:9000,相应的,在uwsgi.ini文件中要指定与此相同的socket

include /etc/nginx/uwsgi_params;       # uwsgi_params是nginx和uwsgi之间的参数对应表,无需修改,在此处引用进去即可

}

}}

events {

worker_connections 1024; ## Default: 1024

}

配置好nginx后,先启动uwsgi服务器,两种方法:
可以在wsgi.py文件同级目录下运行命令:

uwsgi --socket 192.168.0.105:9000 --module wsgi
也可以通过init文件的方式启动uwsgi:

在任一目录下(本例中在与nginx.conf同级目录下)创建一个文件,命名为uwsgi.ini(只要后缀是ini即可),添加如下内容:

[uwsgi]

chdir = /virtualenv/mezzanine/mezzsite     #django工程的目录

wsgi-file = /virtualenv/mezzanine/mezzsite/wsgi.py  #wsgi.py文件的绝对路径

home = /virtualenv/mezzanine/   #如果有虚拟环境,则填虚拟环境的目录路径(可选)

master = true

processes = 2

socket =192.168.0.105:9000   #指定一个用于与nginx通信的socket

vacuum = true     #退出时清理环境

buffer-size = 65536  #最大缓冲区,如果设置得太小,请求的数据超过buffer-size的话,网站会起不来

保存后在uwsgi.ini同级目录下,运行命令启动uwsgi:

uwsgi --ini uwsgi.ini

uwsgi服务器启动后,启动nginx服务:

service nginx start

Step5: 通过nginx访问站点:

此时,可以通过在浏览器中输入192.168.0.105:8080成功的访问站点了,且样式都可以正确加载了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: