How to rebuild apache configs for all sites? [SOLVED]

Discussion in 'Tips/Tricks/Mods' started by zyzzza, Jul 2, 2017.

  1. zyzzza

    zyzzza New Member

    Hi,
    I've done a script which would add additional apache rules to the DB (web_domain table) to all of my sites , but i can't find a way to rebuild the configs for all sites in one go. Could anyone advise how i can rebuild configs (so that values in DB would appear in apache configs) for all the sites on a server ?

    PS: Just for those who is wondering why is that - we need allowing only certain IPs for certain sites, and requirements are changing monthly , and having 200 clients with 300 sites is difficult to do manually .

    Thanks.
     
  2. sjau

    sjau Local Meanie Moderator

    Download ispconfig and let it make an update, at the end you'll be prompted if you want to reconfigure sevices:

    Code:
    cd /tmp
    wget http://www.ispconfig.org/downloads/ISPConfig-3-stable.tar.gz
    tar xvfz ISPConfig-3-stable.tar.gz
    cd ispconfig3_install/install
    php -q update.php
    
    OTOH you could use the remote API to alter things in a programmatically way...
     
  3. zyzzza

    zyzzza New Member

    Thanks for tip,
    however - maybe there is a single command to do this without running update script ? Also concerning the API - might use it later , but it will need time to get to it , thus at the moment would be good finding out a one-liner command to update things :)

    Thanks
     
  4. sjau

    sjau Local Meanie Moderator

    There's no way currently that I know that will rewrite all configs files from the db.

    However what you could do if yu need to add the same info to each vhost file is
    (1) use the advanced settings in ISPC to add modified code to the vhosts/websites, like maybe
    Code:
    #--BeginCustom
    xxxxxxxxxx
    #--EndCustom
    
    With that you could then e.g. use sed or ther tools from the cli to just replac that piece with your new info and to apache reload to read new configs. However, if you modify the website anyhow in IPSC, it will write back the original stuff that you added.

    Here's a small bash script that should do the thing:
    Code:
    #!/usr/bin/env bash
    
    needle="#--BeginCustom"
    newValue="IT WORKS!!!!"
    
    # -t option to remove trailing newline
    mapfile -t vhost < "test.txt"
    
    for i in "${!vhost[@]}"; do
        # Check if array value matches the needle
        if [[ "${vhost[${i}]}" = "${needle}" ]]; then
            # The custom code starts at the line after the match
            j=$((i+1))
            # Set new custom vaue
            vhost[${j}]="${newValue}"
        fi
    done
    
    printf "%s\n" "${vhost[@]}"
    
    Here's the testfile
    Code:
    <Directory /var/www/domain.tld>
    AllowOverride None
    Require all denied
    </Directory>
    
    <VirtualHost *:80>
    DocumentRoot /var/domain.tld/web
    
    
    <If "%{HTTPS} == 'off'">
    Redirect permanent / https://www.domain.tld/
    </If>
    <If "%{HTTPS} == 'on'">
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    </If>
    #--BeginCustom
    this is a default entry that needs to be replaced
    #--EndCustom
    
    
    </VirtualHost>
    
    And here's the result
    Code:
    ./test.sh
    <Directory /var/www/domain.tld>
    AllowOverride None
    Require all denied
    </Directory>
    
    <VirtualHost *:80>
    DocumentRoot /var/domain.tld/web
    
    
    <If "%{HTTPS} == 'off'">
    Redirect permanent / https://www.domain.tld/
    </If>
    <If "%{HTTPS} == 'on'">
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    </If>
    #--BeginCustom
    IT WORKS!!!!
    #--EndCustom
    
    
    </VirtualHost>
    
     
  5. sjau

    sjau Local Meanie Moderator

    And here an untested update. Basically having added a loop to go through the vhost files and then store them back.

    Code:
    #!/usr/bin/env bash
    
    # Run this script as root and provide new info as parameter, e.g.  ./updateVhosts.sh "xxx x xx x x"
    
    needle="#--BeginCustom"
    newValue="${1}"
    
    # Loop through all the vhost files in /etc/apache2/sites-available
    for f in "/etc/apache/sites-available/"*".vhost"; do
        # map the vhost file to an array; use -t option to remove trailing newline
        mapfile -t vhost < "${f}"
    
        # Loop through each array entry
        for i in "${!vhost[@]}"; do
            # Check if array value matches the needle
            if [[ "${vhost[${i}]}" = "${needle}" ]]; then
                # The custom code starts at the line after the match
                j=$((i+1))
                # Set new custom vaue
                vhost[${j}]="${newValue}"
            fi
        done
        # Replace existing vhost file with altered one
        printf "%s\n" "${vhost[@]}" > "${f}"
    done
    # Reload config
    systemctl reload apache2
    
     
  6. till

    till Super Moderator Staff Member ISPConfig Developer

    I would use Tools > Resync in ISPConfig interface for that.
     
    zyzzza likes this.
  7. sjau

    sjau Local Meanie Moderator

    d'oh :)

    Never had a need for that this far :)
     
  8. zyzzza

    zyzzza New Member

    Oh, thats worked :) Thanks for showing an easy way :)
     

Share This Page