]>
Commit | Line | Data |
---|---|---|
6b2a2776 | 1 | --- |
2 | title: Socket Activation with Popular Daemons | |
3 | category: Manuals and Documentation for Users and Administrators | |
4 | layout: default | |
5 | SPDX-License-Identifier: LGPL-2.1-or-later | |
6 | --- | |
7 | ||
8 | ## nginx | |
9 | ||
10 | nginx 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 | ``` | |
15 | http { | |
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] | |
27 | User=nginx | |
28 | Group=nginx | |
29 | Environment=NGINX=3:4; | |
30 | ExecStart=/usr/sbin/nginx -c/etc/nginx/my-nginx.conf | |
31 | PrivateNetwork=true | |
32 | ``` | |
33 | ||
34 | ||
35 | **/etc/systemd/system/my-nginx.socket** | |
36 | ||
37 | ``` | |
38 | [Socket] | |
39 | ListenStream=80 | |
40 | ListenStream=0.0.0.0:80 | |
41 | BindIPv6Only=ipv6-only | |
42 | After=network.target | |
43 | Requires=network.target | |
44 | ||
45 | [Install] | |
46 | WantedBy=sockets.target | |
47 | ``` | |
48 | ||
49 | ||
50 | ## PHP-FPM | |
51 | ||
52 | Like nginx, PHP-FPM includes a socket-passing mechanism an environmental variable. In PHP-FPM's case, it's `FPM_SOCKETS`. | |
53 | ||
54 | This 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 | ||
56 | Paths 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] | |
64 | pid = /run/my-php-fpm-pool.pid ; Not really used by anything with daemonize = no, but needs to be writable. | |
65 | error_log = syslog ; Will aggregate to the service's systemd journal. | |
66 | daemonize = no ; systemd handles the forking. | |
67 | ||
68 | [www] | |
69 | listen = /var/run/my-php-fpm-pool.socket ; Must match systemd socket unit. | |
70 | user = nginx ; Ignored but required. | |
71 | group = nginx ; Ignored but required. | |
72 | pm = static | |
73 | pm.max_children = 10 | |
74 | slowlog = syslog | |
75 | ``` | |
76 | ||
77 | ||
78 | **/etc/systemd/system/my-php-fpm-pool.service** | |
79 | ||
80 | ``` | |
81 | [Service] | |
82 | User=nginx | |
83 | Group=nginx | |
84 | Environment="FPM_SOCKETS=/var/run/my-php-fpm-pool.socket=3" | |
85 | ExecStart=/usr/sbin/php-fpm --fpm-config=/etc/php-fpm.d/my-php-fpm-pool.conf | |
86 | KillMode=process | |
87 | ``` | |
88 | ||
89 | ||
90 | **/etc/systemd/system/my-php-fpm-pool.socket** | |
91 | ||
92 | ``` | |
93 | [Socket] | |
94 | ListenStream=/var/run/my-php-fpm-pool.socket | |
95 | ||
96 | [Install] | |
97 | WantedBy=sockets.target | |
98 | ``` | |
99 | ||
100 | ||
101 | ### Second, the setup commands | |
102 | ||
103 | ```sh | |
104 | sudo systemctl --system daemon-reload | |
105 | sudo systemctl start my-php-fpm-pool.socket | |
106 | sudo systemctl enable my-php-fpm-pool.socket | |
107 | ``` | |
108 | ||
109 | ||
110 | After accessing the web server, the service should be running. | |
111 | ||
112 | ```sh | |
113 | sudo systemctl status my-php-fpm-pool.service | |
114 | ``` | |
115 | ||
116 | ||
117 | It'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 | |
120 | sudo systemctl stop my-php-fpm-pool.socket my-php-fpm-pool.service | |
121 | sudo systemctl start my-php-fpm-pool.socket | |
122 | ``` |