Load Balancing (Linux Virtual Server)

The Panel provides a ready-to-use set of templates which allows generate such Apache configuration that makes a Panel server run with Linux Virtual Server. To utilize the solution, use the templates from the templates/load_balancing directory: copy or move them to templates/custom and generate new configuration.

The solution implies that Apache accepts any IP for load-balanced name-based hosts. Generally speaking, the configuration templates feature the following:

The rest of the section explains the changes made to configuration templates in detail.

1. Removing IP addresses from NameVirtualHost

In the template server/nameVirtualHost.php, the default content

<?php foreach ($VAR->server->ipAddresses->all as $ipaddress): ?>
NameVirtualHost <?php echo $ipaddress->address ?>:<?php
    echo ($OPT['ssl']
        ? $VAR->server->webserver->httpsPort
        : $VAR->server->webserver->httpPort) . "\n" ?>
<?php endforeach; ?>

is changed to

NameVirtualHost *:<?php
    echo ($OPT['ssl']
        ? $VAR->server->webserver->httpsPort
        : $VAR->server->webserver->httpPort) . "\n" ?>

 

2. Removing IP addresses from VirtualHost

Definitions of the VirtualHost directive in all templates are changed.

For example, in the template domain/domainVirtualHost.php, the default content

<VirtualHost <?php echo $VAR->domain->physicalHosting->ipAddress->address ?>:<?php echo $OPT['ssl'] ? $VAR->server->webserver->httpsPort : $VAR->server->webserver->httpPort ?>>

is changed to

<VirtualHost *:<?php echo $OPT['ssl'] ? $VAR->server->webserver->httpsPort : $VAR->server->webserver->httpPort ?>>

 

3. Making the server default SSL certificate be used for all SSL-enabled virtual hosts

Definitions of SSL certificates set up on IP addresses are changed in all templates.

For example, in the template domain/domainVirtualHost.php, the default content

<?php if ($OPT['ssl']): ?>
<?php if ($VAR->domain->physicalHosting->ipAddress->sslCertificate->ce): ?>
    SSLEngine on
    SSLVerifyClient none
    SSLCertificateFile <?php echo $VAR->domain->physicalHosting->ipAddress->sslCertificate->ceFilePath ?>
<?php if ($VAR->domain->physicalHosting->ipAddress->sslCertificate->ca): ?>
    SSLCACertificateFile <?php echo $VAR->domain->physicalHosting->ipAddress->sslCertificate->caFilePath ?>
<?php endif; ?>
<?php endif; ?>
<?php else: ?>
    <IfModule mod_ssl.c>
        SSLEngine off
    </IfModule>
<?php endif; ?>

is changed to

<?php if ($OPT['ssl']): ?>
<?php if ($VAR->server->defaultSslCertificate->ce): ?>
    SSLEngine on
    SSLVerifyClient none
    SSLCertificateFile "<?php echo $VAR->server->defaultSslCertificate->ceFilePath ?>"
<?php if ($VAR->server->defaultSslCertificate->ca): ?>
    SSLCACertificateFile "<?php echo $VAR->server->defaultSslCertificate->caFilePath ?>"
<?php endif; ?>
<?php endif; ?>
<?php else: ?>
    <IfModule mod_ssl.c>
        SSLEngine off
    </IfModule>
<?php endif; ?>

 

4. Restoring the default website functionality

4.1. Making the server default virtual host open when any IP registered on the server is addressed

In the template server/vhosts.php, the default content

<?php for($ipAddresses = $VAR->server->ipAddresses->all, $ipAddress = reset($ipAddresses); $ipAddress; $ipAddress = next($ipAddresses)): ?>
    <VirtualHost \
        <?php echo $ipAddress->address ?>:<?php echo $OPT['ssl'] ? $VAR->server->webserver->httpsPort : $VAR->server->webserver->httpPort ?> \
<?php for ($n = 1; $n < $OPT['ipLimit'] && $ipAddress = next($ipAddresses); $n++): ?>
        <?php echo $ipAddress->address ?>:<?php echo $OPT['ssl'] ? $VAR->server->webserver->httpsPort : $VAR->server->webserver->httpPort ?> \
<?php endfor; ?>
    >
        ServerName "default<?php echo 1 == $OPT['ipLimit'] ? '-' . str_replace('.', '_', $ipAddress->address) : '' ?>"

//////////////////////////////////////
// Unchanged part of code is skipped//
//////////////////////////////////////

    </VirtualHost>
<?php endfor; ?>

is changed to

ServerName "default"
    <VirtualHost *:<?php echo $OPT['ssl'] ? $VAR->server->webserver->httpsPort : $VAR->server->webserver->httpPort ?>>
        ServerName "default"

//////////////////////////////////////
// Unchanged part of code is skipped//
//////////////////////////////////////

    </VirtualHost>

4.2. Moving definition of server default virtual host to the end of VirtualHost definition

In the template server.php, the following piece of code is moved to the very end of the template:

<?php echo $VAR->includeTemplate('server/vhosts.php', array(
    'ssl' => false,
    'ipLimit' => $VAR->server->webserver->apache->vhostIpCapacity,
)) ?>
<?php echo $VAR->includeTemplate('server/vhosts.php', array(
    'ssl' => true,
    'ipLimit' => 1,
)) ?>

Please send us your feedback on this help page.