You can use systemd socket activation with Apache 2.4. The Fedora package has this configuration available by default; you just need to systemctl enable httpd.socket
instead of systemctl enable httpd.service
.
This example is from Fedora 37, which includes Apache 2.4.55.
The socket file look like:
[Unit]
Description=Apache httpd Server Socket
Documentation=man:httpd.socket(8)
[Socket]
ListenStream=80
NoDelay=true
DeferAcceptSec=30
[Install]
WantedBy=sockets.target
And the service unit is just the standard httpd.service
:
[Unit]
Description=The Apache HTTP Server
Wants=httpd-init.service
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Documentation=man:httpd.service(8)
[Service]
Type=notify
Environment=LANG=C
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
PrivateTmp=true
OOMPolicy=continue
[Install]
WantedBy=multi-user.target
With this in place, when I activate the socket (systemctl start httpd.socket
) there are no httpd
processes running:
# ps -fe |grep httpd
root 1303 944 0 15:11 pts/0 00:00:00 grep --color=auto httpd
#
But systemd is listening on port 80:
# ss -tlnp | grep :80
LISTEN 0 4096 *:80 *:* users:(("systemd",pid=1,fd=52))
If I connect to the socket:
# curl localhost
I can see that Apache is now handling connections:
[root@localhost system]# ps -fe | grep httpd
root 1309 1 0 15:12 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1310 1309 0 15:12 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1311 1309 0 15:12 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1312 1309 0 15:12 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 1313 1309 0 15:12 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 1494 944 0 15:13 pts/0 00:00:00 grep --color=auto httpd