Multiple Apache httpd configurations - One Installation

Description

This is an extension to the http daemon from apache.org.
The goal was to advance apache's http daemon that we can start it with multiple configurations on one single server with many ip addresses! We use the http daemon generally for loadbalancing Tomcat/IIS/... applications + SSL offloading.
On our former installations we had to compile the http daemon for every instance we wanted to run on the server. Now we can compile/install one apache http daemon and use this single instance with multiple configurations. (ip's/ssl-certificates/...)
The other goal was that it had to be easy to change the http daemon version to a newer one which is now pretty charming!  :)

Compiling / installing & configuring Apache's HTTPD + Modules

Installing & configuring the httpd

Create apache2 folder in /usr/local
Downloading the source to root/software
eventually set proxy environment variable
export http_proxy=http://IP:Port
wget http://mirror.deri.at/apache/httpd/httpd-2.2.XX.tar.gz
Patching the sources
If your installation is not that big (# of access) you can skip the following step.
Set the compiler flag via the enironment varible:
export CFLAGS=-DDEFAULT_THREAD_LIMIT=256
Installing the needed libraries
apt-get install zlib1g-dev libssl-dev build-essentials
Extracting the downloaded source
tar -zxvf httpd-2.2.XX.tar.gz
Building & installing the package
After patching the sources (if necessary for you) it's time to configure / compile & install apache's http daemon.
make clean
Change to the extracted httpd-2.2.XX directory.
./configure --enable-mods-shared=all --enable-modules=all --enable-proxy --enable-proxy-connect --enable-proxy-ftp \
 --enable-proxy-http --enable-proxy-balancer --enable-ssl=shared --with-mpm=worker --prefix=/usr/local/apache2/2.2.XX \
 --exec-prefix=/usr/local/apache2/2.2.XX

make
make install
Creating symlink
Creating the "core"-link (symlink) in the /usr/local/apache2/ folder to 2.2.XX
ln –s 2.2.XX core

Installing & configuring mod_jk (Tomcat Connector)

Downloading the source to root/software
wget http://mirror.deri.at/apache/tomcat/tomcat-connectors/jk/source/jk-1.2.XX/tomcat-connectors-1.2.XX-src.tar.gz
Extracting the downloaded source
tar -zxvf tomcat-connectors-1.2.XX.tar.gz
Building & installing the package
cd tomcat-connectors-1.2.XX-src/native
make clean
Change to the "native"-folder
./configure --with-apxs=/usr/local/apache2/core/bin/apxs
make
make install

Installing & configuring mod_perl

This module is needed for mod_macro!
Downloading the source to root/software
wget http://perl.apache.org/dist/mod_perl-2.0-current.tar.gz
Installing the needed libperl library
apt-get install libperl-dev
Extracting the downloaded source
tar -zxvf mod_perl-2.0-current.tar.gz
Building & installing the package
cd mod_perl-2.0.X

perl Makefile.PL MP_APXS=/usr/local/apache2/core/bin/apxs

make
The module was build under ./src/modules/perl/mod_perl.so in the build directory.
cp ./src/modules/perl/mod_perl.so /usr/local/apache2/core/modules/

Installing & configuring mod_macro

mod_macro is a great configuration enhancement for apache's http daemon. We use it to make our configurations easier to read & change!
Downloading the source to root/software
wget http://www.coelho.net/mod_macro/mod_macro-latest.tar.gz
Extracting the downloaded source
The source has to be extracted to the source directory of the apache!
tar –zxvf mod_macro-1.1.XX
cp -Rp mod_macro-1.1.XX httpd-2.2.XX
Building & installing the package
cd /root/software/httpd-2.2.XX

/usr/local/apache2/core/bin/apxs -c mod_macro-1.1.XX/mod_macro.c
Installing the build module
cp ./mod_macro-1.1.11/.libs/mod_macro.so /usr/local/apache2/core/modules/

Template extension

Includes manage_apache.pl + search_replace.pl + template. The manage_apache.pl & search_replace.pl is available as download in the specific sections. Thx to snipt.org where most of our scripts are hosted!  :)

Description

Description of "manage_apache.pl" parameters
manage_apache.pl [ options ] <commands...>

      -help|?         print help
      -ip|ipaddress   IP address
      -port           Port
      -folder|path    Folder name
      -delete|remove  Folder name
      -apache         init-script prefix
      -force          Force installation
manage_apache.pl -ip IPADDRESS -port PORT -folder FOLDERNAME -apache APACHEFOLDER -delete FOLDERNAME -force yes/y
Description of "search_replace.pl" parameters
int replace (InputFile, OutputFile, search-String, replace-String);
1.) Input file will be copied to /tmp/$file. $file will be extracted from input file (ex: inputfile: /usr/local/apache2/folderXY/conf/httpd.conf -> $file = httpd.conf)
2.) Search string will be searched and written to the output file
3.) /tmp/$file will be deleted
4.) Returned value = how often the search string was found (& replaced)
Description of "manage_apache.pl" script with "search_replace.pl" function
1.) The whole template folder will be copied to $prefix/$folder (ex: /usr/local/apache2/folderXY)
2.) The following values will be replaced in httpd.conf (IP, Port, SSLPort, Folder, Prefix)
3.) The httpd start/stop script will be renamed from "template" to $folder (in /bin)
4.) The value XXConfig will be replaced by $folder
5.) Init-Script for the new httpd will be linked (ln -s /usr/local/apache2/$folder/bin/$folder /etc/init.d/apache2_$folder)
6.) Create runlevel entries (update-rc.d apache2_$folder defaults)
Modified httpd.conf in our template
1.) The mod_jk-module was linked
LoadModule jk_module modules/mod_jk.so
2.) Some more configurations
#KEEPALIVE TIMEOUT
MaxKeepAliveRequests 100
KeepAliveTimeout 5

#MPM CONFIGURATIONS

<IfDefine !HIGHMEM>
<IfModule worker.c>
        ServerLimit 25
        StartServers 10
        MaxClients 1600
        ThreadLimit 64
        MinSpareThreads 25
        MaxSpareThreads 256
        ThreadsPerChild 64
        MaxRequestsPerChild 0
</IfModule>
</IfDefine>

<IfDefine HIGHMEM>
<IfModule worker.c>
        ServerLimit 25
        StartServers 10
        MaxClients 3200
        ThreadLimit 128
        MinSpareThreads 25
        MaxSpareThreads 256
        ThreadsPerChild 128
        MaxRequestsPerChild 0
</IfModule>
</IfDefine>

Options -Indexes

#LOAD CONFIGURATIONS
Include conf/sites-enabled/*
3.) Folders where created/linked under /conf ("sites-enabled" & "sites-available")
4.) The mod_perl-module was linked
LoadModule perl_module modules/mod_perl.so
<IfModule !mod_perl.c>
Error Hey, you forgot to install mod_perl! I need it!
</IfModule>
5.) The mod_macro-module was linked
LoadModule macro_module modules/mod_macro.so

manage_apache.pl

manage_apache.pl
perl
  1. #!/usr/bin/perl
  2. require 'search_replace.pl';
  3. my $hasArguments=1;
  4. if (defined $ARGV[0]) { $hasArguments=0; }
  5. use Getopt::Long;
  6. use Pod::Usage;
  7. use File::Copy;
  8. use Cwd 'abs_path';
  9. use File::Path qw(remove_tree);
  10. my $ipaddress = "";
  11. my $port = "80";
  12. my $sslport = "443";
  13. my $folder = "";
  14. my $help = 0;
  15. my $delete = 0;
  16. my $apache = "apache2";
  17. my $prefix = "/usr/local/apache2";
  18. my $nprefix = "";
  19. my $tempprefix = "/usr/local/apache2/mod";
  20. my $init = "/etc/init.d";
  21. my $force = "";
  22. my $i=0;
  23. $result = GetOptions ("help|?" => \$help,
  24. "ip|ipaddress=s" => \$ipaddress,
  25. "port=i" => \$port,
  26. "sslport=i" => \$sslport,
  27. "folder|path=s" => \$folder,
  28. "prefix=s" => \$prefix,
  29. "delete|remove=s" => \$delete,
  30. "apache=s" => \$apache,
  31. "force=s" => \$force);
  32. my $outf="$prefix/$folder/conf/httpd.conf";
  33. my $logrotatef="$prefix/$folder/logrotate/apache2_logrotate";
  34. if($help or $hasArguments){
  35. pod2usage ('-exitval' => 0, -verbose => 1);
  36. }
  37. if($delete) {
  38. print "\nDo you really want to delete the http-server $prefix/$delete? (yes/no): ";
  39. my $input = <>;
  40. chomp($input);
  41. if($input eq "yes") {
  42. my $init_link = $init."/".$apache."_".$delete;
  43. print "removing: $prefix/$delete\n";
  44. remove_tree("$prefix/$delete",{result => \my $list});
  45. print "removed $_\n" for @$list;
  46. if(unlink ($init_link)==1){
  47. print "File $init_link deleted successfully\n";
  48. } else {
  49. print "Error: File $init_link not deleted\n";
  50. }
  51. my $logrotate_link = "/etc/logrotate.d/apache2_".$delete;
  52. if(unlink ($logrotate_link)==1){
  53. print "File $logrotate_link deleted successfully\n";
  54. } else {
  55. print "Error: File $logrotate_link not deleted\n";
  56. }
  57. $console_out = qx ( update-rc.d $init_link remove );
  58. print $console_out;
  59. }
  60. exit(0);
  61. }
  62. if(-d $prefix.$folder) {
  63. die "Folder already exists!\n";
  64. }
  65. if(!($force eq "yes") and !($force eq "y")) {
  66. print "\nIP:\t\t$ipaddress\nPort:\t\t$port\nSSL-Port:\t$sslport\nPrefix:\t\t$prefix\nFolder:\t\t$folder\nInit-Prefix:\t$apache\n";
  67. print "\nDo you really want to create the following http-server? (yes/no): ";
  68. my $input = <>;
  69. chomp($input);
  70. if($input eq "no") {
  71. print "\nThe http-server won't be installed!\n";
  72. exit(0);
  73. }
  74. }
  75. my $console_out = "";
  76. # first of all copy template directory to new httpd-configuration directory
  77. $console_out = qx ( cp -Rv $tempprefix/template $prefix/$folder );
  78. print $console_out;
  79. # search and replace configuration parameters in httpd.conf
  80. my @Werte= ($ipaddress,$port,$sslport,$folder,$prefix);
  81. my @XX = ("XXIPADDRESS","XXPORT","XXSSLPORT","XXPATH","XXPREFIX");
  82. foreach (@Werte){
  83. my $returnout = replace ("$outf", "$outf", "$XX[$i]", "$Werte[$i]");
  84. if ($returnout > 0) {
  85. print "$XX[$i]: $returnout times replaced!\n";
  86. } else {
  87. print "$XX[$i]: $returnout - not found!\n";
  88. }
  89. $i++;
  90. }
  91. # search and replace path in logrotate file
  92. $console_out = replace ("$logrotatef", "$logrotatef", "XXPATH", "$folder");
  93. if ($console_out > 0) {
  94. print "XXPATH: $console_out times replaced!\n";
  95. } else {
  96. print "XXPATH: $console_out - not found!\n";
  97. }
  98. # create link to logrotate file
  99. symlink("$logrotatef","/etc/logrotate.d/".$apache."_".$folder);
  100. # rename http binary
  101. $console_out = qx ( mv $prefix/$folder/bin/template $prefix/$folder/bin/$folder );
  102. # search and replace in httpd start script
  103. $console_out = replace ("$prefix/$folder/bin/$folder","$prefix/$folder/bin/$folder","XXCONFIG",$folder);
  104. if ($console_out > 0) {
  105. print "XXCONFIG: $console_out times replaced!\n\n";
  106. } else {
  107. print "XXCONFIG: $console_out - not found!\n\n";
  108. }
  109. # create a link for the init-script
  110. symlink("$prefix/$folder/bin/$folder","$init/".$apache."_$folder");
  111. # add the init-script to the runlevels for auto-start
  112. my $update = "update-rc.d ".$apache."_".$folder." defaults";
  113. $console_out = qx ( $update );
  114. print $console_out;
  115. __END__
  116. =head1 NAME
  117. manage_apache.pl
  118. =head1 SYNOPSIS
  119. manage_apache.pl [ options ] <commands...>
  120.   -help|? print help
  121.   -ip|ipaddress IP address
  122.   -port Port (default: 80)
  123.   -sslport SSL-Port (default: 443)
  124.   -folder|path Folder name
  125.   -prefix Prefix folder
  126.   -delete|remove Folder name
  127.   -apache Init-Script Prefix (default: apache2)
  128.   -force Force installation
  129. manage_apache.pl -ip IPADDRESS -port PORT -folder FOLDERNAME -apache APACHEFOLDER -prefix PREFIXFOLDER -delete FOLDERNAME -force YES

search_replace.pl

search_replace.pl
perl
  1. sub replace{
  2. my $inputf = shift;
  3. my $outputf = shift;
  4. my $search = shift;
  5. my $repl = shift;
  6. my $rev = reverse ($outputf);
  7. my $pos_slash = index($rev, "/",0);
  8. my $file = reverse( substr($rev,0,$pos_slash));
  9. my $console_out = "";
  10. my $tempf = "/tmp/$file";
  11. my $out=qx (cp $inputf $tempf);
  12. open INPUT, "<$tempf" or die "cannot open $tempf";
  13. open OUTPUT, ">$outputf" or die "error in $outputf";
  14. print "\nOutput: $outputf\n";
  15. print "input: $tempf\n";
  16. print "search: $search\n";
  17. print "repl: $repl\n";
  18. print "Substring: $file\n";
  19. my $returnval = 0;
  20. while(<INPUT>){
  21. my $orig = $_;
  22. $_ =~ s/$search/$repl/g;
  23. if($_ ne $orig) {
  24. $returnval++;
  25. }
  26. print OUTPUT $_;
  27. }
  28. close ($tempf);
  29. close ($outputf);
  30. if (unlink ($tempf) == 1){
  31. print "Tempfile $tempf deleted\n";
  32. }
  33. else{
  34. print "Tempfile $tempf not deleted\n";
  35. }
  36. return $returnval;
  37. }
  38. 1;

Comments

Popular posts from this blog

PHPMixBill V5 mikrotik Billing Solutions

How to install Asterisk and A2billing on Ubuntu Server 12.04LTS

odbcinst: SQLGetPrivateProfileString failed with