@@ -212,12 +212,15 @@ Using Gunicorn with upstart is simple. In this example we will run the app
212
212
Systemd
213
213
-------
214
214
215
- A tool that is starting to be common on linux systems is Systemd _. Below are
216
- configurations files and instructions for using systemd to create a unix socket
217
- for incoming Gunicorn requests. Systemd will listen on this socket and start
218
- gunicorn automatically in response to traffic. Later in this section are
219
- instructions for configuring Nginx to forward web traffic to the newly created
220
- unix socket:
215
+ A tool that is starting to be common on linux systems is Systemd _. It is a
216
+ system services manager that allows for strict process management, resources
217
+ and permissions control.
218
+
219
+ Below are configurations files and instructions for using systemd to create
220
+ a unix socket for incoming Gunicorn requests. Systemd will listen on this
221
+ socket and start gunicorn automatically in response to traffic. Later in
222
+ this section are instructions for configuring Nginx to forward web traffic
223
+ to the newly created unix socket:
221
224
222
225
**/etc/systemd/system/gunicorn.service **::
223
226
@@ -228,14 +231,18 @@ unix socket:
228
231
229
232
[Service]
230
233
Type=notify
234
+ # the specific user that our service will run as
231
235
User=someuser
232
236
Group=someuser
237
+ # another option for an even more restricted service is
238
+ # DynamicUser=yes
239
+ # see http://0pointer.net/blog/dynamic-users-with-systemd.html
233
240
RuntimeDirectory=gunicorn
234
241
WorkingDirectory=/home/someuser/applicationroot
235
- ExecStart=/usr/bin/gunicorn --pid /run/gunicorn/pid \
236
- --bind unix:/run/gunicorn.sock applicationname.wsgi
242
+ ExecStart=/usr/bin/gunicorn applicationname.wsgi
237
243
ExecReload=/bin/kill -s HUP $MAINPID
238
- ExecStop=/bin/kill -s TERM $MAINPID
244
+ KillMode=mixed
245
+ TimeoutStopSec=5
239
246
PrivateTmp=true
240
247
241
248
[Install]
@@ -248,33 +255,47 @@ unix socket:
248
255
249
256
[Socket]
250
257
ListenStream=/run/gunicorn.sock
251
- User=someuser
252
- Group=someuser
253
-
258
+ # Our service won't need permissions for the socket, since it
259
+ # inherits the file descriptor by socket activation
260
+ # only the nginx daemon will need access to the socket
261
+ User=www-data
262
+ # Optionally restrict the socket permissions even more.
263
+ # Mode=600
264
+
254
265
[Install]
255
266
WantedBy=sockets.target
256
267
257
- **/etc/tmpfiles.d/gunicorn.conf **::
258
268
259
- d /run/gunicorn 0755 someuser somegroup -
269
+ Next enable and start the socket (it will autostart at boot too)::
270
+
271
+ systemctl enable --now gunicorn.socket
260
272
261
- Next enable the socket so it autostarts at boot::
262
273
263
- systemctl enable gunicorn.socket
274
+ Now let's see if the nginx daemon will be able to connect to the socket.
275
+ Running ``sudo -u www-data curl --unix-socket /run/gunicorn.sock http ``,
276
+ our Gunicorn service will be automatically started and you should see some
277
+ HTML from your server in the terminal.
264
278
265
- Either reboot, or start the services manually ::
279
+ .. note ::
266
280
267
- systemctl start gunicorn.socket
281
+ systemd employs cgroups to track the processes of a service, so it doesn't
282
+ need pid files. In the rare case that you need to find out the service main
283
+ pid, you can use ``systemctl show --value -p MainPID gunicorn.service ``, but
284
+ if you only want to send a signal an even better option is
285
+ ``systemctl kill -s HUP gunicorn.service ``.
268
286
287
+ .. note ::
269
288
270
- After running ``curl --unix-socket /run/gunicorn.sock http ``, Gunicorn
271
- should start and you should see some HTML from your server in the terminal.
289
+ ``www-data `` is the default nginx user in debian, other distriburions use
290
+ different users (for example: ``http `` or ``nginx ``). Check you distro to
291
+ know what to put for the socket user, and for the sudo command.
272
292
273
293
You must now configure your web proxy to send traffic to the new Gunicorn
274
294
socket. Edit your ``nginx.conf `` to include the following:
275
295
276
296
**/etc/nginx/nginx.conf **::
277
297
298
+ user www-data;
278
299
...
279
300
http {
280
301
server {
@@ -292,15 +313,15 @@ socket. Edit your ``nginx.conf`` to include the following:
292
313
The listen and server_name used here are configured for a local machine.
293
314
In a production server you will most likely listen on port 80,
294
315
and use your URL as the server_name.
295
-
316
+
296
317
Now make sure you enable the nginx service so it automatically starts at boot::
297
318
298
319
systemctl enable nginx.service
299
-
320
+
300
321
Either reboot, or start Nginx with the following command::
301
322
302
323
systemctl start nginx
303
-
324
+
304
325
Now you should be able to test Nginx with Gunicorn by visiting
305
326
http://127.0.0.1:8000/ in any web browser. Systemd is now set up.
306
327
0 commit comments