[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#798430: apache2: please add systemd service file



Control: tags -1 + patch

Hi Stehan,

On Mon, 19 Sep 2016, Raphael Hertzog wrote:
> I'm going to work on this as we really want apache2 in stretch to have a
> unit file so that we can benefit from all the systemd features for
> Apache too.

Please find attached the patches to apply on your git repository. I tested
the resulting packages and it seems to work fine for me:

$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2016-09-20 11:55:28 EDT; 2s ago
  Process: 3683 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
  Process: 3696 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 3700 (apache2)
    Tasks: 55 (limit: 4915)
   CGroup: /system.slice/apache2.service
           ├─3700 /usr/sbin/apache2 -k start
           ├─3701 /usr/sbin/apache2 -k start
           └─3702 /usr/sbin/apache2 -k start

Sep 20 11:55:28 debian-stretch-desktop systemd[1]: Starting The Apache HTTP Server...
Sep 20 11:55:28 debian-stretch-desktop apachectl[3696]: AH00558: apache2: Could not reliably determine th
Sep 20 11:55:28 debian-stretch-desktop systemd[1]: Started The Apache HTTP Server.
$ sudo systemctl stop apache2
$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2016-09-20 11:56:57 EDT; 2s ago
  Process: 3904 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
  Process: 3763 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
  Process: 3837 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 3841 (code=exited, status=0/SUCCESS)

Sep 20 11:56:13 debian-stretch-desktop systemd[1]: Starting The Apache HTTP Server...
Sep 20 11:56:13 debian-stretch-desktop apachectl[3837]: AH00558: apache2: Could not reliably determine th
Sep 20 11:56:13 debian-stretch-desktop systemd[1]: Started The Apache HTTP Server.
Sep 20 11:56:57 debian-stretch-desktop systemd[1]: Stopping The Apache HTTP Server...
Sep 20 11:56:57 debian-stretch-desktop apachectl[3904]: AH00558: apache2: Could not reliably determine th
Sep 20 11:56:57 debian-stretch-desktop systemd[1]: Stopped The Apache HTTP Server.

$ sudo systemctl is-enabled apache-htcacheclean.service 
disabled
$ sudo a2enmod cache_disk
Considering dependency cache for cache_disk:
Module cache already enabled
Enabling module cache_disk.
Enabling external service apache-htcacheclean
Synchronizing state of apache-htcacheclean.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache-htcacheclean
insserv: warning: current start runlevel(s) (empty) of script `apache-htcacheclean' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `apache-htcacheclean' overrides LSB defaults (0 1 6).
The service will be started on next reboot.
To activate the new configuration, you need to run:
  systemctl restart apache2
  systemctl start apache-htcacheclean
$ sudo systemctl is-enabled apache-htcacheclean.service
enabled
$ sudo systemctl start apache-htcacheclean
$ sudo systemctl status apache-htcacheclean|cat
● apache-htcacheclean.service - Disk Cache Cleaning Daemon for Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache-htcacheclean.service; enabled; vendor preset: enabled)
   Active: active (running) since mar. 2016-09-20 12:56:52 EDT; 27s ago
  Process: 4766 ExecStart=/usr/bin/htcacheclean -d $HTCACHECLEAN_DAEMON_INTERVAL -p $HTCACHECLEAN_PATH -l $HTCACHECLEAN_SIZE $HTCACHECLEAN_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 4767 (htcacheclean)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/apache-htcacheclean.service
           └─4767 /usr/bin/htcacheclean -d 120 -p /var/cache/apache2/mod_cache_disk -l 300M -n

sept. 20 12:56:52 debian-stretch-desktop systemd[1]: Starting Disk Cache Cleaning Daemon for Apache HTTP Server...
sept. 20 12:56:52 debian-stretch-desktop systemd[1]: Started Disk Cache Cleaning Daemon for Apache HTTP Server.


I tested the multi-instance support as well:

$ sudo sh /usr/share/doc/apache2/examples/setup-instance dev
Setting up /etc/apache2-dev ...
systemd is in use, no init script installed
use the 'apache2@dev.service' service to control your new instance
sample commands:
systemctl start apache2@dev.service
systemctl enable apache2@dev.service
Setting up symlinks: a2enmod-dev a2dismod-dev a2ensite-dev a2dissite-dev a2enconf-dev a2disconf-dev apache2ctl-dev
Setting up /etc/logrotate.d/apache2-dev and /var/log/apache2-dev ...
Setting up /etc/default/apache-htcacheclean-dev
$ sudo vim /etc/apache2-dev/ports.conf 
$ sudo systemctl start apache2@dev.service
$ sudo systemctl status apache2@dev.service
● apache2@dev.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2@.service; disabled; vendor preset
   Active: active (running) since Tue 2016-09-20 11:48:27 EDT; 5s ago
  Process: 3305 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCE
 Main PID: 3316 (apache2)
    Tasks: 55 (limit: 4915)
   CGroup: /system.slice/system-apache2.slice/apache2@dev.service
           ├─3316 /usr/sbin/apache2 -d /etc/apache2-dev -k start
           ├─3317 /usr/sbin/apache2 -d /etc/apache2-dev -k start
           └─3318 /usr/sbin/apache2 -d /etc/apache2-dev -k start

Sep 20 11:48:27 debian-stretch-desktop systemd[1]: Starting The Apache HTTP Serv
Sep 20 11:48:27 debian-stretch-desktop apachectl[3305]: AH00558: apache2: Could 
Sep 20 11:48:27 debian-stretch-desktop systemd[1]: Started The Apache HTTP Serve
$ sudo a2enmod-dev cache_disk
Considering dependency cache for cache_disk:
Enabling module cache.
Enabling module cache_disk.
Enabling external service apache-htcacheclean@dev.service
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean@dev.service -> /lib/systemd/system/apache-htcacheclean@.service.
The service will be started on next reboot.
To activate the new configuration, you need to run:
  systemctl restart apache2@dev.service
  systemctl start apache-htcacheclean@dev.service
$ sudo cp -a /var/cache/apache2/  /var/cache/apache2-dev/
$ sudo systemctl start apache-htcacheclean@dev.service
$ sudo systemctl status apache-htcacheclean@dev.service
● apache-htcacheclean@dev.service - Disk Cache Cleaning Daemon for Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache-htcacheclean@.service; enabled; vendor preset: enabled)
   Active: active (running) since mar. 2016-09-20 13:01:38 EDT; 5s ago
  Process: 4887 ExecStart=/usr/bin/htcacheclean -d $HTCACHECLEAN_DAEMON_INTERVAL -p $HTCACHECLEAN_PATH -l
 Main PID: 4888 (htcacheclean)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/system-apache\x2dhtcacheclean.slice/apache-htcacheclean@dev.service
           └─4888 /usr/bin/htcacheclean -d 120 -p /var/cache/apache2-dev/mod_cache_disk -l 300M -n

sept. 20 13:01:38 debian-stretch-desktop systemd[1]: Starting Disk Cache Cleaning Daemon for Apache HTTP 
sept. 20 13:01:38 debian-stretch-desktop systemd[1]: Started Disk Cache Cleaning Daemon for Apache HTTP S


I had to make changes to the postinst to account for the changes of
Default-Start values.

I tested the upgrade from jessie:
$ sudo dpkg -Oi *.deb
(Reading database ... 143819 files and directories currently installed.)
Preparing to unpack apache2_2.4.23-5_amd64.deb ...
Restoring lost conffile /etc/apache2/mods-available/cern_meta.load.
Restoring lost conffile /etc/apache2/mods-available/imagemap.load.
Unpacking apache2 (2.4.23-5) over (2.4.10-10+deb8u7) ...
Preparing to unpack apache2-bin_2.4.23-5_amd64.deb ...
Unpacking apache2-bin (2.4.23-5) over (2.4.10-10+deb8u7) ...
Preparing to unpack apache2-data_2.4.23-5_all.deb ...
Unpacking apache2-data (2.4.23-5) over (2.4.10-10+deb8u7) ...
Skipping unselected package apache2-dbg.
Skipping unselected package apache2-dev.
Skipping unselected package apache2-doc.
Skipping unselected package apache2-suexec-custom.
Skipping unselected package apache2-suexec-pristine.
Preparing to unpack apache2-utils_2.4.23-5_amd64.deb ...
Unpacking apache2-utils (2.4.23-5) over (2.4.10-10+deb8u7) ...
Setting up apache2-bin (2.4.23-5) ...
Setting up apache2-data (2.4.23-5) ...
Setting up apache2-utils (2.4.23-5) ...
Setting up apache2 (2.4.23-5) ...
Installing new version of config file /etc/apache2/apache2.conf ...
Installing new version of config file /etc/apache2/conf-available/security.conf ...
Installing new version of config file /etc/apache2/mods-available/proxy_html.load ...
Installing new version of config file /etc/apache2/mods-available/userdir.conf ...
Installing new version of config file /etc/apache2/sites-available/default-ssl.conf ...
Installing new version of config file /etc/cron.daily/apache2 ...
Installing new version of config file /etc/init.d/apache2 ...
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Removing obsolete conffile /etc/bash_completion.d/apache2 ...
insserv: warning: current start runlevel(s) (empty) of script `apache-htcacheclean' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `apache-htcacheclean' overrides LSB defaults (0 1 6).
Processing triggers for systemd (231-4) ...
Processing triggers for man-db (2.7.5-1) ...
$ systemctl status apache-htcacheclean.service 
● apache-htcacheclean.service - Disk Cache Cleaning Daemon for Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache-htcacheclean.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Then the same with cache_disk enabled:
$ sudo dpkg -Oi *.deb
(Reading database ... 143819 files and directories currently installed.)
Preparing to unpack apache2_2.4.23-5_amd64.deb ...
Restoring lost conffile /etc/apache2/mods-available/cern_meta.load.
Restoring lost conffile /etc/apache2/mods-available/imagemap.load.
Unpacking apache2 (2.4.23-5) over (2.4.10-10+deb8u7) ...
Preparing to unpack apache2-bin_2.4.23-5_amd64.deb ...
Unpacking apache2-bin (2.4.23-5) over (2.4.10-10+deb8u7) ...
Preparing to unpack apache2-data_2.4.23-5_all.deb ...
Unpacking apache2-data (2.4.23-5) over (2.4.10-10+deb8u7) ...
Skipping unselected package apache2-dbg.
Skipping unselected package apache2-dev.
Skipping unselected package apache2-doc.
Skipping unselected package apache2-suexec-custom.
Skipping unselected package apache2-suexec-pristine.
Preparing to unpack apache2-utils_2.4.23-5_amd64.deb ...
Unpacking apache2-utils (2.4.23-5) over (2.4.10-10+deb8u7) ...
Setting up apache2-bin (2.4.23-5) ...
Setting up apache2-data (2.4.23-5) ...
Setting up apache2-utils (2.4.23-5) ...
Setting up apache2 (2.4.23-5) ...
Installing new version of config file /etc/apache2/apache2.conf ...
Installing new version of config file /etc/apache2/conf-available/security.conf ...
Installing new version of config file /etc/apache2/mods-available/proxy_html.load ...
Installing new version of config file /etc/apache2/mods-available/userdir.conf ...
Installing new version of config file /etc/apache2/sites-available/default-ssl.conf ...
Installing new version of config file /etc/cron.daily/apache2 ...
Installing new version of config file /etc/init.d/apache2 ...
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Removing obsolete conffile /etc/bash_completion.d/apache2 ...
Processing triggers for systemd (231-4) ...
Processing triggers for man-db (2.7.5-1) ...
$ systemctl status apache-htcacheclean.service 
● apache-htcacheclean.service - Disk Cache Cleaning Daemon for Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache-htcacheclean.service; enabled; vendor preset: enabled)
   Active: active (running) since mer. 2016-09-21 05:21:34 EDT; 7s ago
 Main PID: 13453 (htcacheclean)
   CGroup: /system.slice/apache-htcacheclean.service
           └─13453 /usr/bin/htcacheclean -d 120 -p /var/cache/apache2/mod_cache_disk -l 300M -n

I also test a fresh install and apache-htcacheclean ends up disabled and not
running as well.

I think this meets all the requirements to be merged.

Cheers,
-- 
Raphaël Hertzog ◈ Debian Developer

Support Debian LTS: http://www.freexian.com/services/debian-lts.html
Learn to master Debian: http://debian-handbook.info/get/
>From 1bef5ddabce451f9e738c7313eaa756770456252 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
Date: Tue, 20 Sep 2016 14:56:30 +0200
Subject: [PATCH 1/6] Make it obvious that the init script reads a different
 default file depending on its name

---
 debian/apache2.apache-htcacheclean.init | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/debian/apache2.apache-htcacheclean.init b/debian/apache2.apache-htcacheclean.init
index cbe8828..026aec9 100755
--- a/debian/apache2.apache-htcacheclean.init
+++ b/debian/apache2.apache-htcacheclean.init
@@ -24,7 +24,7 @@ DIR_SUFFIX="${NAME##apache-htcacheclean}"
 APACHE_CONFDIR="${APACHE_CONFDIR:=/etc/apache2$DIR_SUFFIX}"
 RUN_USER=$(. $APACHE_CONFDIR/envvars > /dev/null && echo "$APACHE_RUN_USER")
 
-# Default values. Edit /etc/default/apache-htcacheclean to change these
+# Default values. Edit /etc/default/apache-htcacheclean$DIR_SUFFIX to change these
 HTCACHECLEAN_SIZE="${HTCACHECLEAN_SIZE:=300M}"
 HTCACHECLEAN_DAEMON_INTERVAL="${HTCACHECLEAN_DAEMON_INTERVAL:=120}"
 HTCACHECLEAN_PATH="${HTCACHECLEAN_PATH:=/var/cache/apache2$DIR_SUFFIX/mod_cache_disk}"
-- 
2.9.3

>From 0a58b89af7fafc04285a9b3bee43cd3c02864aba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
Date: Tue, 20 Sep 2016 15:39:15 +0200
Subject: [PATCH 2/6] Add systemd unit files. Closes: #798430

Partly based on former work of Timo Aaltonen <tjaalton@debian.org>.
---
 debian/apache2.apache-htcacheclean.service  | 16 ++++++++++++++++
 debian/apache2.apache-htcacheclean@.service | 16 ++++++++++++++++
 debian/apache2.service                      | 14 ++++++++++++++
 debian/apache2@.service                     | 16 ++++++++++++++++
 debian/changelog                            |  4 ++++
 debian/control                              |  3 ++-
 debian/rules                                |  8 +++++++-
 7 files changed, 75 insertions(+), 2 deletions(-)
 create mode 100644 debian/apache2.apache-htcacheclean.service
 create mode 100644 debian/apache2.apache-htcacheclean@.service
 create mode 100644 debian/apache2.service
 create mode 100644 debian/apache2@.service

diff --git a/debian/apache2.apache-htcacheclean.service b/debian/apache2.apache-htcacheclean.service
new file mode 100644
index 0000000..70222f3
--- /dev/null
+++ b/debian/apache2.apache-htcacheclean.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=Disk Cache Cleaning Daemon for Apache HTTP Server
+After=apache2.service
+
+[Service]
+Type=forking
+User=www-data
+Environment=HTCACHECLEAN_SIZE=300M
+Environment=HTCACHECLEAN_DAEMON_INTERVAL=120
+Environment=HTCACHECLEAN_PATH=/var/cache/apache2/mod_cache_disk
+Environment=HTCACHECLEAN_OPTIONS=-n
+EnvironmentFile=-/etc/default/apache-htcacheclean
+ExecStart=/usr/bin/htcacheclean -d $HTCACHECLEAN_DAEMON_INTERVAL -p $HTCACHECLEAN_PATH -l $HTCACHECLEAN_SIZE $HTCACHECLEAN_OPTIONS
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/apache2.apache-htcacheclean@.service b/debian/apache2.apache-htcacheclean@.service
new file mode 100644
index 0000000..1ec2f62
--- /dev/null
+++ b/debian/apache2.apache-htcacheclean@.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=Disk Cache Cleaning Daemon for Apache HTTP Server
+After=apache2@%i.service
+
+[Service]
+Type=forking
+User=www-data
+Environment=HTCACHECLEAN_SIZE=300M
+Environment=HTCACHECLEAN_DAEMON_INTERVAL=120
+Environment=HTCACHECLEAN_PATH=/var/cache/apache2-%I/mod_cache_disk
+Environment=HTCACHECLEAN_OPTIONS=-n
+EnvironmentFile=-/etc/default/apache-htcacheclean-%I
+ExecStart=/usr/bin/htcacheclean -d $HTCACHECLEAN_DAEMON_INTERVAL -p $HTCACHECLEAN_PATH -l $HTCACHECLEAN_SIZE $HTCACHECLEAN_OPTIONS
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/apache2.service b/debian/apache2.service
new file mode 100644
index 0000000..92a371a
--- /dev/null
+++ b/debian/apache2.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=The Apache HTTP Server
+After=network.target remote-fs.target nss-lookup.target
+
+[Service]
+Type=forking
+ExecStart=/usr/sbin/apachectl start
+ExecStop=/usr/sbin/apachectl stop
+ExecReload=/usr/sbin/apachectl graceful
+PrivateTmp=true
+Restart=on-abort
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/apache2@.service b/debian/apache2@.service
new file mode 100644
index 0000000..c072050
--- /dev/null
+++ b/debian/apache2@.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=The Apache HTTP Server
+After=network.target remote-fs.target nss-lookup.target
+ConditionPathIsDirectory=/etc/apache2-%I
+
+[Service]
+Type=forking
+Environment=APACHE_CONFDIR=/etc/apache2-%I
+ExecStart=/usr/sbin/apachectl start
+ExecStop=/usr/sbin/apachectl stop
+ExecReload=/usr/sbin/apachectl graceful
+PrivateTmp=true
+Restart=on-abort
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/changelog b/debian/changelog
index 861d285..4eef0f3 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,8 +1,12 @@
 apache2 (2.4.23-5) UNRELEASED; urgency=medium
 
+  [ Stefan Fritsch ]
   * Tweak creation of .tar.gz embedded in preinst to get reproducible
     build.
 
+  [ Raphaël Hertzog ]
+  * Add systemd unit files. Closes: #798430
+
  -- Stefan Fritsch <sf@debian.org>  Sat, 13 Aug 2016 13:38:30 +0200
 
 apache2 (2.4.23-4) unstable; urgency=medium
diff --git a/debian/control b/debian/control
index 151d462..9bc98a1 100644
--- a/debian/control
+++ b/debian/control
@@ -6,7 +6,8 @@ Uploaders: Stefan Fritsch <sf@debian.org>, Arno Töll <arno@debian.org>
 Build-Depends: debhelper (>= 9.20131213~), lsb-release, dpkg-dev (>= 1.16.1~),
  libaprutil1-dev (>= 1.5.0), libapr1-dev (>= 1.5.0), libpcre3-dev, zlib1g-dev,
  libnghttp2-dev, libssl-dev (>= 0.9.8m), perl,
- liblua5.2-dev, libxml2-dev, autotools-dev, gawk | awk
+ liblua5.2-dev, libxml2-dev, autotools-dev, gawk | awk,
+ dh-systemd
 Build-Conflicts: autoconf2.13
 Standards-Version: 3.9.8
 Vcs-Browser: https://anonscm.debian.org/cgit/pkg-apache/apache2.git/
diff --git a/debian/rules b/debian/rules
index 0202f75..00dff14 100755
--- a/debian/rules
+++ b/debian/rules
@@ -96,7 +96,7 @@ generate-maintainer-scripts: debian/apache2.preinst
 	done
 
 clean build build-arch build-indep binary binary-arch binary-indep: %:
-	dh $@ --parallel --with autotools_dev
+	dh $@ --parallel --with autotools_dev,systemd
 
 override_dh_auto_configure: generate-maintainer-scripts prebuild-checks support/suexec-custom.c
 	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
@@ -150,6 +150,12 @@ override_dh_installinit:
 	dh_installinit --error-handler=true
 	dh_installinit --name apache-htcacheclean --noscripts
 
+override_dh_systemd_enable:
+	dh_systemd_enable -papache2 apache2.service
+	dh_systemd_enable -papache2 --name=apache2@ apache2@.service
+	dh_systemd_enable -papache2 --no-enable --name=apache-htcacheclean apache-htcacheclean.service
+	dh_systemd_enable -papache2 --no-enable --name=apache-htcacheclean@ apache-htcacheclean@.service
+
 override_dh_installdocs-indep:
 	# TODO: So, did anyone check convert_docs needs an update? ;)
 	perl debian/convert_docs debian/apache2-doc/usr/share/doc/apache2-doc/manual
-- 
2.9.3

>From 087e8f7f1d5e613f5db166df1b3545c5ea48bbee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
Date: Tue, 20 Sep 2016 16:03:56 +0200
Subject: [PATCH 3/6] a2enmod: implement systemd support

Use systemctl when systemd is in use and make it aware of
the apache-htcacheclean@instance.service generic service
for multi-instance support.
---
 debian/a2enmod   | 41 ++++++++++++++++++++++++++++++-----------
 debian/changelog |  3 +++
 2 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/debian/a2enmod b/debian/a2enmod
index 350a285..3116688 100755
--- a/debian/a2enmod
+++ b/debian/a2enmod
@@ -84,7 +84,14 @@ my $linkdir = File::Spec->abs2rel( $availdir, $enabldir );
 my $request_reload = 0;
 my $request_htcacheclean;
 my $htc = "apache-htcacheclean$dir_suffix";
-
+my $htc_service = "apache-htcacheclean";  # Service name for systemd
+my $apache_service = "apache2";
+if (defined($dir_suffix) and $dir_suffix ne '') {
+    # Uses '@instance.service' suffix instead of '-instance' suffix
+    my $service_suffix = '@' . substr($dir_suffix, 1) . '.service';
+    $htc_service .= $service_suffix;
+    $apache_service .= $service_suffix;
+}
 my $rc = 0;
 
 if ( !scalar @ARGV ) {
@@ -114,13 +121,18 @@ foreach my $acton (@objs) {
 }
 
 my $htcstart = "";
-if ($request_htcacheclean) {
-    my $cmd = ($act eq "enable") ? "start" : "stop";
+my $apache_reload = "";
+my $cmd = ($act eq "enable") ? "start" : "stop";
+if (-d "/run/systemd" and -x "/bin/systemctl") {
+    $htcstart = "  systemctl $cmd $htc_service\n";
+    $apache_reload = "  systemctl $reload $apache_service\n";
+} else {
     $htcstart = "  service $htc $cmd\n";
+    $apache_reload = "  service apache2$dir_suffix $reload\n";
 }
 info(  "To activate the new configuration, you need to run:\n"
-     . "  service apache2$dir_suffix $reload\n"
-     . $htcstart
+     . $apache_reload
+     . ($request_htcacheclean ? $htcstart : "")
 ) if $request_reload;
 
 exit($rc);
@@ -574,17 +586,24 @@ sub special_module_handling {
         when ('cache_disk') {
             $request_htcacheclean = 1;
             my $verb = "\u$act";
+            my $command;
             $verb =~ s/e$/ing/;
-            info("$verb external service $htc\n");
-            # The init script has no Default-Start runlevels, so we need to
-            # specify them explicitly.
-            system("update-rc.d $htc $act 2 3 4 5");
-            if ($rc == 0) {
+            if (-d "/run/systemd" and -x "/bin/systemctl") {
+                info("$verb external service $htc_service\n");
+                $command = "systemctl $act $htc_service";
+            } else {
+                info("$verb external service $htc\n");
+                # The init script has no Default-Start runlevels, so we need to
+                # specify them explicitly.
+                $command = "update-rc.d $htc $act 2 3 4 5";
+            }
+            my $res = system($command);
+            if ($res == 0) {
                 info("The service will be started on next reboot.\n")
                     if $act eq 'enable';
             }
             else {
-                warning("update-rc.d failed\n");
+                warning("'$command' failed\n");
             }
 
         }
diff --git a/debian/changelog b/debian/changelog
index 4eef0f3..dfec91d 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -6,6 +6,9 @@ apache2 (2.4.23-5) UNRELEASED; urgency=medium
 
   [ Raphaël Hertzog ]
   * Add systemd unit files. Closes: #798430
+  * Improve a2enmod to enable apache-htcacheclean with systemctl and let
+    it enable 'apache-htcacheclean@instance.service' for multi-instance
+    support.
 
  -- Stefan Fritsch <sf@debian.org>  Sat, 13 Aug 2016 13:38:30 +0200
 
-- 
2.9.3

>From 0a6976aa5ed4a42e4ada6fb9f279423e8fbeec8a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
Date: Tue, 20 Sep 2016 16:11:57 +0200
Subject: [PATCH 4/6] setup-instance: better systemd support

Rely on the systemd apache2@instance.service for multi-instance support.
Update the associated README.
---
 debian/README.multiple-instances | 32 +++++++++++++++++++++++---------
 debian/changelog                 |  2 ++
 debian/setup-instance            | 21 ++++++++++++++++-----
 3 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/debian/README.multiple-instances b/debian/README.multiple-instances
index 77dcf98..42c3baf 100644
--- a/debian/README.multiple-instances
+++ b/debian/README.multiple-instances
@@ -14,12 +14,16 @@ Adjust the configuration in /etc/apache2-xxx, especially the listen ports in
 ports.conf and in the virtual host directives in
 /etc/apache2-xxx/sites-enabled/*.
 
-You can then use a2enmod-xxx, /etc/init.d/apache2-xxx, ... as usual, and they
+You can then use a2enmod-xxx, apache2ctl-xxx, ... as usual, and they
 will affect the new instance of apache2.
 
+If you use systemd, the service name of your new instance is
+"apache2@xxx.service". Otherwise the script installed an init script
+named /etc/init.d/apache2-xxx.
+
 To start the new apache2 instance on boot, use
 - if you use systemd, run:
-	systemctl enable apache2-xxx
+	systemctl enable apache2@xxx
 - if you use sysv-init with dependency based boot sequence (the default):
   If you don't have ssl keys with passphrases, you may want to set
   'X-Interactive: false' in /etc/init.d/apache2-xxx to reduce boot time.
@@ -57,13 +61,24 @@ The Apache 2 package will only remove the default configuration files and
 directories.
 
 
-Configuration
-=============
+htcacheclean service
+====================
+
+The apache-htcacheclean service can be handled in the same way as the
+main apache2 script. You can create an
+'apache-htcacheclean@instance.service' service under systemd or you can
+copy /etc/init.d/apache-htcacheclean as
+/etc/init.d/apache-htcacheclean-xxx under SystemV.
+
+The parameters of the service can be customized with
+/etc/default/apache-htcacheclean-xxx (that is created by the
+setup-instance helper script).
 
-The init script will try to read /etc/defaults/apache2-xxx. If that does not
-exist, it will use /etc/defaults/apache2 instead.
 
-The following environment variables can be used to influence the scripts.  The
+Environment variables
+=====================
+
+The following environment variables can be used to influence many scripts.  The
 default apache2 configuration will make use of them, too. Most can be set in
 /etc/apache2-xxx/envvars. Variables set in /etc/apache2-xxx/envvars must be
 exported.
@@ -79,7 +94,7 @@ APACHE_ARGUMENTS	empty if $APACHE_CONFDIR = /etc/apache2
 APACHE_RUN_USER		www-data
 APACHE_RUN_GROUP	www-data
 
-APACHE_PID_FILE         /var/run/apache2.pid or /var/run/apachd2-xxx.pid
+APACHE_PID_FILE         /var/run/apache2/apache2.pid or /var/run/apache2-xxx/apache2.pid
 APACHE_RUN_DIR		/var/run/apache2     or /var/run/apache2-xxx
 APACHE_LOCK_DIR		/var/lock/apache2    or /var/lock/apache2-xxx
 APACHE_LOG_DIR		/var/log/apache2     or /var/log/apache2-xxx
@@ -91,4 +106,3 @@ APACHE_SITES_ENABLED	$APACHE_CONFDIR/sites-enabled
 
 APACHE_LYNX		www-browser -dump
 APACHE_STATUSURL	http://localhost:80/server-status
-
diff --git a/debian/changelog b/debian/changelog
index dfec91d..8eb8e88 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -9,6 +9,8 @@ apache2 (2.4.23-5) UNRELEASED; urgency=medium
   * Improve a2enmod to enable apache-htcacheclean with systemctl and let
     it enable 'apache-htcacheclean@instance.service' for multi-instance
     support.
+  * Improve setup-instance to rely on the systemd apache2@instance.service for
+    multi-instance support.
 
  -- Stefan Fritsch <sf@debian.org>  Sat, 13 Aug 2016 13:38:30 +0200
 
diff --git a/debian/setup-instance b/debian/setup-instance
index 2c8e499..e404dec 100644
--- a/debian/setup-instance
+++ b/debian/setup-instance
@@ -18,11 +18,19 @@ fi
 echo Setting up /etc/apache2-$SUFFIX ...
 cp -a /etc/apache2 /etc/apache2-$SUFFIX
 
-echo Setting up /etc/init.d/apache2-$SUFFIX ...
-cp /usr/share/doc/apache2/examples/secondary-init-script /etc/init.d/apache2-$SUFFIX
-# adjust service name (this prevents us from using a simple symlink)
-perl -p -i -e s,XXX,$SUFFIX, /etc/init.d/apache2-$SUFFIX
-chmod 755 /etc/init.d/apache2-$SUFFIX
+if [ -d /run/systemd ] && [ -x /bin/systemctl ]; then
+    echo "systemd is in use, no init script installed"
+    echo "use the 'apache2@$SUFFIX.service' service to control your new instance"
+    echo "sample commands:"
+    echo "systemctl start apache2@$SUFFIX.service"
+    echo "systemctl enable apache2@$SUFFIX.service"
+else
+    echo "Setting up /etc/init.d/apache2-$SUFFIX ..."
+    cp /usr/share/doc/apache2/examples/secondary-init-script /etc/init.d/apache2-$SUFFIX
+    # adjust service name (this prevents us from using a simple symlink)
+    perl -p -i -e s,XXX,$SUFFIX, /etc/init.d/apache2-$SUFFIX
+    chmod 755 /etc/init.d/apache2-$SUFFIX
+fi
 
 echo -n Setting up symlinks: 
 for a in a2enmod a2dismod a2ensite a2dissite a2enconf a2disconf apache2ctl ; do
@@ -37,3 +45,6 @@ perl -p -i -e s,apache2,apache2-$SUFFIX,g /etc/logrotate.d/apache2-$SUFFIX
 mkdir /var/log/apache2-$SUFFIX
 chmod 750 /var/log/apache2-$SUFFIX
 chown root:adm /var/log/apache2-$SUFFIX
+
+echo "Setting up /etc/default/apache-htcacheclean-$SUFFIX"
+cp -a /etc/default/apache-htcacheclean /etc/default/apache-htcacheclean-$SUFFIX
-- 
2.9.3

>From 53bda30687048663706cb04a8d728e61e356368b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
Date: Tue, 20 Sep 2016 17:33:21 +0200
Subject: [PATCH 5/6] Drop /lib/systemd/system/apache2.service.d/forking.conf

We have proper native systemd support now.
---
 debian/apache2.install | 1 -
 debian/changelog       | 2 ++
 debian/forking.conf    | 3 ---
 3 files changed, 2 insertions(+), 4 deletions(-)
 delete mode 100644 debian/forking.conf

diff --git a/debian/apache2.install b/debian/apache2.install
index 4972511..798d2f0 100644
--- a/debian/apache2.install
+++ b/debian/apache2.install
@@ -8,4 +8,3 @@ debian/apache2ctl				/usr/sbin
 debian/a2query					/usr/sbin
 debian/ask-for-passphrase			/usr/share/apache2/
 debian/debhelper/apache2-maintscript-helper	/usr/share/apache2/
-debian/forking.conf				/lib/systemd/system/apache2.service.d
diff --git a/debian/changelog b/debian/changelog
index 8eb8e88..35d6988 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -11,6 +11,8 @@ apache2 (2.4.23-5) UNRELEASED; urgency=medium
     support.
   * Improve setup-instance to rely on the systemd apache2@instance.service for
     multi-instance support.
+  * Drop /lib/systemd/system/apache2.service.d/forking.conf now that we have
+    proper native systemd support.
 
  -- Stefan Fritsch <sf@debian.org>  Sat, 13 Aug 2016 13:38:30 +0200
 
diff --git a/debian/forking.conf b/debian/forking.conf
deleted file mode 100644
index b031203..0000000
--- a/debian/forking.conf
+++ /dev/null
@@ -1,3 +0,0 @@
-[Service]
-Type=forking
-RemainAfterExit=no
-- 
2.9.3

>From c5b62eaddedcb3d46d28cddb76c10e8c24612704 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
Date: Tue, 20 Sep 2016 18:52:30 +0200
Subject: [PATCH 6/6] Update apache-htcacheclean.init to have sensible
 Default-Start values

With the old value, "systemctl enable apache-htcacheclean" would fail
like this:
 Synchronizing state of apache-htcacheclean.service with SysV service script with /lib/systemd/systemd-sysv-install.
 Executing: /lib/systemd/systemd-sysv-install enable apache-htcacheclean
 update-rc.d: error: apache-htcacheclean Default-Start contains no runlevels, aborting.

So we add meta-data to make the script enabled by default but we actually disable
it immediately in the postinst. And we can clean up some hacks that we introduced
to cover for the Default-Start value being empty.
---
 debian/a2enmod                          |  4 +--
 debian/apache2.apache-htcacheclean.init |  4 +--
 debian/apache2.postinst                 | 49 +++++++++++++++++++++++----------
 debian/changelog                        |  3 ++
 debian/rules                            |  9 ++++--
 5 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/debian/a2enmod b/debian/a2enmod
index 3116688..2656a2e 100755
--- a/debian/a2enmod
+++ b/debian/a2enmod
@@ -593,9 +593,7 @@ sub special_module_handling {
                 $command = "systemctl $act $htc_service";
             } else {
                 info("$verb external service $htc\n");
-                # The init script has no Default-Start runlevels, so we need to
-                # specify them explicitly.
-                $command = "update-rc.d $htc $act 2 3 4 5";
+                $command = "update-rc.d $htc $act";
             }
             my $res = system($command);
             if ($res == 0) {
diff --git a/debian/apache2.apache-htcacheclean.init b/debian/apache2.apache-htcacheclean.init
index 026aec9..a704321 100755
--- a/debian/apache2.apache-htcacheclean.init
+++ b/debian/apache2.apache-htcacheclean.init
@@ -7,8 +7,8 @@ fi
 # Provides:          apache-htcacheclean
 # Required-Start:    $remote_fs $syslog
 # Required-Stop:     $remote_fs $syslog
-# Default-Start:
-# Default-Stop:      0 1 2 3 4 5 6
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
 # Short-Description: Cache cleaner process for Apache2 web server
 # Description:       Start the htcacheclean helper
 #  This script will start htcacheclean which will periodically scan the
diff --git a/debian/apache2.postinst b/debian/apache2.postinst
index 7063dfa..f494434 100644
--- a/debian/apache2.postinst
+++ b/debian/apache2.postinst
@@ -147,26 +147,36 @@ start_htcacheclean ()
 	fi
 }
 
+disable_htcacheclean()
+{
+	if deb-systemd-helper debian-installed apache-htcacheclean.service; then
+		deb-systemd-helper disable apache-htcacheclean.service >/dev/null || true
+	fi
+	update-rc.d apache-htcacheclean disable >/dev/null
+}
+
 # The apache-htcacheclean service is disabled by default. Can't use
-# debhelper. The update-rc.d 'enable' call must come after the 'defaults'
+# debhelper. The update-rc.d 'disable' call must come after the 'defaults'
 # call, or the former will fail.
 handle_htcacheclean ()
 {
-	if [ -x "/etc/init.d/apache-htcacheclean" ]; then
-		update-rc.d apache-htcacheclean defaults >/dev/null
+	if dpkg --compare-versions "$2" lt "2.4.18-2~"; then
+		# Disable on initial installation or when upgrading from an old
+		# version without that init script and with the module disabled
+		# (or when configured to run from cron)
+		if [ ! -e "/etc/apache2/mods-enabled/cache_disk.load" ]; then
+			disable_htcacheclean
+			return
+		elif (. /etc/default/apache-htcacheclean && [ "$HTCACHECLEAN_MODE" = "cron" ]); then
+			disable_htcacheclean
+			return
+		fi
 	fi
 
-	if dpkg --compare-versions "$2" gt "2.4.18-2~" ||
-	   [ ! -e "/etc/apache2/mods-enabled/cache_disk.load" ] ; then
-		return 0
-	fi
-	if ( . /etc/default/apache-htcacheclean &&
-		[ "$HTCACHECLEAN_MODE" != "cron" ] &&
-		[ "$HTCACHECLEAN_RUN" != "no" ] )
-	then
-		update-rc.d apache-htcacheclean enable 2 3 4 5
+	# Restart it if applicable
+	if [ -e "/etc/apache2/mods-enabled/cache_disk.load" ]; then
+		start_htcacheclean
 	fi
-	start_htcacheclean
 }
 
 msg ()
@@ -269,13 +279,12 @@ case "$1" in
 		enable_default_modules $@
 		enable_default_conf $@
 		install_default_site $@
-		handle_htcacheclean $@
 		execute_deferred_actions
 
 	;;
 
 	abort-upgrade)
-		start_htcacheclean $@
+
 	;;
 
 	abort-remove|abort-deconfigure)
@@ -290,6 +299,16 @@ esac
 
 #DEBHELPER#
 
+# Deal with htcacheclean after debhelper's initial init script handling
+case "$1" in
+	configure)
+		handle_htcacheclean $@
+	;;
+	abort-upgrade)
+		start_htcacheclean $@
+	;;
+esac
+
 exit 0
 
 # vim: syntax=sh ts=4 sw=4 sts=4 sr noet
diff --git a/debian/changelog b/debian/changelog
index 35d6988..cac4b24 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -13,6 +13,9 @@ apache2 (2.4.23-5) UNRELEASED; urgency=medium
     multi-instance support.
   * Drop /lib/systemd/system/apache2.service.d/forking.conf now that we have
     proper native systemd support.
+  * Modify handling of /etc/init.d/apache-htcacheclean to have a usual
+    Default-Start value but instead we disable it manually in the postinst.
+    That way "systemctl enable apache-htcacheclean" works.
 
  -- Stefan Fritsch <sf@debian.org>  Sat, 13 Aug 2016 13:38:30 +0200
 
diff --git a/debian/rules b/debian/rules
index 00dff14..5a96c95 100755
--- a/debian/rules
+++ b/debian/rules
@@ -148,13 +148,16 @@ override_dh_fixperms-indep:
 
 override_dh_installinit:
 	dh_installinit --error-handler=true
-	dh_installinit --name apache-htcacheclean --noscripts
+	# We enable apache-htcacheclean but we don't start it, some
+	# custom postinst code will then manually either disable it or
+	# start the service
+	dh_installinit --name apache-htcacheclean --no-start
 
 override_dh_systemd_enable:
 	dh_systemd_enable -papache2 apache2.service
 	dh_systemd_enable -papache2 --name=apache2@ apache2@.service
-	dh_systemd_enable -papache2 --no-enable --name=apache-htcacheclean apache-htcacheclean.service
-	dh_systemd_enable -papache2 --no-enable --name=apache-htcacheclean@ apache-htcacheclean@.service
+	dh_systemd_enable -papache2 --name=apache-htcacheclean apache-htcacheclean.service
+	dh_systemd_enable -papache2 --name=apache-htcacheclean@ apache-htcacheclean@.service
 
 override_dh_installdocs-indep:
 	# TODO: So, did anyone check convert_docs needs an update? ;)
-- 
2.9.3


Reply to: