use DBI;
use Data::Dumper;
my $dbtype = "mysql";
my $dbhost = "XXXXXXXX.XXX.XX";
my $dbname = "XXXXXXXX";
my $dbuser = "XXXXXXXX";
my $dbpass = "XXXXXXXX";
my %users = ();
my %groups = ();
while ( my $user = getpwent() ) { $users{$user} = 1; }
while ( my $group = getgrent() ) { $groups{$group} = 1; }
my $self = Apache2::ServerUtil->server;
my $flags = { PrintError => 1 };
my $dbh = DBI->connect("dbi:$dbtype:$dbname:$dbhost", $dbuser, $dbpass, $flags);
if (defined $dbh) {
# Load configuration from MySQL table
my $sth = $dbh->prepare("
SELECT * FROM virtualhosts ORDER BY ServerName
");
$sth->execute;
while (my $record = $sth->fetchrow_hashref) {
my $url = $record->{'ServerName'};
my $docroot = $record->{'DocumentRoot'};
my $cacheroot = "/var/www/mason/$url";
my $uid = $record->{'Username'};
my $gid = $record->{'Groupname'};
# Verify DocumentRoot exists
unless ( -d $docroot ) {
warn "Skipped $record->{'ServerName'}: DocumentRoot '$docroot' invalid\n";
next;
}
# Verify Username exists
unless ( $users{$uid} ) {
warn "Skipped $record->{'ServerName'}: Username '$uid' invalid\n";
next;
}
# Verify Groupname exists
unless ( $groups{$gid} ) {
warn "Skipped $record->{'ServerName'}: Groupname '$gid' invalid\n";
next;
}
push @{$VirtualHost{'*:80'}}, {
ServerName => $url,
ServerAdmin => $record->{'ServerAdmin'},
DocumentRoot => $docroot,
SuexecUserGroup => "$uid $gid",
};
}
open(my $fh, '>', '/var/log/httpd/debug.log');
print $fh Dumper(\%VirtualHost);
close $fh;
} else {
die "MySQL error: ".$DBI::errstr;
}
####
$VAR1 = {
'*:80' => [
{
'ServerName' => 'vsite1.XXX.XX',
'SuexecUserGroup' => 'XXXXXXXX XXXXXXXX',
'ServerAdmin' => 'XXXXXXXX@XXX.XX',
'DocumentRoot' => '/home/XXXXXXXX/public_html'
},
{
'ServerName' => 'vsite2.XXX.XX',
'SuexecUserGroup' => 'XXXXXXXX XXXXXXXX',
'ServerAdmin' => 'XXXXXXXX@XXX.XX',
'DocumentRoot' => '/home/XXXXXXXX/public_html'
},
{
'ServerName' => 'vsite3.XXX.XX',
'SuexecUserGroup' => 'XXXXXXXX XXXXXXXX',
'ServerAdmin' => 'XXXXXXXX@XXX.XX',
'DocumentRoot' => '/home/XXXXXXXX/public_html'
}
]
};
####
$ httpd -S
[Mon Dec 01 18:11:12.970705 2014] [so:warn] [pid 19208] AH01574: module apreq_module is already loaded, skipping
[Mon Dec 01 18:11:12.971179 2014] [so:warn] [pid 19208] AH01574: module perl_module is already loaded, skipping
VirtualHost configuration:
*:80 is a NameVirtualHost
default server vsite1.XXX.XX (mod_perl:1)
port 80 namevhost vsite1.XXX.XX (mod_perl:1)
port 80 namevhost vsite1.XXX.XX (mod_perl:1)
port 80 namevhost vsite2.XXX.XX (mod_perl:7)
port 80 namevhost vsite2.XXX.XX (mod_perl:7)
port 80 namevhost vsite3.XXX.XX (mod_perl:13)
port 80 namevhost vsite3.XXX.XX (mod_perl:13)
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: MODPERL2
User: name="apache" id=48
Group: name="apache" id=48
####
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
Linux XXXX.XXX.XX 3.10.0-123.9.3.el7.x86_64 #1 SMP Thu Nov 6 15:06:03 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
httpd-2.4.6-18.el7.centos.x86_64
mod_perl-2.0.8-10.20140624svn1602105.el7.x86_64