]> git.ipfire.org Git - thirdparty/systemd.git/blame - docs/DAEMON_SOCKET_ACTIVATION.md
Merge pull request #31000 from flatcar-hub/krnowak/mutable-overlays
[thirdparty/systemd.git] / docs / DAEMON_SOCKET_ACTIVATION.md
CommitLineData
6b2a2776 1---
2title: Socket Activation with Popular Daemons
3category: Manuals and Documentation for Users and Administrators
4layout: default
5SPDX-License-Identifier: LGPL-2.1-or-later
6---
7
8## nginx
9
10nginx includes an undocumented, internal socket-passing mechanism based on the `NGINX` environmental variable. It uses this to perform reloads without having to close and reopen its sockets, but it's also useful for socket activation.
11
12**/etc/nginx/my-nginx.conf**
13
14```
15http {
16 server {
17 listen [::]:80 ipv6only=on;
18 listen 80;
19 }
20}
21```
22
23**/etc/systemd/system/my-nginx.service**
24
25```
26[Service]
27User=nginx
28Group=nginx
29Environment=NGINX=3:4;
30ExecStart=/usr/sbin/nginx -c/etc/nginx/my-nginx.conf
31PrivateNetwork=true
32```
33
34
35**/etc/systemd/system/my-nginx.socket**
36
37```
38[Socket]
39ListenStream=80
40ListenStream=0.0.0.0:80
41BindIPv6Only=ipv6-only
42After=network.target
43Requires=network.target
44
45[Install]
46WantedBy=sockets.target
47```
48
49
50## PHP-FPM
51
52Like nginx, PHP-FPM includes a socket-passing mechanism an environmental variable. In PHP-FPM's case, it's `FPM_SOCKETS`.
53
54This configuration is possible with any web server that supports FastCGI (like Apache, Lighttpd, or nginx). The web server does not need to know anything special about the socket; use a normal PHP-FPM configuration.
55
56Paths are based on a Fedora 19 system.
57
58### First, the configuration files
59
60**/etc/php-fpm.d/my-php-fpm-pool.conf**
61
62```
63[global]
64pid = /run/my-php-fpm-pool.pid ; Not really used by anything with daemonize = no, but needs to be writable.
65error_log = syslog ; Will aggregate to the service's systemd journal.
66daemonize = no ; systemd handles the forking.
67
68[www]
69listen = /var/run/my-php-fpm-pool.socket ; Must match systemd socket unit.
70user = nginx ; Ignored but required.
71group = nginx ; Ignored but required.
72pm = static
73pm.max_children = 10
74slowlog = syslog
75```
76
77
78**/etc/systemd/system/my-php-fpm-pool.service**
79
80```
81[Service]
82User=nginx
83Group=nginx
84Environment="FPM_SOCKETS=/var/run/my-php-fpm-pool.socket=3"
85ExecStart=/usr/sbin/php-fpm --fpm-config=/etc/php-fpm.d/my-php-fpm-pool.conf
86KillMode=process
87```
88
89
90**/etc/systemd/system/my-php-fpm-pool.socket**
91
92```
93[Socket]
94ListenStream=/var/run/my-php-fpm-pool.socket
95
96[Install]
97WantedBy=sockets.target
98```
99
100
101### Second, the setup commands
102
103```sh
104sudo systemctl --system daemon-reload
105sudo systemctl start my-php-fpm-pool.socket
106sudo systemctl enable my-php-fpm-pool.socket
107```
108
109
110After accessing the web server, the service should be running.
111
112```sh
113sudo systemctl status my-php-fpm-pool.service
114```
115
116
117It's possible to shut down the service and re-activate it using the web browser, too. It's necessary to stop and start the socket to reset some shutdown PHP-FPM does that otherwise breaks reactivation.
118
119```sh
120sudo systemctl stop my-php-fpm-pool.socket my-php-fpm-pool.service
121sudo systemctl start my-php-fpm-pool.socket
122```