uwsgi+django+nginx

简介:

    首先安装nginx和uwsgi,nginx用源码安装,uwsgi用pip install uwsgi安装,环境为centos6.8,python为2.7。开始前,确保项目在python manage.py runserver下能正常运行。

    uwsgi支持ini、xml等多种配置方式,本文简单介绍两种方式:ini和xml。

  1. ini方式

    

1
2
3
4
5
6
7
8
9
10
11
12
[root@work ~] # cat /etc/uwsgi9000.ini 
[uwsgi]
socket = 127.0.0.1:9000
master =  true
chdir =  /django_web/web
wsgi- file  = web /wsgi .py
processes = 4
daemonize =  /django_web/django_web .log
pidfile =  /tmp/uwsgi .pid
vacuum =  true
log-maxsize = 50000000
disable-logging =  true

创建一个ini配置文件,找到nginx的安装目录(如:/usr/local/nginx/),打开conf/nginx.conf文件,修改server配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
server {
         listen       80;
         server_name  192.168.137.224;
         access_log  /django_web/access_log ;
         error_log  /django_web/error_log ;
 
 
         location / {            
             include  uwsgi_params;  
             # uwsgi_pass 对应uwsgi配置中的socket
             uwsgi_pass  127.0.0.1:9000;  
         }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
 
         location  /static/  {
             alias   /django_web/web/collected_static/ ;
             index  index.html index.htm;
         }
 
         location  /media/  {
             alias  /django_web/web/media/ ;
         }
}

这里提一下:/static/目录需要修改django项目下的settings.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
 
STATIC_URL  =  '/static/'
 
# 当运行 python manage.py collectstatic 的时候
# STATIC_ROOT 文件夹 是用来将所有STATICFILES_DIRS中所有文件夹中的文件,以及各app中static中的文件都复制过来
# 把这些文件放到一起是为了用apache等部署的时候更方便
STATIC_ROOT  =  os.path.join(BASE_DIR,  'collected_static' )
 
# 其它 存放静态文件的文件夹,可以用来存放项目中公用的静态文件,里面不能包含 STATIC_ROOT
# 如果不想用 STATICFILES_DIRS 可以不用,都放在 app 里的 static 中也可以
STATICFILES_DIRS  =  (
     os.path.join(BASE_DIR,  "common_static" ),
     '/path/to/others/static/' ,   # 用不到的时候可以不写这一行
)
 
# 这个是默认设置,Django 默认会在 STATICFILES_DIRS中的文件夹 和 各app下的static文件夹中找文件
# 注意有先后顺序,找到了就不再继续找了
STATICFILES_FINDERS  =  (
     "django.contrib.staticfiles.finders.FileSystemFinder" ,
     "django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

目录类似为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
[root@work web] # tree
.
├── collected_static
│   └── admin
│       ├── css
│       │   ├── base.css
│       │   ├── changelists.css
│       │   ├── dashboard.css
│       │   ├── fonts.css
│       │   ├── forms.css
│       │   ├── login.css
│       │   ├── rtl.css
│       │   └── widgets.css
│       ├── fonts
│       │   ├── LICENSE.txt
│       │   ├── README.txt
│       │   ├── Roboto-Bold-webfont.woff
│       │   ├── Roboto-Light-webfont.woff
│       │   └── Roboto-Regular-webfont.woff
│       ├── img
│       │   ├── calendar-icons.svg
│       │   ├── gis
│       │   │   ├── move_vertex_off.svg
│       │   │   └── move_vertex_on.svg
│       │   ├── icon-addlink.svg
│       │   ├── icon-alert.svg
│       │   ├── icon-calendar.svg
│       │   ├── icon-changelink.svg
│       │   ├── icon-clock.svg
│       │   ├── icon-deletelink.svg
│       │   ├── icon-no.svg
│       │   ├── icon-unknown-alt.svg
│       │   ├── icon-unknown.svg
│       │   ├── icon- yes .svg
│       │   ├── inline-delete.svg
│       │   ├── LICENSE
│       │   ├── README.txt
│       │   ├── search.svg
│       │   ├── selector-icons.svg
│       │   ├── sorting-icons.svg
│       │   ├── tooltag-add.svg
│       │   └── tooltag-arrowright.svg
│       └── js
│           ├── actions.js
│           ├── actions.min.js
│           ├── admin
│           │   ├── DateTimeShortcuts.js
│           │   └── RelatedObjectLookups.js
│           ├── calendar.js
│           ├── cancel.js
│           ├── change_form.js
│           ├── collapse.js
│           ├── collapse.min.js
│           ├── core.js
│           ├── inlines.js
│           ├── inlines.min.js
│           ├── jquery.init.js
│           ├── popup_response.js
│           ├── prepopulate_init.js
│           ├── prepopulate.js
│           ├── prepopulate.min.js
│           ├── SelectBox.js
│           ├── SelectFilter2.js
│           ├── timeparse.js
│           ├── urlify.js
│           └── vendor
│               ├── jquery
│               │   ├── jquery.js
│               │   ├── jquery.min.js
│               │   └── LICENSE-JQUERY.txt
│               └── xregexp
│                   ├── LICENSE-XREGEXP.txt
│                   ├── xregexp.js
│                   └── xregexp.min.js
├── common_static
├── Demo
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── 0002_contact_tag.py
│   │   ├── 0002_contact_tag.pyc
│   │   ├── 0003_host.py
│   │   ├── 0003_host.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── static
│   ├── tests.py
│   └── views.py
├── django_wsgi.py
├── django_wsgi.pyc
├── manage.py
├── templates
│   ├── hello.html
│   ├── post.html
│   └── search_form.html
├──  test .py
├── web
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── search2.py
│   ├── search2.pyc
│   ├── search.py
│   ├── search.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── testdb_delete.py
│   ├── testdb_delete.pyc
│   ├── testdb.py
│   ├── testdb.pyc
│   ├── testdb_select.py
│   ├── testdb_select.pyc
│   ├── testdb_update.py
│   ├── testdb_update.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── view.py
│   ├── view.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── web_socket.xml

静态文件放在对应的 app 下的 static 文件夹中 或者 STATICFILES_DIRS 中的文件夹中。

当 DEBUG = True 时,Django 就能自动找到放在里面的静态文件。(Django 通过 STATICFILES_FINDERS 中的“查找器”,找到符合的就停下来,寻找的过程 类似于 Python 中使用 import xxx 时,找 xxx 这个包的过程)。

当 settings.py 中的 DEBUG = True 时,打开开发服务器 python manage.py runserver 直接访问 /static/zqxt.png 就可以找到这个静态文件。

也可以在 settings.py 中指定所有 app 共用的静态文件,比如 jquery.js 等

1
2
3
STATICFILES_DIRS  =  (
     os.path.join(BASE_DIR,  "common_static" ),
)

然后,运行

1
python manage.py collectstatic

会将所有静态文件收集到collected_static目录下。

运行 

1
2
uwsgi -i  /etc/uwsgi9000 .ini &
/usr/local/nginx/sbin/nginx

访问127.0.0.1就相当于访问django项目。

2.xml方式

nginx配置一样,不同ini的是要在django项目根目录下建一个.xml文件,如上文目录视图。

1
2
3
4
5
6
7
8
[root@work web] # cat web_socket.xml 
<uwsgi>
     <socket>:9000< /socket >
     <chdir> /django_web/web < /chdir >
     <module>django_wsgi< /module >
     <processes>4< /processes > <!-- 进程数 --> 
     <daemonize>uwsgi.log< /daemonize >
< /uwsgi >

然后运行

1
2
uwsgi -x  /django_web/web/web_socket .xml &
/usr/local/nginx/sbin/nginx

个人还是习惯用ini方式,简洁明了。




      本文转自YU文武貝 51CTO博客,原文链接:http://blog.51cto.com/linuxerxy/1876921,如需转载请自行联系原作者






相关文章
|
负载均衡 应用服务中间件 nginx
Django通过Nginx和uWSGI实现负载均衡
Django通过Nginx和uWSGI实现负载均衡
891 0
|
中间件 应用服务中间件 nginx
Nginx+uWSGI+Django原理
Nginx+uWSGI+Django原理
|
监控 应用服务中间件 nginx
nginx+uwsgi+flask
nginx+uwsgi+flask
|
应用服务中间件 Linux 测试技术
Nginx+uWSGI
【8月更文挑战第22天】
238 6
|
负载均衡 应用服务中间件 网络安全
Django后端架构开发:Nginx服务优化实践
Django后端架构开发:Nginx服务优化实践
444 2
|
网络协议 Unix 应用服务中间件
Nginx七层(应用层)反向代理:UWSGI代理uwsgi_pass篇
Nginx七层(应用层)反向代理:UWSGI代理uwsgi_pass篇
1583 1
|
监控 应用服务中间件 网络安全
部署Django应用:使用Gunicorn和Nginx构建高效的生产环境
部署Django应用:使用Gunicorn和Nginx构建高效的生产环境
1626 0
|
关系型数据库 应用服务中间件 数据库
如何安装和配置 Django 与 Postgres、Nginx 和 Gunicorn
如何安装和配置 Django 与 Postgres、Nginx 和 Gunicorn
370 0
|
前端开发 应用服务中间件 Shell
django(三)django配置支持nginx访问
我正在参加「掘金·启航计划」 VUE3.0版本发布,按照之前的计划,博客前端的页面打算使用VUE3.0重新编写。目前后端使用的是PHP的web框架thinkphp5.0,新版的博客后端打算使用python的web框架django。 目前我的服务器使用的环境是lnmp(centos+nginx+mariadb+php),因此运行django框架的时候,我选择使用nginx + uwsgi 我的服务器使用的是阿里云的centos7.8 运行原理如下图所示: 一:技术扩展WSGI WSGI 是 Web Server Gateway Interface 的缩写。以层的角度来看,WSGI 所在层的位置低
407 0
|
应用服务中间件 nginx Docker
将 react-typescript + django 部署到 nginx 容器(docker)
将 react-typescript + django 部署到 nginx 容器(docker)
417 0