-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaussette-circus.html
369 lines (319 loc) · 20.5 KB
/
chaussette-circus.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
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="index, follow" />
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:ital,wght@0,400;0,700;1,400&family=Source+Sans+Pro:ital,wght@0,300;0,400;0,700;1,400&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://dotmobo.xyz/theme/stylesheet/style.min.css">
<link id="dark-theme-style" rel="stylesheet" type="text/css"
media="(prefers-color-scheme: dark), (prefers-color-scheme: no-preference)"
href="https://dotmobo.xyz/theme/stylesheet/dark-theme.min.css">
<link id="pygments-dark-theme" rel="stylesheet" type="text/css"
media="(prefers-color-scheme: dark), (prefers-color-scheme: no-preference)"
href="https://dotmobo.xyz/theme/pygments/monokai.min.css">
<link id="pygments-light-theme" rel="stylesheet" type="text/css"
media="(prefers-color-scheme: light)"
href="https://dotmobo.xyz/theme/pygments/emacs.min.css">
<link rel="stylesheet" type="text/css" href="https://dotmobo.xyz/theme/font-awesome/css/fontawesome.css">
<link rel="stylesheet" type="text/css" href="https://dotmobo.xyz/theme/font-awesome/css/brands.css">
<link rel="stylesheet" type="text/css" href="https://dotmobo.xyz/theme/font-awesome/css/solid.css">
<link href="https://dotmobo.xyz/css/custom.css" rel="stylesheet">
<!-- Google Analytics -->
<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-68852219-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<!-- Chrome, Firefox OS and Opera -->
<meta name="theme-color" content="#333333">
<!-- Windows Phone -->
<meta name="msapplication-navbutton-color" content="#333333">
<!-- iOS Safari -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<!-- Microsoft EDGE -->
<meta name="msapplication-TileColor" content="#333333">
<meta name="author" content="Morgan" />
<meta name="description" content="Servir une application web python avec chaussette, circus et nginx" />
<meta name="keywords" content="python, circus, chaussette, nginx, django, bottle, flask, mozilla, waitress">
<meta property="og:site_name" content=".mobo"/>
<meta property="og:title" content="Servir une application web python avec chaussette, circus et nginx"/>
<meta property="og:description" content="Servir une application web python avec chaussette, circus et nginx"/>
<meta property="og:locale" content="fr_FR"/>
<meta property="og:url" content="https://dotmobo.xyz/chaussette-circus.html"/>
<meta property="og:type" content="article"/>
<meta property="article:published_time" content="2016-04-09 00:00:00+02:00"/>
<meta property="article:modified_time" content=""/>
<meta property="article:author" content="https://dotmobo.xyz/author/morgan.html">
<meta property="article:section" content="Python"/>
<meta property="article:tag" content="python"/>
<meta property="article:tag" content="circus"/>
<meta property="article:tag" content="chaussette"/>
<meta property="article:tag" content="nginx"/>
<meta property="article:tag" content="django"/>
<meta property="article:tag" content="bottle"/>
<meta property="article:tag" content="flask"/>
<meta property="article:tag" content="mozilla"/>
<meta property="article:tag" content="waitress"/>
<meta property="og:image" content="//dotmobo.xyz/images/avatar700.jpg">
<title>.mobo – Servir une application web python avec chaussette, circus et nginx</title>
</head>
<body >
<aside>
<div>
<a href="https://dotmobo.xyz">
<img src="//dotmobo.xyz/images/avatar700.jpg" alt=".mobo" title=".mobo">
</a>
<h1>
<a href="https://dotmobo.xyz">.mobo</a>
</h1>
<p>Explorations d'un développeur</p>
<ul class="social">
<li>
<a class="sc-github" href="https://github.com/dotmobo" target="_blank">
<i class="fab fa-github"></i>
</a>
</li>
<li>
<a class="sc-twitter" href="https://twitter.com/dotmobo" target="_blank">
<i class="fab fa-twitter"></i>
</a>
</li>
<li>
<a class="sc-rss" href="//dotmobo.xyz/feeds/all.atom.xml" target="_blank">
<i class="fas fa-rss"></i>
</a>
</li>
</ul>
</div>
</aside>
<main>
<nav>
<a href="https://dotmobo.xyz">Accueil</a>
<a href="/categories.html">Catégories</a>
<a href="/tags.html">Tags</a>
<a href="/archives.html">Archives</a>
</nav>
<article class="single">
<header>
<h1 id="chaussette-circus">Servir une application web python avec chaussette, circus et nginx</h1>
<p>
Posté le 09/04/2016 dans <a href="https://dotmobo.xyz/category/python.html">Python</a>
</p>
</header>
<div>
<img alt="Chaussette" class="align-right" src="./images/chaussette.png" />
<p>Voilà, tu as enfin développé ton application django/bottle/flask (raye les
mentions inutiles) et tu es fin prêt à la rendre disponible au monde entier.</p>
<p><em>Mais comment faire ? Quel serveur wsgi utiliser ? A l'aide !</em></p>
<p>Il existe différentes alternatives, mais je vais te proposer celle que j'affectionne
tout particulièrement, à savoir le trio chaussette/circus/nginx.</p>
<div class="section" id="creer-une-application-web">
<h2>1) Créer une application web</h2>
<p>Tu vas commencer par créer une application <a class="reference external" href="http://bottlepy.org/">bottle</a>
de base (pour changer de django, tiens !).</p>
<p>Tu crées et actives un environnement virtuel <strong>myapp</strong> en python 3.5 à l'aide de
<em>virtualenvwrapper</em>:</p>
<div class="highlight"><pre><span></span><span class="n">mkdir</span> <span class="n">myproject</span> <span class="o">&&</span> <span class="n">cd</span> <span class="n">myproject</span>
<span class="n">mkvirtualenv</span> <span class="o">-</span><span class="n">p</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="nb">bin</span><span class="o">/</span><span class="n">python3</span><span class="mf">.5</span> <span class="n">myapp</span>
</pre></div>
<p>Tu installes bottle dans ton virtualenv:</p>
<div class="highlight"><pre><span></span>pip<span class="w"> </span>install<span class="w"> </span>bottle
</pre></div>
<p>Dans le répertoire de ton projet, tu crées un module python <strong>myapp</strong> qui contient
un fichier vide <strong>__init__.py</strong> et un fichier <strong>wsgi.py</strong>:</p>
<div class="highlight"><pre><span></span><span class="n">mkdir</span> <span class="n">myapp</span> <span class="o">&&</span> <span class="n">touch</span> <span class="n">myapp</span><span class="o">/</span><span class="fm">__init__</span><span class="o">.</span><span class="n">py</span> <span class="n">myapp</span><span class="o">/</span><span class="n">wsgi</span><span class="o">.</span><span class="n">py</span>
</pre></div>
<p>Ton fichier <strong>wsgi.py</strong> doit ressembler à ça:</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">bottle</span> <span class="kn">import</span> <span class="n">route</span><span class="p">,</span> <span class="n">template</span><span class="p">,</span> <span class="n">default_app</span>
<span class="nd">@route</span><span class="p">(</span><span class="s1">'/hello/<name>'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="k">return</span> <span class="n">template</span><span class="p">(</span><span class="s1">'<b>Hello {{name}}</b>!'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>
<span class="n">application</span> <span class="o">=</span> <span class="n">default_app</span><span class="p">()</span>
</pre></div>
<p>Pour information, sous django, le fichier à utiliser s'appelle aussi <strong>wsgi.py</strong>.</p>
</div>
<div class="section" id="installer-chaussette-et-waitress">
<h2>2) Installer chaussette et waitress</h2>
<p>Maintenant, tu vas utiliser <a class="reference external" href="https://chaussette.readthedocs.org">chaussette</a>
pour lancer ton application web.</p>
<p>Chaussette est <a class="reference external" href="http://sametmax.com/quest-ce-que-wsgi-et-a-quoi-ca-sert/">un serveur wsgi</a>
qui permet le dialogue entre les requêtes http et ton application python.
Il autorise l'utilisation de différents backends wsgi, comme
<a class="reference external" href="http://waitress.readthedocs.org/">waitress</a> par exemple,
et a également la particularité de pouvoir servir ton application sur un socket déjà
ouvert. Dis comme ça, ça n'a l'air de rien, mais ça permet du coup d'utiliser un
manager de sockets comme circus ou supervisor.</p>
<p>Tu installes chaussette et waitress en étant <strong>dans</strong> le virtualenv <strong>myapp</strong>:</p>
<div class="highlight"><pre><span></span>pip<span class="w"> </span>install<span class="w"> </span>chaussette<span class="w"> </span>waitress
</pre></div>
<p>Et tu lances ton serveur wsgi depuis le répertoire de ton projet <strong>myproject</strong>:</p>
<div class="highlight"><pre><span></span>chaussette<span class="w"> </span>--backend<span class="w"> </span>waitress<span class="w"> </span>myapp.wsgi.application
</pre></div>
<p>Rends-toi sur <a class="reference external" href="http://127.0.0.1:8080/hello/you">http://127.0.0.1:8080/hello/you</a>,
tu devrais voir ta page apparaître. Si tout est ok, tu peux couper chaussette et
passer à la suite.</p>
</div>
<div class="section" id="configurer-circus">
<h2>3) Configurer circus</h2>
<p>Place au spectacle !</p>
<p><a class="reference external" href="http://circus.readthedocs.org/">Circus</a> est, grosso-modo, un manager de processus
et de sockets compatible python 2 et python 3. C'est lui qui va se charger de
monitorer et de redémarrer tes applications.
Il permet d'utiliser les virtualenvs et se marie très bien
avec chaussette. Ce projet nous vient à l'origine de la
<a class="reference external" href="https://www.mozilla.org/en-US/foundation/">fondation mozilla</a>.</p>
<p>Tu l'installes via pip <strong>en dehors</strong> du virtualenv <strong>myapp</strong> (donc en global sur ton système):</p>
<div class="highlight"><pre><span></span>deactivate
apt-get<span class="w"> </span>install<span class="w"> </span>libzmq-dev<span class="w"> </span>libevent-dev
pip<span class="w"> </span>install<span class="w"> </span>circus
</pre></div>
<p>Et tu crées le fichier de configuration de circus <strong>circus.ini</strong> dans ton <em>home</em>
par exemple. C'est là que tu vas pouvoir configurer tes <em>watchers</em>, qui vont
lancer tes processus chaussette.</p>
<p>Dans le fichier <strong>~/circus.ini</strong>, tu mets:</p>
<div class="highlight"><pre><span></span><span class="o">[</span>circus<span class="o">]</span>
<span class="nv">statsd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
<span class="nv">httpd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
<span class="o">[</span>watcher:myapp<span class="o">]</span>
<span class="nv">cmd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>/home/TONUSER/.virtualenvs/myapp/bin/chaussette<span class="w"> </span>--fd<span class="w"> </span><span class="k">$(</span>circus.sockets.web<span class="k">)</span><span class="w"> </span>--backend<span class="w"> </span>waitress<span class="w"> </span>myapp.wsgi.application
<span class="nv">working_dir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>/home/TONUSER/LECHEMINVERSTONPROJET/myproject
<span class="nv">numprocesses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
<span class="nv">copy_env</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
<span class="nv">use_sockets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
<span class="nv">virtualenv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>/home/TONUSER/.virtualenvs/myapp
<span class="nv">virtualenv_py_ver</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>.5
<span class="o">[</span>socket:web<span class="o">]</span>
<span class="nv">host</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">127</span>.0.0.1
<span class="nv">port</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8001</span>
</pre></div>
<p>N'oublie pas de modifier les différents chemins de <strong>working_dir</strong>, <strong>cmd</strong> et
<strong>virtualenv</strong> pour que ça correspondent à ta propre machine. Tu peux également
configurer plusieurs <em>watchers</em> si tu souhaites monitorer plusieurs applications
web.</p>
<p>Enfin, tu lances le <em>daemon</em> de circus:</p>
<div class="highlight"><pre><span></span>circusd<span class="w"> </span>--daemon<span class="w"> </span>~/circus.ini
</pre></div>
<p>Si tout s'est bien passé, tu devrais pouvoir utiliser la commande <strong>circusctl</strong>
pour voir le statut de tes applications, les redémarrer et autres.
Sinon, tu peux exécuter <strong>circusd</strong> sans l'option <strong>--daemon</strong> pour debugger.</p>
<p>Tu peux voir ci-dessous quelques exemples d'utilisation de <strong>circusctl</strong>:</p>
<div class="highlight"><pre><span></span>circusctl<span class="w"> </span>--help<span class="w"> </span><span class="c1"># voir l'ensemble des commandes disponibles</span>
circusctl<span class="w"> </span>status<span class="w"> </span><span class="c1"># voir le statut des applications</span>
circusctl<span class="w"> </span>listsockets<span class="w"> </span><span class="c1"># lister les sockets utilisés par les applications</span>
circusctl<span class="w"> </span>restart<span class="w"> </span>myapp<span class="w"> </span><span class="c1"># redémarrer myapp</span>
circusctl<span class="w"> </span>reload<span class="w"> </span>myapp<span class="w"> </span><span class="c1"># recharcher la configuration du watcher myapp</span>
</pre></div>
<p>Rends-toi sur <a class="reference external" href="http://127.0.0.1:8001/hello/you">http://127.0.0.1:8001/hello/you</a>
pour vérifier que tout fonctionne.</p>
<p>Grâce à circus, tu peux désormais manager plusieurs applications différentes,
qui tournent sous des environnements virtuels différents.</p>
<p>Pour information, il existe une interface web pour monitorer circus appelé
<strong>circus-web</strong>, mais qui n'est pas encore compatible python 3.</p>
</div>
<div class="section" id="parametrer-nginx">
<h2>3) Paramétrer nginx</h2>
<p>Bon, il ne te reste plus qu'à mettre en place nginx. C'est un
serveur http libre et performant qui est une très bonne alternative à apache.
Il va nous permettre de transmettre les requêtes http à circus/chaussette via les
sockets.</p>
<p>Tu l'installes via <em>apt-get</em> par exemple:</p>
<div class="highlight"><pre><span></span>apt-get<span class="w"> </span>install<span class="w"> </span>nginx
</pre></div>
<p>Et tu vas créer la configuration suivante dans <strong>/etc/nginx/sites-available/myapp.conf</strong>:</p>
<div class="highlight"><pre><span></span>upstream<span class="w"> </span>myapp<span class="w"> </span><span class="o">{</span>
<span class="w"> </span>server<span class="w"> </span><span class="m">127</span>.0.0.1:8001<span class="p">;</span>
<span class="o">}</span>
server<span class="w"> </span><span class="o">{</span>
<span class="w"> </span>listen<span class="w"> </span><span class="m">80</span><span class="p">;</span>
<span class="w"> </span>server_name<span class="w"> </span>localhost<span class="p">;</span>
<span class="w"> </span>location<span class="w"> </span>/<span class="w"> </span><span class="o">{</span>
<span class="w"> </span>proxy_pass<span class="w"> </span>http://myapp<span class="nv">$request_uri</span><span class="p">;</span>
<span class="w"> </span>proxy_redirect<span class="w"> </span>off<span class="p">;</span>
<span class="w"> </span>proxy_set_header<span class="w"> </span>Host<span class="w"> </span><span class="nv">$host</span><span class="p">;</span>
<span class="w"> </span>proxy_set_header<span class="w"> </span>X-Real-IP<span class="w"> </span><span class="nv">$remote_addr</span><span class="p">;</span>
<span class="w"> </span><span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>Tu actives ta conf', tu supprimes le site par défaut et tu redémarres nginx:</p>
<div class="highlight"><pre><span></span>ln<span class="w"> </span>-s<span class="w"> </span>/etc/nginx/sites-available/myapp.conf<span class="w"> </span>/etc/nginx/sites-enabled/myapp
rm<span class="w"> </span>/etc/nginx/sites-enabled/default
service<span class="w"> </span>nginx<span class="w"> </span>restart<span class="w"> </span><span class="c1">#ou via systemd selon ta distro</span>
</pre></div>
<p>Il ne te reste plus qu'à te rendre sur <a class="reference external" href="http://localhost/hello/you">http://localhost/hello/you</a> pour observer le résultat !</p>
</div>
</div>
<div class="tag-cloud">
<p>
<a href="https://dotmobo.xyz/tag/python.html">python</a>
<a href="https://dotmobo.xyz/tag/circus.html">circus</a>
<a href="https://dotmobo.xyz/tag/chaussette.html">chaussette</a>
<a href="https://dotmobo.xyz/tag/nginx.html">nginx</a>
<a href="https://dotmobo.xyz/tag/django.html">django</a>
<a href="https://dotmobo.xyz/tag/bottle.html">bottle</a>
<a href="https://dotmobo.xyz/tag/flask.html">flask</a>
<a href="https://dotmobo.xyz/tag/mozilla.html">mozilla</a>
<a href="https://dotmobo.xyz/tag/waitress.html">waitress</a>
</p>
</div>
<!-- Disqus -->
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'dotmobo';
(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>
Veuillez activer le JavaScript pour voir les commentaires.
</noscript>
<!-- End Disqus -->
</article>
<footer>
<p>
© 2024 - This work 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>
</p>
<p>
Construit avec <a href="http://getpelican.com" target="_blank">Pelican</a> utilisant le thème <a href="http://bit.ly/flex-pelican" target="_blank">Flex</a>
<span class="footer-separator">|</span>
Switch to the <a href="javascript:void(0)" onclick="theme.switch(`dark`)">dark</a> | <a href="javascript:void(0)" onclick="theme.switch(`light`)">light</a> | <a href="javascript:void(0)" onclick="theme.switch(`browser`)">browser</a> theme
<script id="dark-theme-script"
src="https://dotmobo.xyz/theme/dark-theme/dark-theme.min.js"
data-enable-auto-detect-theme="True"
data-default-theme="dark"
type="text/javascript">
</script>
</p><p>
<a rel="license"
href="http://creativecommons.org/licenses/by-sa/4.0/"
target="_blank">
<img alt="Creative Commons License"
title="Creative Commons License"
style="border-width:0"
src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png"
width="80"
height="15"/>
</a>
</p> </footer>
</main>
<script type="application/ld+json">
{
"@context" : "http://schema.org",
"@type" : "Blog",
"name": " .mobo ",
"url" : "https://dotmobo.xyz",
"image": "//dotmobo.xyz/images/avatar700.jpg",
"description": ".mobo - explorations d'un développeur"
}
</script>
</body>
</html>