How to avoid repeating sed commands when adding sites to Varnish?

user149572

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

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]

編集
0

コメントを追加

0

関連記事

分類Dev

How to avoid repeating blocks of XAML in a menu

分類Dev

How to avoid repeating the following replace code?

分類Dev

How to avoid web browser warning when my site use HTTPS protocol, and it's pages include images from sites without HTTPS?

分類Dev

How can I avoid "no input files" error from sed, when run from xargs?

分類Dev

How can I avoid repeating the css and not using !important?

分類Dev

How to work with multiple sed commands in aix?

分類Dev

sed repeating the last line in the stream

分類Dev

How do I uglify code when building Gatsby sites?

分類Dev

How to left join when there are repeating left table observations?

分類Dev

How to avoid coupling when using useReducer?

分類Dev

Avoid repeating lots of queries every time

分類Dev

How to update XPaths when adding a node

分類Dev

How to ignore specific query parameter with varnish cache?

分類Dev

how to check repeating Vowels

分類Dev

How to avoid re-rendering the whole List instead of adding the new item to the DOM list in react JS?

分類Dev

How to avoid hardware compability problems when shopping for a laptop to run Linux on?

分類Dev

How to avoid slow collapse when using bootstrap collapse and div in a table

分類Dev

How to avoid duplicate key error in swift when iterating over a dictionary

分類Dev

How to avoid pylint not-an-iterable when using a custom property class

分類Dev

How to avoid setState() inside render() when state depends on render

分類Dev

Nodejs: How to avoid nested .then() when using async/await

分類Dev

how to avoid multiple http requests when same component is used twice

分類Dev

How to avoid a chained apply when selecting from multiindex?

分類Dev

Julia: how to avoid boilerplate code when structs sharing attributes

分類Dev

How to Avoid Recreating Object When Using Let with LINQ

分類Dev

How can I avoid a black background when fading in an overlay with ffmpeg?

分類Dev

How to avoid using a var when doing multiple string replacements in a string

分類Dev

how to avoid high CPU usage when no active queries

分類Dev

PHP executing external commands - how to when username and password required

Related 関連記事

  1. 1

    How to avoid repeating blocks of XAML in a menu

  2. 2

    How to avoid repeating the following replace code?

  3. 3

    How to avoid web browser warning when my site use HTTPS protocol, and it's pages include images from sites without HTTPS?

  4. 4

    How can I avoid "no input files" error from sed, when run from xargs?

  5. 5

    How can I avoid repeating the css and not using !important?

  6. 6

    How to work with multiple sed commands in aix?

  7. 7

    sed repeating the last line in the stream

  8. 8

    How do I uglify code when building Gatsby sites?

  9. 9

    How to left join when there are repeating left table observations?

  10. 10

    How to avoid coupling when using useReducer?

  11. 11

    Avoid repeating lots of queries every time

  12. 12

    How to update XPaths when adding a node

  13. 13

    How to ignore specific query parameter with varnish cache?

  14. 14

    how to check repeating Vowels

  15. 15

    How to avoid re-rendering the whole List instead of adding the new item to the DOM list in react JS?

  16. 16

    How to avoid hardware compability problems when shopping for a laptop to run Linux on?

  17. 17

    How to avoid slow collapse when using bootstrap collapse and div in a table

  18. 18

    How to avoid duplicate key error in swift when iterating over a dictionary

  19. 19

    How to avoid pylint not-an-iterable when using a custom property class

  20. 20

    How to avoid setState() inside render() when state depends on render

  21. 21

    Nodejs: How to avoid nested .then() when using async/await

  22. 22

    how to avoid multiple http requests when same component is used twice

  23. 23

    How to avoid a chained apply when selecting from multiindex?

  24. 24

    Julia: how to avoid boilerplate code when structs sharing attributes

  25. 25

    How to Avoid Recreating Object When Using Let with LINQ

  26. 26

    How can I avoid a black background when fading in an overlay with ffmpeg?

  27. 27

    How to avoid using a var when doing multiple string replacements in a string

  28. 28

    how to avoid high CPU usage when no active queries

  29. 29

    PHP executing external commands - how to when username and password required

ホットタグ

アーカイブ