Dear Wiki user, You have subscribed to a wiki page or wiki category on "Httpd Wiki" for change notification.
The following page has been changed by sjorge: http://wiki.apache.org/httpd/ApacheVirtualHostMysql/hosts%2econf The comment on the change is: ftp support ------------------------------------------------------------------------------ # Host Settings # ############################ <IfModule !mod_perl.c> - # lets trow and error + # lets trow and error - please enable mod_perl! + please enable mod_perl! </IfModule> <IfModule mod_perl.c> - # Required Modules + # Required Modules - <IfDefine DAV> + <IfDefine DAV> - LoadModule dav_module modules/mod_dav.so + LoadModule dav_module modules/mod_dav.so - LoadModule dav_fs_module modules/mod_dav_fs.so + LoadModule dav_fs_module modules/mod_dav_fs.so - DAVLockDB logs/davlock + DAVLockDB logs/davlock - </IfDefine> + </IfDefine> + <IfDefine FTP> + LoadModule ftp_module modules/mod_ftp.so + FTPLimitDBFile logs/ftpd.db + FTPOptions CheckMaxClients ShowUnAuthorizedFiles + FTPLimitLoginUser 10 + LogFormat "%{%b %e %H:%M:%S %Y}t %T %a %B %U %M %F %d %W %u %S %Z %Y" ftp_log + </IfDefine> + - # Listening Sockets + # Listening Sockets - Listen 80 + Listen 80 - NameVirtualHost *:80 + NameVirtualHost *:80 - <IfDefine DAV> + <IfDefine DAV> - Listen 81 - NameVirtualHost *:81 - </IfDefine> + Listen 81 + NameVirtualHost *:81 + </IfDefine> + <IfDefine FTP> + Listen 21 ftp + AcceptFilter ftp none + NameVirtualHost *:21 + </IfDefine> - <Perl> - # database configuration - my %dbcfg = ( - server => 'localhost', - database => 'httpd', - user => 'apache', - pass => 'password' - ); - my %host = ( - http_tmpl => '/srv/httpd/conf/templates/http.tmpl', - dav_tmpl => '/srv/httpd/conf/templates/webdav.tmpl', - path => '/srv/hosts' - ); - # modules - use strict; - use warnings; - use DBI; # DBI + DBD MySQL Driver + <Perl> + # database configuration + my %dbcfg = ( + server => 'localhost', + database => 'httpd', + user => 'apache', + pass => 'wRu3REfr' + ); + my %host = ( + http_tmpl => '/srv/httpd/conf/templates/http.tmpl', + dav_tmpl => '/srv/httpd/conf/templates/webdav.tmpl', + ftp_tmpl => '/srv/httpd/conf/templates/ftp.tmpl', + path => '/srv/hosts' + ); + + # modules + use strict; + use warnings; + use DBI; # DBI + DBD MySQL Driver - use Apache2::PerlSections; # Apache2::PerlSection is needed for add_config + use Apache2::PerlSections; # Apache2::PerlSection is needed for add_config - # read templates + # read templates - open(TMPL, $host{'http_tmpl'}) or die "Can't read template"; + open(TMPL, $host{'http_tmpl'}) or die "Can't read http template"; - $host{'http_tmpl'} = ''; - while (<TMPL>){ - chomp; + $host{'http_tmpl'} = ''; + while (<TMPL>){ + chomp; - $host{'http_tmpl'} = $host{'http_tmpl'} . $_ . "\n"; + $host{'http_tmpl'} = $host{'http_tmpl'} . $_ . "\n"; - } - close(TMPL); + } + close(TMPL); - open(TMPL, $host{'dav_tmpl'}) or die "Can't read template"; + open(TMPL, $host{'dav_tmpl'}) or die "Can't read dav template"; - $host{'dav_tmpl'} = ''; - while (<TMPL>){ - chomp; + $host{'dav_tmpl'} = ''; + while (<TMPL>){ + chomp; - $host{'dav_tmpl'} = $host{'dav_tmpl'} . $_ . "\n"; + $host{'dav_tmpl'} = $host{'dav_tmpl'} . $_ . "\n"; - } - close(TMPL); + } + close(TMPL); - # apache server hook + open(TMPL, $host{'ftp_tmpl'}) or die "Can't read ftp template"; + $host{'ftp_tmpl'} = ''; + while (<TMPL>){ + chomp; + $host{'ftp_tmpl'} = $host{'ftp_tmpl'} . $_ . "\n"; + } + close(TMPL); + + # apache server hook - my $srv = Apache2::PerlSections->server(); + my $srv = Apache2::PerlSections->server(); - - # database connection - my $dbh = DBI->connect( + + # database connection + my $dbh = DBI->connect( - 'DBI:mysql:'.$dbcfg{'database'}.':'.$dbcfg{'server'}, + 'DBI:mysql:'.$dbcfg{'database'}.':'.$dbcfg{'server'}, - $dbcfg{'user'}, - $dbcfg{'pass'} - ); + $dbcfg{'user'}, + $dbcfg{'pass'} + ); - if(not $dbh){ + if(not $dbh){ - print "Can't connect to mysql server!\n"; + print "Can't connect to mysql server!\n"; - die $DBI::errstr; + die $DBI::errstr; - } - - # fetch hosts + } + + # fetch hosts - my $hosts = $dbh->prepare(q{ + my $hosts = $dbh->prepare(q{ - SELECT hosts.id, name, IF( ISNULL( configuration), '', configuration) configuration, webdav, cgi, ssi, php + SELECT hosts.id, name, IF( ISNULL( configuration), '', configuration) configuration, webdav, ftp, cgi, ssi, php - FROM hosts + FROM hosts - LEFT JOIN configuration ON (configuration.id = hosts.id) + LEFT JOIN configuration ON (configuration.id = hosts.id) - WHERE enabled = 1 - ORDER BY hosts.id ASC; - }) or die $dbh->errstr; + WHERE enabled = 1 + ORDER BY hosts.id ASC; + }) or die $dbh->errstr; - # generate vhosts - $hosts->execute; + # generate vhosts + $hosts->execute; - while ( (my $id,my $name,my $cfg,my $bDAV,my $bCGI,my $bSSI,my $bPHP) = $hosts->fetchrow_array() ) { + while ( (my $id,my $name,my $cfg,my $bDAV,my $bFTP,my $bCGI,my $bSSI,my $bPHP) = $hosts->fetchrow_array() ) { - # generate aditional configuration - if ($bSSI == 1) { - my $ssi = ''; - $ssi = $ssi . "<IfModule mod_include.c>\n"; + # generate aditional configuration + if ($bSSI == 1) { + my $ssi = ''; + $ssi = $ssi . "\t<IfModule mod_include.c>\n"; - $ssi = $ssi . "\tAddType text/html .shtml .shtm\n"; + $ssi = $ssi . "\t\tAddType text/html .shtml .shtm\n"; - $ssi = $ssi . "\tAddOutputFilter INCLUDES .shtml .shtm\n"; + $ssi = $ssi . "\t\tAddOutputFilter INCLUDES .shtml .shtm\n"; - $ssi = $ssi . "</IfModule>\n"; + $ssi = $ssi . "\t</IfModule>\n"; - $cfg = $ssi . $cfg; - } + $cfg = $ssi . $cfg; + } - if ($bCGI == 1) { - my $cgi = ''; + if ($bCGI == 1) { + my $cgi = ''; - $cgi = $cgi . "ScriptAlias /cgi-bin/ \"%host_dir%/%name%/cgi-bin/\"\n"; + $cgi = $cgi . "\tScriptAlias /cgi-bin/ \"%host_dir%/%name%/cgi-bin/\"\n"; - $cgi = $cgi . "<Directory \"%host_dir%/%name%/cgi-bin\">\n"; + $cgi = $cgi . "\t<Directory \"%host_dir%/%name%/cgi-bin\">\n"; - $cgi = $cgi . "\tAllowOverride None\n"; + $cgi = $cgi . "\t\tAllowOverride None\n"; - $cgi = $cgi . "\tOptions None\n"; + $cgi = $cgi . "\t\tOptions None\n"; - $cgi = $cgi . "\tOrder allow,deny\n"; + $cgi = $cgi . "\t\tOrder allow,deny\n"; - $cgi = $cgi . "\tAllow from all\n"; + $cgi = $cgi . "\t\tAllow from all\n"; - $cgi = $cgi . "</Directory>\n"; + $cgi = $cgi . "\t</Directory>\n"; + $cgi = $cgi . "\t<IfModule mod_cgi.c>\n"; + $cgi = $cgi . "\t\tAddHandler cgi-script .cgi .pl\n"; + $cgi = $cgi . "\t</IfModule>\n"; - $cgi = $cgi . "<IfModule mod_cgi.c>\n"; + $cgi = $cgi . "\t<IfModule mod_cgid.c>\n"; - $cgi = $cgi . "\tAddHandler cgi-script .cgi .pl\n"; + $cgi = $cgi . "\t\tAddHandler cgi-script .cgi .pl\n"; - $cgi = $cgi . "</IfModule>\n"; + $cgi = $cgi . "\t</IfModule>\n"; + $cfg = $cgi . $cfg; + } - $cgi = $cgi . "<IfModule mod_cgid.c>\n"; - $cgi = $cgi . "\tAddHandler cgi-script .cgi .pl\n"; - $cgi = $cgi . "</IfModule>\n"; - $cfg = $cgi . $cfg; - } - if ($bPHP == 1) { - my $php = ''; - $php = $php . "<IfModule mod_php5.c>\n"; + if ($bPHP == 1) { + my $php = ''; + $php = $php . "\t<IfModule mod_php5.c>\n"; - $php = $php . "\tAddHandler application/x-httpd-php .php\n"; + $php = $php . "\t\tAddHandler application/x-httpd-php .php\n"; - $php = $php . "\tAddHandler application/x-httpd-php-source .phps\n"; + $php = $php . "\t\tAddHandler application/x-httpd-php-source .phps\n"; - $php = $php . "</IfModule>\n"; + $php = $php . "\t</IfModule>\n"; - $cfg = $php . $cfg; - } - - # get aliases - my $aliases = ''; - my $alias = $dbh->prepare(q{ - SELECT alias - FROM aliases - WHERE id = ?; + $cfg = $php . $cfg; + } + + # get aliases + my $aliases = ''; + my $alias = $dbh->prepare(q{ + SELECT alias + FROM aliases + WHERE id = ?; - }) or die $dbh->errstr; + }) or die $dbh->errstr; - $alias->execute($id); - + $alias->execute($id); + - while ( (my $n) = $alias->fetchrow_array() ) { + while ( (my $n) = $alias->fetchrow_array() ) { - $aliases = $aliases . " " . $n; - } - - if ($aliases ne '') { - $aliases = "ServerAlias" . $aliases; - } - - # validate documentroot - if(!-d "$host{'path'}/$name"){ + $aliases = $aliases . " " . $n; + } + + if ($aliases ne '') { + $aliases = "ServerAlias" . $aliases; + } + + # validate documentroot + if(!-d "$host{'path'}/$name"){ - mkdir("$host{'path'}/$name", 0755); + mkdir("$host{'path'}/$name", 0755); - mkdir("$host{'path'}/$name/_sys", 0755); + mkdir("$host{'path'}/$name/_sys", 0755); - mkdir("$host{'path'}/$name/_sys/logs", 0755); + mkdir("$host{'path'}/$name/_sys/logs", 0755); - mkdir("$host{'path'}/$name/_sys/tmp", 0755); #for php temp directory + mkdir("$host{'path'}/$name/_sys/tmp", 0755); #for php temp directory - mkdir("$host{'path'}/$name/_sys/sessions", 0755); #for php sessions directory + mkdir("$host{'path'}/$name/_sys/sessions", 0755); #for php sessions directory - mkdir("$host{'path'}/$name/httpdocs", 0755); + mkdir("$host{'path'}/$name/httpdocs", 0755); - mkdir("$host{'path'}/$name/cgi-bin", 0755); + mkdir("$host{'path'}/$name/cgi-bin", 0755); - system('chown -R apache:apache "'.$host{'path'}.'/'.$name.'"'); + system('chown -R apache:apache "'.$host{'path'}.'/'.$name.'"'); - } + } - # create vhost - my $vhost = $host{'http_tmpl'}; - if ($bDAV == 1) { - $vhost = $vhost . "\n" . $host{'dav_tmpl'} - } - $vhost =~ s/%id%/$id/g; - $vhost =~ s/%cfg%/$cfg/g; - $vhost =~ s/%host_dir%/$host{'path'}/g; - $vhost =~ s/%name%/$name/g; - $vhost =~ s/%aliases%/$aliases/g; + # create vhost + my $vhost = $host{'http_tmpl'}; + if ($bDAV == 1) { + $vhost = $vhost . "\n" . $host{'dav_tmpl'}; + } + if ($bFTP == 1) { + $vhost = $vhost . "\n" . $host{'ftp_tmpl'}; + } + $vhost =~ s/%id%/$id/g; + $vhost =~ s/%cfg%/$cfg/g; + $vhost =~ s/%host_dir%/$host{'path'}/g; + $vhost =~ s/%name%/$name/g; + $vhost =~ s/%aliases%/$aliases/g; - $vhost =~ s/%db_server%/$dbcfg{'server'}/g; - $vhost =~ s/%db_name%/$dbcfg{'database'}/g; - $vhost =~ s/%db_user%/$dbcfg{'user'}/g; - $vhost =~ s/%db_pass%/$dbcfg{'pass'}/g; - - # push vhosts to apache - $srv->add_config([split /\n/, $vhost]); + $vhost =~ s/%db_server%/$dbcfg{'server'}/g; + $vhost =~ s/%db_name%/$dbcfg{'database'}/g; + $vhost =~ s/%db_user%/$dbcfg{'user'}/g; + $vhost =~ s/%db_pass%/$dbcfg{'pass'}/g; + + # push vhosts to apache + $srv->add_config([split /\n/, $vhost]); + + # debugging + #print "----" . $name . "----\n"; + #print $vhost; + } + # cleanup + $dbh->disconnect(); - # debugging - #print "----" . $name . "----"; - #print $vhost; - - } - - # cleanup - $dbh->disconnect(); </Perl> </IfModule> + }}}
