-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
django-deploying-with-fastcgi-nginx.html
373 lines (310 loc) · 18.6 KB
/
django-deploying-with-fastcgi-nginx.html
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE html>
<html lang="zh-hans" prefix="og: http://ogp.me/ns# fb: https://www.facebook.com/2008/fbml" class="han-init">
<head>
<title>[django]使用 nginx + fastcgi 部署 django - Mozillazg's Blog</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://mozillazg.com/favicon.ico" rel="icon">
<link rel="canonical" href="https://mozillazg.com/2013/01/django-deploying-with-fastcgi-nginx.html">
<meta name="author" content="mozillazg" />
<meta name="keywords" content="python,django,nginx,fastcgi" />
<meta name="description" content="本文测试环境:linux mint,python 2.7,django 1.4 安装依赖 安装 flup: $ sudo pip install flup 安装 nginx $ sudo apt-get install nginx 设置 django 本例中项目名为 bbs: $ pwd /var/www/bbs $ tree . -d . |-- bb # 应用 |-- bbs # settings.py wsgi.py |-- static # 静态文件 | `-- css `-- templates # TEMPLATE_DIRS 配置 settings: TEMPLATE_DIRS = ( '/var/www/bbs ..." />
<meta property="og:site_name" content="Mozillazg's Blog" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="[django]使用 nginx + fastcgi 部署 django"/>
<meta property="og:url" content="https://mozillazg.com/2013/01/django-deploying-with-fastcgi-nginx.html"/>
<meta property="og:description" content="本文测试环境:linux mint,python 2.7,django 1.4 安装依赖 安装 flup: $ sudo pip install flup 安装 nginx $ sudo apt-get install nginx 设置 django 本例中项目名为 bbs: $ pwd /var/www/bbs $ tree . -d . |-- bb # 应用 |-- bbs # settings.py wsgi.py |-- static # 静态文件 | `-- css `-- templates # TEMPLATE_DIRS 配置 settings: TEMPLATE_DIRS = ( '/var/www/bbs ..."/>
<meta property="article:published_time" content="2013-01-31" />
<meta property="article:section" content="django" />
<meta property="article:tag" content="python" />
<meta property="article:tag" content="django" />
<meta property="article:tag" content="nginx" />
<meta property="article:tag" content="fastcgi" />
<meta property="article:author" content="mozillazg" />
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@mozillazg">
<meta name="twitter:creator" content="@mozillazg">
<meta name="twitter:domain" content="https://mozillazg.com">
<!-- Bootstrap -->
<link rel="stylesheet" href="https://mozillazg.com/theme/css/bootstrap.min.css" type="text/css"/>
<link href="https://mozillazg.com/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="https://mozillazg.com/theme/css/pygments/github.css" rel="stylesheet">
<link rel="stylesheet" href="https://mozillazg.com/theme/css/style.css" type="text/css"/>
<link href="https://mozillazg.com/static/custom.css" rel="stylesheet">
<link href="https://mozillazg.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate"
title="Mozillazg's Blog ATOM Feed"/>
<link href="https://mozillazg.com/feeds/all.rss.xml" type="application/rss+xml" rel="alternate"
title="Mozillazg's Blog RSS Feed"/>
<link href="https://mozillazg.com/feeds/django.atom.xml" type="application/atom+xml" rel="alternate"
title="Mozillazg's Blog django ATOM Feed"/>
</head>
<body>
<div class="navbar" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://mozillazg.com/" class="navbar-brand">
Mozillazg's Blog </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li><a href="/index.html">Home</a></li>
<li><a href="https://mozillazg.com/feeds/all.atom.xml">Feed</a></li>
<li><a href="https://mozillazg.com/2014/10/pages/about-me.html">
About me
</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="https://mozillazg.com/archives.html"><i class="fa fa-th-list"></i><span class="icon-label">Archives</span></a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<div class="container">
<div class="row">
<div class="col-lg-12">
<section id="content" class="yue">
<article>
<header class="text-center">
<h1>
<a href="https://mozillazg.com/2013/01/django-deploying-with-fastcgi-nginx.html"
rel="bookmark"
title="Permalink to [django]使用 nginx + fastcgi 部署 django">
[django]使用 nginx + fastcgi 部署 django
</a>
</h1>
<p class="published">
<time datetime="2013-01-31T00:00:00+00:00">
2013-01-31
</time>
</p>
</header>
<div class="entry-content">
<div class="well-sm article-info">
<footer class="post-info">
<a href="https://mozillazg.com/category/django.html">django</a>
<span class="label label-default hide">Tags</span>
<a href="https://mozillazg.com/tag/python.html">python</a>
/
<a href="https://mozillazg.com/tag/django.html">django</a>
/
<a href="https://mozillazg.com/tag/nginx.html">nginx</a>
/
<a href="https://mozillazg.com/tag/fastcgi.html">fastcgi</a>
</footer><!-- /.post-info --> </div>
<div>
<p>本文测试环境:linux mint,python 2.7,django 1.4</p>
<h2 id="_1">安装依赖</h2>
<p>安装 flup:</p>
<div class="codehilite"><pre><span class="gp">$</span> sudo pip install flup
</pre></div>
<p>安装 nginx</p>
<div class="codehilite"><pre><span class="gp">$</span> sudo apt-get install nginx
</pre></div>
<h2 id="django">设置 django</h2>
<p>本例中项目名为 bbs:</p>
<div class="codehilite"><pre><span class="gp">$</span> <span class="nb">pwd</span>
<span class="go">/var/www/bbs</span>
<span class="gp">$</span> tree . -d
<span class="go">.</span>
<span class="go">|-- bb # 应用</span>
<span class="go">|-- bbs # settings.py wsgi.py</span>
<span class="go">|-- static # 静态文件</span>
<span class="go">| `-- css</span>
<span class="go">`-- templates # TEMPLATE_DIRS</span>
</pre></div>
<p>配置 settings:</p>
<div class="codehilite"><pre><span class="n">TEMPLATE_DIRS</span> <span class="o">=</span> <span class="p">(</span>
<span class="s">'/var/www/bbs/templates'</span><span class="p">,</span> <span class="c"># 这里要是绝对路径</span>
<span class="p">)</span>
<span class="n">STATIC_ROOT</span> <span class="o">=</span> <span class="s">'/var/www/bbs/static'</span>
</pre></div>
<p>配置 wsgi.py:</p>
<div class="codehilite"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'/var/www/bbs/'</span><span class="p">)</span> <span class="c"># 项目目录的绝对路径</span>
<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">"DJANGO_SETTINGS_MODULE"</span><span class="p">,</span> <span class="s">"bbs.settings"</span><span class="p">)</span>
</pre></div>
<p>本例中使用 /var/www/bbs/static/ 保存静态文件,使用命令
<code>python manage.py collectstatic</code> 收集静态文件。</p>
<p>最终 /var/www/bbs/static/ 目录结构应该类似(admin 目录及其目录下文件一定要有):</p>
<div class="codehilite"><pre><span class="gp">$</span> tree static -d
<span class="go">static</span>
<span class="go">|-- admin</span>
<span class="go">| |-- css</span>
<span class="go">| |-- img</span>
<span class="go">| | `-- gis</span>
<span class="go">| `-- js</span>
<span class="go">| `-- admin</span>
<span class="go">`-- css</span>
</pre></div>
<h2 id="nginx">配置 nginx</h2>
<div class="codehilite"><pre><span class="gp">$</span> vim /etc/nginx/conf.d/bbs.conf
<span class="go">server {</span>
<span class="go"> listen 80;</span>
<span class="go"> server_name bbs.com;</span>
<span class="go"> access_log /var/log/nginx/bbs.access.log;</span>
<span class="go"> error_log /var/log/nginx/bbs.error.log;</span>
<span class="go"> location / {</span>
<span class="go"> # host and port to fastcgi server</span>
<span class="go"> fastcgi_pass 127.0.0.1:8081; # host:port</span>
<span class="go"> fastcgi_param PATH_INFO $fastcgi_script_name;</span>
<span class="go"> fastcgi_param REQUEST_METHOD $request_method;</span>
<span class="go"> fastcgi_param QUERY_STRING $query_string;</span>
<span class="go"> fastcgi_param CONTENT_TYPE $content_type;</span>
<span class="go"> fastcgi_param CONTENT_LENGTH $content_length;</span>
<span class="go"> fastcgi_pass_header Authorization;</span>
<span class="go"> fastcgi_intercept_errors off;</span>
<span class="go"> fastcgi_param SERVER_PROTOCOL $server_protocol;</span>
<span class="go"> fastcgi_param SERVER_PORT $server_port;</span>
<span class="go"> fastcgi_param SERVER_NAME $server_name;</span>
<span class="go"> }</span>
<span class="go"> location /static/ { # STATIC_URL</span>
<span class="go"> alias /var/www/bbs/static/; # STATIC_ROOT </span>
<span class="go"> }</span>
<span class="go"> location /meida/ { # MEDIA_URL</span>
<span class="go"> alias /var/www/bbs/media/; # MEDIA_ROOT </span>
<span class="go"> }</span>
<span class="go"> # 如果 admin 文件夹在 static 目录下,可以不配置下面项</span>
<span class="go"> location /static/admin/ { # admin static file</span>
<span class="go"> alias /var/www/bbs/static/admin/;</span>
<span class="go"> }</span>
<span class="go">}</span>
</pre></div>
<p>以 fastcgi 的形式运行 django 项目(注意:这里的 host 及 port 要与上面配置的一样):</p>
<div class="codehilite"><pre><span class="gp">$</span> python manage.py runfcgi <span class="nv">host</span><span class="o">=</span>127.0.0.1 <span class="nv">port</span><span class="o">=</span>8081
</pre></div>
<p>终止 fastcgi 话,查看相关进程然后结束掉即可:</p>
<div class="codehilite"><pre><span class="gp">$</span> ps aux | grep cgi
<span class="go">mzg 2500 0.0 5.0 34860 12480 ? S 15:43 0:00 python manage.py runfcgi host=127.0.0.1 port=8081</span>
<span class="go">mzg 2501 0.0 4.7 34860 11784 ? S 15:43 0:00 python manage.py runfcgi host=127.0.0.1 port=8081</span>
<span class="go">mzg 2502 0.0 4.7 34860 11788 ? S 15:43 0:00 python manage.py runfcgi host=127.0.0.1 port=8081</span>
<span class="gp">$</span> sudo <span class="nb">kill </span>2500
<span class="gp">$</span> ps aux | grep cgi
<span class="go">mzg 2745 0.0 0.3 5128 876 pts/1 S+ 16:05 0:00 grep --colour=auto cgi</span>
</pre></div>
<p>测试环境的话,由于域名是虚构的,所以要配置 /etc/hosts 添加 <code>127.0.0.1 bbs.com</code>:</p>
<div class="codehilite"><pre><span class="gp">$</span> sudo vim /etc/hosts
<span class="gp">$</span> tail /etc/hosts
<span class="go">127.0.0.1 bbs.com</span>
</pre></div>
<p>更改目录权限:</p>
<div class="codehilite"><pre><span class="gp">$</span> sudo chown www-data:www-data /var/www/bbs -R
</pre></div>
<p>启动 nginx 服务</p>
<div class="codehilite"><pre><span class="gp">$</span> service nginx restart
</pre></div>
<p>结果:</p>
<p><img alt="前台" src="/static/images/2013-1-django-deploying-nginx-01.png" /></p>
<p><img alt="后台" src="/static/images/2013-1-django-deploying-nginx-02.png" /></p>
<p>后续 linux 相关的工作暂且不表:</p>
<ul>
<li><del>配置项目目录访问权限</del></li>
<li>配置 iptables ,开放 80 端口</li>
</ul>
<p>测试项目及配置文件可以从 <a href="https://github.com/mozillazg/django-simple-projects/tree/master/projects/bbs">这个代码仓库</a> 中获取。</p>
<h2 id="_2">参考</h2>
<ul>
<li><a href="https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/">How to use Django with FastCGI, SCGI, or AJP | Django documentation</a></li>
<li><a href="http://www.dmclaughlin.com/2008/11/03/complete-guide-to-deploying-django-on-ubuntu-with-nginx-fastcgi-and-mysql/">David McLaughlin » Complete guide to deploying django on Ubuntu with nginx, FastCGI and MySQL</a></li>
</ul>
</div>
</div>
<!-- /.entry-content -->
<section class="well" id="related-posts">
<p>Related Posts:</p>
<ul>
<li><a href="https://mozillazg.com/2013/06/django-south-rename-model-or-table.html">[django]使用 South 重命名 model 名称</a></li>
<li><a href="https://mozillazg.com/2013/02/django-typeerror-decod-argument-1-must-be-string-not-none.html">[django] 创建后台超级用户时出现 TypeError: decode() argument 1 must be string, not None 错误</a></li>
<li><a href="https://mozillazg.com/2013/10/django-TypeError-delete-got-an-unexpected-keyword-argument-using.html">[django]修复 "TypeError: delete() got an unexpected keyword argument 'using'"</a></li>
<li><a href="https://mozillazg.com/2013/04/django-custom-model-display-name-on-admin-pages.html">[django]自定义 model 在后台显示的名称</a></li>
<li><a href="https://mozillazg.com/2014/03/django-save-uploaded-file-with-utf8-encoding-by-custom-model-field.html">[django]通过自定义 Model Field 的方式将上传的文本文件按 UTF-8 编码保存</a></li>
</ul>
</section>
<hr/>
<section class="comments" id="comments">
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'my-github-blog'; // required: replace example with your forum shortname
var disqus_identifier = 'django-deploying-with-fastcgi-nginx';
var disqus_url = 'https://mozillazg.com/2013/01/django-deploying-with-fastcgi-nginx.html';
var disqus_config = function () {
this.language = "zh-hans";
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</article>
</section>
</div>
</div>
</div>
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2016 mozillazg
· Powered by <a href="https://github.com/DandyDev/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>,
<a href="http://docs.getpelican.com/" target="_blank">Pelican</a>,
<a href="http://getbootstrap.com" target="_blank">Bootstrap</a> <p><small> <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="/static/images/by-sa-80x15.png" /></a>
"<span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">Mozillazg's Blog</span>" by <a xmlns:cc="http://creativecommons.org/ns#" href="https://mozillazg.com" property="cc:attributionName" rel="cc:attributionURL">mozillazg</a> is
licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>, except where indicated otherwise.
</small></p>
</div>
<div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div>
</div>
</div>
</footer>
<script src="https://mozillazg.com/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://mozillazg.com/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="https://mozillazg.com/theme/js/respond.min.js"></script>
<!-- Disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'my-github-blog'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script');
s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- End Disqus Code -->
<!-- Google Analytics Universal -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-77172981-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics Universal Code -->
</body>
</html>