1

I have a local website on Debian 11 which is rarely used so I thought I might want to start Apache using systemd socket activation when visiting the site and then shut down after few minutes of inactivity.

After installing apache on debian I stop and disable the service with systemctl disable --now apache2.service, then create /etc/systemd/system/apache2.socket with the following content, reload systemd with systemctl daemon-reload, and start the socket with systemctl start systemd.socket.

[Unit]
Description=Apache Server Socket

[Socket] ListenStream=80

[Install] WantedBy=sockets.target

I can confirm systemd is actually listening, and apache is started when visiting the site, but it stops immediately with error

apachectl[2794]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
apachectl[2794]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80

According to this answer it should work.

Matteo
  • 111
  • 1
  • 2

1 Answers1

2

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
larsks
  • 47,453