From 2cc6350f0d3cd8501357e2a41577eabc8a8cd8c7 Mon Sep 17 00:00:00 2001 From: Matthias Vogel Date: Thu, 12 Nov 2026 21:48:25 +0000 Subject: [PATCH] [BUGFIX] Restart Apache on Alpine with supervisorctl and after an unclean exit On Alpine, the start script ran httpd through apachectl, a shell script that does not exec it. supervisorctl stop and restart ended apachectl, but httpd kept running with its old configuration, and the new one exited with "httpd (pid N) already running", over and over. The script also removed /var/run/httpd/httpd.pid, while Apache on Alpine writes /run/apache2/httpd.pid, so after an unclean exit a pid file that names a running process kept Apache from starting. The script now removes /run/apache2/httpd.pid and execs httpd itself. The image tests restart Apache with a new configuration and a pid file that names a running process, and check the configuration and that Apache keeps running. --- ci/image-tests/Checks/ApacheChecks.php | 24 +++++++++++++++++++ .../apache/alpine/conf/bin/service.d/httpd.sh | 7 +++--- .../8.2-alpine/conf/bin/service.d/httpd.sh | 7 +++--- .../8.3-alpine/conf/bin/service.d/httpd.sh | 7 +++--- .../8.4-alpine/conf/bin/service.d/httpd.sh | 7 +++--- .../8.5-alpine/conf/bin/service.d/httpd.sh | 7 +++--- .../apache/alpine/bin/service.d/httpd.sh | 7 +++--- .../php-apache/alpine/bin/service.d/httpd.sh | 7 +++--- 8 files changed, 52 insertions(+), 21 deletions(-) diff --git a/ci/image-tests/Checks/ApacheChecks.php b/ci/image-tests/Checks/ApacheChecks.php index 490e743ee..374511f26 100644 --- a/ci/image-tests/Checks/ApacheChecks.php +++ b/ci/image-tests/Checks/ApacheChecks.php @@ -7,6 +7,9 @@ use PHPUnit\Framework\Attributes\DataProvider; use Webdevops\Build\ImageTests\ImageTestCase; +use function sleep; +use function sprintf; + /** * Checks of the images with Apache: configuration layout, TLS files, modules, and Apache answering on port 80 and * 443. @@ -136,4 +139,25 @@ public function testApacheRuns(): void { $this->assertServiceRunning('apache'); } + + /** + * supervisorctl restart starts a new Apache, which reads a configuration added in the meantime, also when the pid + * file of the stopped one is left over and names a running process, here supervisord, as after an unclean exit. + */ + public function testSupervisorctlRestartsApache(): void + { + $container = $this->startContainer('apache-restart'); + $this->request('http://localhost/pi-number.html', container: $container); + $pidFile = self::image()->isAlpine() ? '/run/apache2/httpd.pid' : '/var/run/apache2/apache2.pid'; + $this->assertCommandSucceeds(sprintf( + "printf '%%s\n' 'Header always set X-Image-Test-Restart \"restarted\"' > /opt/docker/etc/httpd/conf.d/image-test-restart.conf && supervisorctl stop apache:apached && echo 1 > %s && supervisorctl start apache:apached", + $pidFile, + ), $container); + $this->waitFor(function () use ($container): void { + self::assertMatchesRegularExpression('/^X-Image-Test-Restart: restarted\r?$/mi', $this->stdout('curl --silent --head http://localhost/pi-number.html', $container)); + }); + $processId = $this->stdout('supervisorctl pid apache:apached', $container); + sleep(self::WAIT_SECONDS * 2); + self::assertSame($processId, $this->stdout('supervisorctl pid apache:apached', $container), 'Apache should keep running, not be restarted over and over.'); + } } diff --git a/docker/apache/alpine/conf/bin/service.d/httpd.sh b/docker/apache/alpine/conf/bin/service.d/httpd.sh index f88544c62..d82a14716 100644 --- a/docker/apache/alpine/conf/bin/service.d/httpd.sh +++ b/docker/apache/alpine/conf/bin/service.d/httpd.sh @@ -7,7 +7,8 @@ source /opt/docker/bin/config.sh includeScriptDir "/opt/docker/bin/service.d/httpd.d/" -# Apache gets grumpy about PID files pre-existing -rm -f /var/run/httpd/httpd.pid +# Apache does not start while its pid file from an earlier run names a running process +rm -f /run/apache2/httpd.pid -exec /usr/sbin/apachectl -DFOREGROUND $SERVICE_APACHE_OPTS +# httpd itself, not apachectl, which would run it as a child: supervisord has to stop and restart httpd +exec /usr/sbin/httpd -DFOREGROUND $SERVICE_APACHE_OPTS diff --git a/docker/php-apache/8.2-alpine/conf/bin/service.d/httpd.sh b/docker/php-apache/8.2-alpine/conf/bin/service.d/httpd.sh index f88544c62..d82a14716 100644 --- a/docker/php-apache/8.2-alpine/conf/bin/service.d/httpd.sh +++ b/docker/php-apache/8.2-alpine/conf/bin/service.d/httpd.sh @@ -7,7 +7,8 @@ source /opt/docker/bin/config.sh includeScriptDir "/opt/docker/bin/service.d/httpd.d/" -# Apache gets grumpy about PID files pre-existing -rm -f /var/run/httpd/httpd.pid +# Apache does not start while its pid file from an earlier run names a running process +rm -f /run/apache2/httpd.pid -exec /usr/sbin/apachectl -DFOREGROUND $SERVICE_APACHE_OPTS +# httpd itself, not apachectl, which would run it as a child: supervisord has to stop and restart httpd +exec /usr/sbin/httpd -DFOREGROUND $SERVICE_APACHE_OPTS diff --git a/docker/php-apache/8.3-alpine/conf/bin/service.d/httpd.sh b/docker/php-apache/8.3-alpine/conf/bin/service.d/httpd.sh index f88544c62..d82a14716 100644 --- a/docker/php-apache/8.3-alpine/conf/bin/service.d/httpd.sh +++ b/docker/php-apache/8.3-alpine/conf/bin/service.d/httpd.sh @@ -7,7 +7,8 @@ source /opt/docker/bin/config.sh includeScriptDir "/opt/docker/bin/service.d/httpd.d/" -# Apache gets grumpy about PID files pre-existing -rm -f /var/run/httpd/httpd.pid +# Apache does not start while its pid file from an earlier run names a running process +rm -f /run/apache2/httpd.pid -exec /usr/sbin/apachectl -DFOREGROUND $SERVICE_APACHE_OPTS +# httpd itself, not apachectl, which would run it as a child: supervisord has to stop and restart httpd +exec /usr/sbin/httpd -DFOREGROUND $SERVICE_APACHE_OPTS diff --git a/docker/php-apache/8.4-alpine/conf/bin/service.d/httpd.sh b/docker/php-apache/8.4-alpine/conf/bin/service.d/httpd.sh index f88544c62..d82a14716 100644 --- a/docker/php-apache/8.4-alpine/conf/bin/service.d/httpd.sh +++ b/docker/php-apache/8.4-alpine/conf/bin/service.d/httpd.sh @@ -7,7 +7,8 @@ source /opt/docker/bin/config.sh includeScriptDir "/opt/docker/bin/service.d/httpd.d/" -# Apache gets grumpy about PID files pre-existing -rm -f /var/run/httpd/httpd.pid +# Apache does not start while its pid file from an earlier run names a running process +rm -f /run/apache2/httpd.pid -exec /usr/sbin/apachectl -DFOREGROUND $SERVICE_APACHE_OPTS +# httpd itself, not apachectl, which would run it as a child: supervisord has to stop and restart httpd +exec /usr/sbin/httpd -DFOREGROUND $SERVICE_APACHE_OPTS diff --git a/docker/php-apache/8.5-alpine/conf/bin/service.d/httpd.sh b/docker/php-apache/8.5-alpine/conf/bin/service.d/httpd.sh index f88544c62..d82a14716 100644 --- a/docker/php-apache/8.5-alpine/conf/bin/service.d/httpd.sh +++ b/docker/php-apache/8.5-alpine/conf/bin/service.d/httpd.sh @@ -7,7 +7,8 @@ source /opt/docker/bin/config.sh includeScriptDir "/opt/docker/bin/service.d/httpd.d/" -# Apache gets grumpy about PID files pre-existing -rm -f /var/run/httpd/httpd.pid +# Apache does not start while its pid file from an earlier run names a running process +rm -f /run/apache2/httpd.pid -exec /usr/sbin/apachectl -DFOREGROUND $SERVICE_APACHE_OPTS +# httpd itself, not apachectl, which would run it as a child: supervisord has to stop and restart httpd +exec /usr/sbin/httpd -DFOREGROUND $SERVICE_APACHE_OPTS diff --git a/provisioning/apache/alpine/bin/service.d/httpd.sh b/provisioning/apache/alpine/bin/service.d/httpd.sh index f88544c62..d82a14716 100644 --- a/provisioning/apache/alpine/bin/service.d/httpd.sh +++ b/provisioning/apache/alpine/bin/service.d/httpd.sh @@ -7,7 +7,8 @@ source /opt/docker/bin/config.sh includeScriptDir "/opt/docker/bin/service.d/httpd.d/" -# Apache gets grumpy about PID files pre-existing -rm -f /var/run/httpd/httpd.pid +# Apache does not start while its pid file from an earlier run names a running process +rm -f /run/apache2/httpd.pid -exec /usr/sbin/apachectl -DFOREGROUND $SERVICE_APACHE_OPTS +# httpd itself, not apachectl, which would run it as a child: supervisord has to stop and restart httpd +exec /usr/sbin/httpd -DFOREGROUND $SERVICE_APACHE_OPTS diff --git a/provisioning/php-apache/alpine/bin/service.d/httpd.sh b/provisioning/php-apache/alpine/bin/service.d/httpd.sh index f88544c62..d82a14716 100644 --- a/provisioning/php-apache/alpine/bin/service.d/httpd.sh +++ b/provisioning/php-apache/alpine/bin/service.d/httpd.sh @@ -7,7 +7,8 @@ source /opt/docker/bin/config.sh includeScriptDir "/opt/docker/bin/service.d/httpd.d/" -# Apache gets grumpy about PID files pre-existing -rm -f /var/run/httpd/httpd.pid +# Apache does not start while its pid file from an earlier run names a running process +rm -f /run/apache2/httpd.pid -exec /usr/sbin/apachectl -DFOREGROUND $SERVICE_APACHE_OPTS +# httpd itself, not apachectl, which would run it as a child: supervisord has to stop and restart httpd +exec /usr/sbin/httpd -DFOREGROUND $SERVICE_APACHE_OPTS