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/*/.