summaryrefslogtreecommitdiffstats
path: root/docs/DAEMON_SOCKET_ACTIVATION.md
blob: 107615e2ea8e8fbd5fecc787c621f603503d0f5d (plain)
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
---
title: Socket Activation with Popular Daemons
category: Manuals and Documentation for Users and Administrators
layout: default
SPDX-License-Identifier: LGPL-2.1-or-later
---

## nginx

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.

**/etc/nginx/my-nginx.conf**

```
http {
  server {
    listen [::]:80 ipv6only=on;
    listen 80;
  }
}
```

**/etc/systemd/system/my-nginx.service**

```
[Service]
User=nginx
Group=nginx
Environment=NGINX=3:4;
ExecStart=/usr/sbin/nginx -c/etc/nginx/my-nginx.conf
PrivateNetwork=true
```

**/etc/systemd/system/my-nginx.socket**

```
[Socket]
ListenStream=80
ListenStream=0.0.0.0:80
BindIPv6Only=ipv6-only

[Install]
WantedBy=sockets.target
```

## PHP-FPM

Like nginx, PHP-FPM includes a socket-passing mechanism an environmental variable.
In PHP-FPM's case, it's `FPM_SOCKETS`.

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.

Paths are based on a Fedora 19 system.

### First, the configuration files

**/etc/php-fpm.d/my-php-fpm-pool.conf**

```
[global]
pid = /run/my-php-fpm-pool.pid ; Not really used by anything with daemonize = no, but needs to be writable.
error_log = syslog             ; Will aggregate to the service's systemd journal.
daemonize = no                 ; systemd handles the forking.

[www]
listen = /var/run/my-php-fpm-pool.socket  ; Must match systemd socket unit.
user = nginx ; Ignored but required.
group = nginx ; Ignored but required.
pm = static
pm.max_children = 10
slowlog = syslog
```

**/etc/systemd/system/my-php-fpm-pool.service**

```
[Service]
User=nginx
Group=nginx
Environment="FPM_SOCKETS=/var/run/my-php-fpm-pool.socket=3"
ExecStart=/usr/sbin/php-fpm --fpm-config=/etc/php-fpm.d/my-php-fpm-pool.conf
KillMode=process
```

**/etc/systemd/system/my-php-fpm-pool.socket**

```
[Socket]
ListenStream=/var/run/my-php-fpm-pool.socket

[Install]
WantedBy=sockets.target
```

### Second, the setup commands

```sh
sudo systemctl --system daemon-reload
sudo systemctl start my-php-fpm-pool.socket
sudo systemctl enable my-php-fpm-pool.socket
```

After accessing the web server, the service should be running.

```sh
sudo systemctl status my-php-fpm-pool.service
```

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.

```sh
sudo systemctl stop my-php-fpm-pool.socket my-php-fpm-pool.service
sudo systemctl start my-php-fpm-pool.socket
```