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.
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.
/etc/apache2/ports.conf/ && /etc/apache2/sites-available/*/
.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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加