-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx-uwsgi-ubuntu-14.html
228 lines (186 loc) · 9.75 KB
/
nginx-uwsgi-ubuntu-14.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
<!DOCTYPE html>
<html lang="en">
<head>
<link href="/images/favicon.png" rel="icon">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>trvrm.github.io</title>
<link rel="stylesheet" type="text/css" href="/theme/css/flatly.min.css" />
<link rel="stylesheet" type="text/css" href="/theme/css/style.css" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
<link href="/theme/css/pygments/tango.css" rel="stylesheet">
<meta name="tags" content="Python" />
<meta name="tags" content="nginx" />
<meta name="tags" content="uwsgi" />
</head>
<body>
<section class="hero is-primary">
<!-- Hero header: will stick at the top -->
<div class="hero-head">
<nav class="navbar ">
<div class="navbar-menu is-active">
<div class="navbar-end">
<a class="navbar-item" href="https://twitter.com/trvrm">
<span class="icon"> <i class="fa fa-twitter"></i> </span>
twitter
</a>
<a class="navbar-item" href="https://github.com/trvrm">
<span class="icon"> <i class="fa fa-github"></i> </span>
github
</a>
</div>
</div>
</div>
</div>
<!-- Hero content: will be in the middle -->
<div class="hero-body">
<div class="container has-text-centered">
<p class="title is-1">trvrm.github.io</p>
</div>
</div>
<div class="hero-foot">
<nav class="navbar ">
<div class="navbar-brand is-active">
<a href="/" class="navbar-item" >
trvrm.github.io
</a>
</div>
<div class="navbar-menu is-active">
<div class="navbar-start">
<a class="navbar-item " href="/category/database.html">Database</a>
<a class="navbar-item " href="/category/software.html">Software</a>
<a class="navbar-item is-active " href="/category/systems.html">Systems</a>
</div>
</div>
</nav>
</div>
</section>
<section class="section">
<div class="container has-text-centered">
<p class="title is-3">
<a href="/nginx-uwsgi-ubuntu-14.html" rel="bookmark" title="Permalink to Nginx and UWSGI on Ubuntu 14">
Nginx and UWSGI on Ubuntu 14
</a>
</p>
<p class="subtitle is-5">
Thu 01 January 2015
</p>
</div>
<hr>
<div class="content ">
<p>The documentation for Nginx and UWSGI is long and complex, but with ubuntu 14
it's actually pretty straightforward to get them up and running.</p>
<p>I present here a a setup that uses nginx and uwsgi emperor to host
multiple python web applications simultaneously on an ubuntu 14 machine.</p>
<p>First, the packages</p>
<div class="highlight"><pre><span></span>$ sudo apt-get install nginx
$ sudo apt-get install uwsgi uwsgi-emperor uwsgi-plugin-python
</pre></div>
<p>Our configuration files will now be under <code class="code">
/etc/nginx</code>
and <code class="code">
/etc/uwsgi-emperor</code>
</p>
<p>You can start, stop, and reload nginx as follows:</p>
<div class="highlight"><pre><span></span>$ sudo service nginx start
$ sudo service nginx stop
$ sudo service nginx reload
</pre></div>
<p>The last command is useful when changing configuration settings.</p>
<p>Now set up a site by creating a file in <code class="code">
/etc/nginx/sites-available</code>
</p>
<div class="highlight"><pre><span></span><span class="c1">#/etc/nginx/sites-available/mysite</span>
server<span class="o">{</span>
server_name your_host_name<span class="p">;</span>
location /app1 <span class="o">{</span>
uwsgi_pass unix:/tmp/app1.socket<span class="p">;</span>
include uwsgi_params<span class="p">;</span>
<span class="o">}</span>
location /app2 <span class="o">{</span>
uwsgi_pass unix:/tmp/app2.socket<span class="p">;</span>
include uwsgi_params<span class="p">;</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>Then,</p>
<div class="highlight"><pre><span></span>$ sudo ln -s /etc/nginx/apps-available/mysite /etc/nginx/sites-enabled
</pre></div>
<div class="alert alert-warning compound">
<p class="compound-first"><strong>Warning</strong></p>
<p class="compound-last">A previous version of this tutorial had the sockets placed in <code class="code">
/run/uwsgi</code>
.
This was a mistake, because under Ubuntu <code class="code">
/run</code>
is mounted as a <code class="code">
tmpfs</code>
, and its content will be deleted on reboot
Your uwsgi sub-directory will vanish and the uwsgi services will not restart.</p>
</div>
<p>Next, set up your 'vassals' (<a class="reference external" href="http://uwsgi-docs.readthedocs.org/en/latest/Emperor.html">http://uwsgi-docs.readthedocs.org/en/latest/Emperor.html</a>)</p>
<p>Create <code class="code">
/etc/uwsgi-emperor/vassals/app1.ini</code>
as follows:</p>
<div class="highlight"><pre><span></span><span class="k">[uwsgi]</span>
<span class="na">plugin</span> <span class="o">=</span> <span class="s">python</span>
<span class="na">processes</span> <span class="o">=</span> <span class="s">2</span>
<span class="na">socket</span> <span class="o">=</span> <span class="s">/tmp/app1.socket</span>
<span class="na">chmod-socket</span> <span class="o">=</span> <span class="s">666</span>
<span class="na">chdir</span> <span class="o">=</span> <span class="s">/srv/app1</span>
<span class="na">wsgi-file</span> <span class="o">=</span> <span class="s">/srv/app1/main.py</span>
<span class="na">uid</span> <span class="o">=</span> <span class="s">www-data</span>
<span class="na">gid</span> <span class="o">=</span> <span class="s">www-data</span>
</pre></div>
<p>And for your second application, create <code class="code">
/etc/uwsgi-emperor/vassals/app2.ini</code>
as similarly:</p>
<div class="highlight"><pre><span></span><span class="k">[uwsgi]</span>
<span class="na">plugin</span> <span class="o">=</span> <span class="s">python</span>
<span class="na">processes</span> <span class="o">=</span> <span class="s">2</span>
<span class="na">socket</span> <span class="o">=</span> <span class="s">/tmp/app2.socket</span>
<span class="na">chmod-socket</span> <span class="o">=</span> <span class="s">666</span>
<span class="na">chdir</span> <span class="o">=</span> <span class="s">/srv/app1</span>
<span class="na">wsgi-file</span> <span class="o">=</span> <span class="s">/srv/app2/main.py</span>
<span class="na">uid</span> <span class="o">=</span> <span class="s">www-data</span>
<span class="na">gid</span> <span class="o">=</span> <span class="s">www-data</span>
</pre></div>
<p>The simple act of <em>creating</em> or touching a .ini file in <code class="code">
/etc/uwsgi-emperor/vassals</code>
will cause
the emperor process to try to restart your application.</p>
<p>Of course, your applications don't exist yet, so let's create them. The simplest wsgi
application can be only a few lines long:</p>
<p>Create <code class="code">
/srv/app1/main.py</code>
</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
<span class="n">start_response</span><span class="p">(</span><span class="s1">'200 OK'</span><span class="p">,</span> <span class="p">[(</span><span class="s1">'Content-Type'</span><span class="p">,</span><span class="s1">'text/html'</span><span class="p">)])</span>
<span class="k">return</span> <span class="p">[</span><span class="s2">"Hello World, I am app1"</span><span class="p">]</span>
</pre></div>
<p>And <code class="code">
/srv/app2/main.py</code>
</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
<span class="n">start_response</span><span class="p">(</span><span class="s1">'200 OK'</span><span class="p">,</span> <span class="p">[(</span><span class="s1">'Content-Type'</span><span class="p">,</span><span class="s1">'text/html'</span><span class="p">)])</span>
<span class="k">return</span> <span class="p">[</span><span class="s2">"I, however, am app2. "</span><span class="p">]</span>
</pre></div>
<p>And that's it!</p>
<p>Visiting <a class="reference external" href="http://your_host_name/app1">http://your_host_name/app1</a> or <a class="reference external" href="http://your_host_name/app2">http://your_host_name/app2</a> should return the text
you put in the python files.</p>
</div>
</section>
<footer class="footer">
<div class="container">
<div class="content has-text-centered">
<p>
Powered by <a href="http://getpelican.com/">Pelican</a>, <a href="http://python.org">Python</a>,
and <a href="http://bulma.io/">Bulma</a>
</p>
<p class="subtitle is-6">Ubi Caritas et Amor, Deus Ibi Est</p>
</div>
</div>
</footer>
</body>
</html>