1

This is my script to install Varnish. I run it each time I raise up a new server environment on a VPS.

cd ~
apt-get update
apt-get install varnish -y
sed -i 's/Listen 80/Listen 8080/g' /etc/apache2/ports.conf
sed -i 's/\*\:80/\*\:8080/g' /etc/apache2/sites-available/000-default.conf
sed -i 's/\*\:80/\*\:8080/g' /etc/apache2/sites-available/domain1.tld.conf && a2ensite domain1.tld.conf
sed -i 's/\*\:80/\*\:8080/g' /etc/apache2/sites-available/domain2.tld.conf && a2ensite domain2.tld.conf
sed -i 's/\*\:80/\*\:8080/g' /etc/apache2/sites-available/domain3.tld.conf && a2ensite domain3.tld.conf
sed -i 's/\*\:80/\*\:8080/g' /etc/apache2/sites-available/domain4.tld.conf && a2ensite domain4.tld.conf
mkdir -p /etc/systemd/system/varnish.service.d # Be aware! You might not need this in the future.
cat <<-'VARNISH' > /etc/systemd/system/varnish.service.d/customexec.conf
[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
VARNISH
systemctl restart apache2.service && systemctl daemon-reload && systemctl restart varnish.service

This code segment seems quite "heavy", especially the repetitiveness of the sed operations regarding domain.tld.

This get's even "heavier" because I have a code segment which is similar in length that uses to me uninstall varnish and revert all changes just in case of desire.

My question:

What strategy will you take to make the installation script shorter in general (at least less rows, maybe also less commands), and in particular, to lower amount of sed operations.

Notes:

  • I would assume that the first thing to do is to somehow unify ports.conf, 000-default.conf, and each .conf file of each site, all into one operation. Maybe via a for loop on /etc/apache2/ports.conf/ && /etc/apache2/sites-available/*/.
  • Besides using a `for ... in` loop for those domains: In your `sed` I would suggest to use `s/\*:80/&80/g`, because then `:` should not be escaped and you can use `&` in the replacement for the whole matched string. – Philippos May 18 '17 at 06:29
  • Hint: you can pass multiple filenames to `sed -i`. – Sparhawk May 18 '17 at 06:48
  • @Philippos, will you please add explanation why & allows this writing without error? –  May 18 '17 at 07:07
  • @Sparhawk If I do so with 000-default.conf and all other Vhost files it becomes very long so I have to horizontally scroll to get to the end. It's less heavy in rows, but heavy in a row. –  May 18 '17 at 07:09
  • Can you use globs? e.g. `/etc/apache2/sites-available/{000-default,domain{1..4}}.conf` – Sparhawk May 18 '17 at 07:12
  • Yes. Seems a nice way. –  May 18 '17 at 07:14
  • @Benia From the `sed` man page: "*An ('&') appearing in the replacement shall be replaced by the string matching the BRE.*" – Philippos May 18 '17 at 07:33
  • @Sparhawk, seems good eh? `/etc/apache2/sites-available/{000-default.conf, *.com.conf}` :) –  May 18 '17 at 08:29
  • What does the `com` part match? Is that a typo? I'd also be wary of using `*` instead of specifying `{1..4}`, just in case there are other files there later. I guess you know your system though. Finally, you can lose 5 characters by putting the `.conf` outside the braces. – Sparhawk May 18 '17 at 10:18
  • .com uses me to make sure it works only on conf files of websites (with .com tld) instead on file like 000-default.conf, etc. –  May 18 '17 at 11:12

1 Answers1

0

Using a function and GNU Parallel you replace the repetetive section:

cd ~
apt-get update
apt-get install varnish -y
sed -i 's/Listen 80/Listen 8080/g' /etc/apache2/ports.conf

myfunc() {
    sed -i 's/\*\:80/\*\:8080/g' /etc/apache2/sites-available/$1 && 
      a2ensite $1
}
export -f myfunc

parallel myfunc  {/} ::: /etc/apache2/sites-available/*

mkdir -p /etc/systemd/system/varnish.service.d # Be aware! You might not need this in the future.
cat <<-'VARNISH' > /etc/systemd/system/varnish.service.d/customexec.conf
[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
VARNISH
systemctl restart apache2.service && systemctl daemon-reload && systemctl restart varnish.service
Ole Tange
  • 33,591
  • 31
  • 102
  • 198