% latex generated by the script '/home/project-web/bumble/cgi-bin/text2latex.cgi' 
% with help from the sed script 'text2latex.sed' 
% query string: 'books/apache/apache-book' 
% sFileName= 'apache-book' 
% sDirName = 'books/apache' 
% sName = '' -->
% latex by http://bumble.sourceforge.net/scripts/text2latex.sed 
% this script is a work in progress 
\documentclass[11pt]{article} 
\usepackage[hmargin=2cm, vmargin=2cm]{geometry} 
\usepackage{url} 
\usepackage{hyperref} 
\begin{document}

 \\&\\& The Apache Web Server
 ------------------------:

 The apache web server is the most widely used webserver on the
 internet. It is a large program with a myriad of configuration options
 and other tools.

GETTING HELP

 The apache webserver has very detailed documentation which should
 (eventually) help you out of any tight spots.

 @@ \url{http://httpd.apache.org/docs/2.2/}
 official documentation for apache version 2.2

 * install documentation for apache2 on a debian style linux
 >> sudo apt-get install apache2-doc

 * show options which can be used with apache 
 >> man apache2
 >> man apache \\#\\#(the same, for an older version)

INSTALLATION

 * install apache version 2 on a debian-style linux
 >> sudo apt-get install apache2
 >> sudo aptitude install apache2 \\#\\#(the same)

 * test the installation from the webserver computer itself
 >> firefox 127.0.0.1
 >> firefox localhost

 * see what the status (running, stopped etc) of the webserver is
 >> sudo apache2ctl status

LAMP INSTALLATION ....

 * install Apache2 wth the MySQL database server and PHP on a 'debian' system
 >> sudo tasksel install lamp-server

 @@ \url{http://www.ubuntugeek.com/how-to-install-apache2-webserver-with-phpcgi-and-perl-support-in-ubuntu-server.html}
 how to install apache2 with php and cgi but not mysql

SYMBOLIC LINKS 

 * make apache serve documents referenced by unix-style symbolic links
 >> options followsymlinks

CGI CONFIGURATION

 * execute all scripts in the folder as cgi scripts 
 >> ScriptAlias \texttt{/cgi-bin/} \texttt{/usr/local/apache2/cgi-bin/} 

 * allow cgi execution of '.cgi' and '.pl' in a particular folder 
 ----------------------------------------------------------------
 <Directory \texttt{/usr/local/apache2/htdocs/somedir>}
 Options +ExecCGI
 AddHandler cgi-script .cgi .pl 
 </Directory> 
 ,,,

 * all cgi execution of all files in all user cgi-bin folders
 ------------------------------------------------------------
 <Directory \texttt{/home/*/public\\_html/cgi-bin>}
 Options ExecCGI
 SetHandler cgi-script
 </Directory> 
 ,,,

 * a perl script to show all environment variables
 -------------------------------------------------
 \\#!/usr/bin/perl
 print "Content-type: text/html\\$\\backslash\\$n\\$\\backslash\\$n";
 foreach \\$key (keys \\%ENV) \{
 print "\\$key  \\$ENV\{\\$key\}<br>";
 \} 
 ,,,

PERL CGI SUPPORT ....

 @@ \url{http://slashdot.org}
 an example of a site using the apache perl module with the 
 web server and perl scripts

 * install the perl-cgi module for apache
 >> sudo aptitude install libapache2-mod-perl2

 * create a cgi folder
 >> sudo mkdir \texttt{/home/www/cgi-bin}

 * add to the virtual host configuration
 ---------------------------------------
 ScriptAlias \texttt{/cgi-bin/} \texttt{/home/www/cgi-bin/}
 <Directory \texttt{/home/www/cgi-bin/>}
 Options ExecCGI
 AddHandler cgi-script cgi pl
 </Directory>
 ,,,

 * test perl cgi support
 -----------------------
 cd \texttt{/home/www/cgi-bin}
 sudo vim test.pl
 \\#!/usr/bin/perl -w
 print "Content-type: text/html\\$\\backslash\\$r\\$\\backslash\\$n\\$\\backslash\\$r\\$\\backslash\\$n";
 print "Hello there!\\$\\backslash\\$n";

 sudo chmod a+x test.pl
 lynx \url{http://yourserverip/cgi-bin/test.pl}
 ,,,

PHP INSTALLATION ....

 * install php5 to run with apache
 >> sudo aptitiude install php5 libapache2-mod-php5

 * install php4 to run with apache
 >> sudo aptitiude install php4 libapache2-mod-php4

 * load the php4 or 5 modules into apache
 >> sudo a2enmod php5
 >> sudo a2enmod php4

 * test if the php installation worked swimmingly
 >> sudo nano \texttt{/var/www/testphp.php}
 >> add the line '<?php phpinfo(); ?>'
 >> lynx \url{http://localhost/testphp.php}

STARTING AND STOPPING THE SERVER

 * restart the apache server
 >> sudo apache2ctl restart

CONFIGURATION TOOLS


\title{} 
\author{bumble.sourceforge.net} 
\maketitle 
\tableofcontents some configuration tools 
 .. rapache - graphical configuration
 ..

MODULES

 @@ \url{http://127.0.0.1/doc/apache2-doc/manual/en/mod/index.html}
 the location of module documentation when apache2 documentation has
 been installed in the webserver
 @@ \url{http://www.debian-administration.org/articles/136}
 how to enable a module

 * show all loaded modules
 >> apache2ctl -M
 >> apachectl -M \\#\\#(the same for older version of apache)
 >> httpd -M \\#\\#(for old versions of apache)

 * show which modules are enabled
 >> ls \texttt{/etc/apache2/mods-enabled/}

 * load the php4 module into apache
 >> sudo a2enmod php4; sudo apache2ctl restart

HTACCESS CONFIGURATION

 To check if .htaccess files are actually being considered
 put junk in them and restart the server.

 * allow .htaccess files to have effect
 >> AllowOverride all

 * prevent the directives in '.htaccess' files from having any effect
 >> AllowOverride None

GLOBAL CONFIGURATION

 * show options for apache2 (which can be used with apache2ctl as well)
 >> man apache2

 * show where the global configuration file is
 >> apache2ctl -V | grep SERVER\\_CONFIG\\_FILE

 * show verbose information about the installation
 >> apache2ctl -V

 * edit the apache2 global configuration file
 >> sudo nano \texttt{/etc/apache2/apache2.conf}
 >> sudo vim \texttt{/etc/apache2/apache2.conf} \\#\\#(for the intrepid)

 * change the default document root to '/home/www/'
 >> DocumentRoot \texttt{/home/www/}
 >> and the following <Directory> tag 
 >> <Directory \texttt{/home/www/>}
 >> ...
 >> </Directory>

 * edit the configuration file for a virtual host
 >> sudo vim \texttt{/etc/apache2/sites-available/default}

ALIASES


REDIRECTIONS

 * place redirections in the ".htaccess" file
 >> Redirect / \url{http://www.site.com}

 * redirect any requests to '/msadc' to microsoft
 >> redirect /msadc \url{http://www.microsoft.com}

 * redirect any request to a 'cmd.exe' file to microsoft
 >> RedirectMatch (.*)\\$\\backslash\\$cmd.exe\\$ \url{http://www.microsoft.com\\$1}

USING MOD REWRITE ....

 @@ \url{http://httpd.apache.org/docs/2.2/rewrite/rewrite\\_guide.html}
 solving specific problems using the rewrite module
 @@ \url{http://www.tutorio.com/tutorial/enable-mod-rewrite-on-apache}
 how to enable the rewrite module

 * redirect filenames ending in ".txt.html" to the cgi script 'text2html.cgi'
 >> RewriteRule \\^(.*)\\$\\backslash\\$.txt\\$\\backslash\\$.html\\$ \texttt{/cgi-bin/text2html.cgi?\\$1}

 For example a request to 
 >> \url{http://www.server.org/path/file.txt.html}
 Is translated to
 >> \url{http://www.server.org/cgi-bin/text2html.cgi?path/file.txt.html}

 * redirect 'www.m.org' to 'www.m.org/cgi-bin/test.cgi?
 >> RewriteRule \\^\\$ \texttt{/cgi-bin/test.cgi?\\$1}

 * allow editing of the '.htaccess' file (probably not a great idea)
 >> RewriteRule \\^htaccess\\$ \texttt{/cgi-bin/edit.cgi?../htdocs/.htaccess}

 If the user makes a request to 'www.m.org/htaccess' she will be redirected to
 'www.m.org/cgi-bin/edit.cgi?../htdocs/.htaccess. If the 'edit.cgi' script
 allows editing of files, then this is one, extremely insecure way of update
 the '.htaccess' file.


CONDITIONAL REDIRECTIONS ....

 * redirect requests from 192.12.131.1 to the page 'about.html' .,,
 SetEnvIf REMOTE\\_ADDR 192.12.131.1 REDIR="redir"
 RewriteCond \\%\{REDIR\} redir
 RewriteRule \\^/\\$ /about.html
 ,,,

DENYING ACCESS

 In some cases it is advisable to disallow any web-access to
 a file on the web-server.

 * deny access from anybody to a web-folder
 >> deny from all

 * forbidd access to all files with a '.cfg' filename extension
 --------------------------------------------------------------
 <Files \\$\\verb|~| "\\$\\backslash\\$.(cfg)\\$">
 order allow,deny
 deny from all
 </Files>
 ,,,

 * forbid access for anyone to the file 'config.inc.php'
 -------------------------------------------------------
 <Files config.inc.php>
 order allow,deny
 deny from all
 </Files>
 ,,,

ALLOW ACCESS BY IP ADDRESS

 Allow connections only from the ip address 192.126.12.199 .,,

 order allow deny
 deny from all
 allow from 192.126.12.199
 ,,,

USING BASIC AUTHENTICATION ....

 
 @@ \url{http://www.htpasswdgenerator.com/apache/htaccess.html}
 a simple introduction to the htaccess file

 * create a password file for user 'jon' to use with basic authentication
 >> htpasswd -c \texttt{/path/to/passwordfile} jon 

 * place the text 'restricted zone' in the title bar of the login box
 >> AuthName "restricted zone"

 * set a password for the file 'private.txt'
 -------------------------------------------
 <Files private.txt>
 AuthName "Users zone"
 AuthType Basic
 AuthUserFile \texttt{/pub/home/your\\_login/.htpasswd}
 </Files>
 ,,,

 * require a password to access all files which end with "x.cgi"
 ---------------------------------------------------------------
 AuthType Basic
 AuthName "Password Protected"
 AuthUserFile \texttt{/home/users/www/.htpasswd}
 <Files \\$\\verb|~| "\\$\\backslash\\$.x\\$\\backslash\\$.cgi\\$">
 Require valid-user
 </Files>
 ,,,

 * require a password to access all files which end with "x.cgi"
 ---------------------------------------------------------------
 AuthType Basic
 AuthName "Password Protected"
 AuthUserFile \texttt{/home/users/www/.htpasswd}
 <Files \\$\\verb|~| "\\$\\backslash\\$.x\\$\\backslash\\$.cgi\\$">
 Require valid-user
 </Files>
 ,,,

 * require a password to access all files which end with 'cgi' or 'txt' 
 ---------------------------------------------------------------
 AuthType Basic
 AuthName "Password Protected"
 AuthUserFile \texttt{/home/users/www/.htpasswd}
 <Files \\$\\verb|~| "\\$\\backslash\\$.(cgi|txt)\\$">
 Require valid-user
 </Files>
 ,,,

 * require a password to access an entire folder
 -----------------------------------------------
 AuthName "Private zone"
 AuthType Basic
 AuthUserFile \texttt{/pub/home/user/.htpasswd}
 require valid-user
 ,,,

 This should be placed in a '.htaccess' file in the folder
 which you wish to protect, or else in a <directory> tag in the
 global configuration file.

LOG FILES


\title{} 
\author{bumble.sourceforge.net} 
\maketitle 
\tableofcontents log file tools
 .. ip2host - substitute servernames for ip addresses in the log files
 .. jdresolv - alternative to logresolv
 .. logstalgia - access log visualizer
 .. visitors - access log analyser
 .. vlogger - log file rotator

 * show the apache2 error log on a debian-style linux system
 >> less \texttt{/var/log/apache2/error.log}

 * look in configuration files to see where the error log file is
 >> grep -sri errorlog \texttt{/etc/apache2}

MONITORING


\title{} 
\author{bumble.sourceforge.net} 
\maketitle 
\tableofcontents apache webserver monitoring tools
 .. apachetop
 ..

LOAD TESTING


\title{} 
\author{bumble.sourceforge.net} 
\maketitle 
\tableofcontents
 .. jmeter - load testing and metering
 
OTHER WEB SERVERS


\title{} 
\author{bumble.sourceforge.net} 
\maketitle 
\tableofcontents some alternatives to apache
 .. mini-httpd - a small server with cgi
 .. lighttpd - small server
 .. cherokee - another one
 .. nanoweb - web server written in php (!?)
 .. nginx - 
 ..

NOTES 

 * restart apache only if config works
 >> alias restart='apache2ctl configtest \\&\\& apache2ctl restart'

 * List apache2 virtualhosts
 >> \texttt{/usr/sbin/apache2ctl} -S 2>\\&1 | perl -ne 'm@.*port\\$\\backslash\\$s+([0-9]+)\\$\\backslash\\$s+\\$\\backslash\\$w+\\$\\backslash\\$s+(\\$\\backslash\\$S+)\\$\\backslash\\$s+\\$\\backslash\\$((.+):.*@ \\&\\& do \{ print "\\$2:\\$1\\$\\backslash\\$n\\$\\backslash\\$t\\$3\\$\\backslash\\$n"; \\$root = qx\{grep DocumentRoot \\$3\}; \\$root =\\$\\verb|~| s/\\^\\$\\backslash\\$s+//; print "\\$\\backslash\\$t\\$root\\$\\backslash\\$n" \};'

 * Restart the web server gracefully
 >> apache2ctl graceful

DOCUMENT-NOTES:
 
 \\# this section contains information about the document and
 \\# will not normally be printed.

 \\# A small (16x16) icon image to identify the book
 document-icon:
 \\# A larger image to identify or illustrate the title page
 document-image:
 \\# what sort of document is this
 document-type: book
 \\# in what kind of state (good or bad) is this document 
 document-quality: just beginning
 \\# work which has been carried out on this document
 document-history:
 @@ 2009
 document begun, in a very rudimentary fashion. 
 @@ jan 29, 2010
 adding some configuration information
 \\# who wrote this
 authors: mjbishop at fastmail dot fm
 \\# a short description of the contents, possible used for doc lists
 short-description: how to administer the apache web server
 \\# the script which will be used to produce html (a webpage)
 make-html: booktohtml.cgi
 \\# the script which will produce 'LaTeX' output (for printing, pdf etc)
 make-latex: booktolatex.cgi


 * Watch for when your web server returns
 >> watch -n 15 curl -s --connect-timeout 10 \url{http://www.google.com/}
 
 * Analyse an Apache access log for the most common IP addresses
 >> tail -10000 access\\_log | awk '\{print \\$1\}' | sort | uniq -c | sort -n | tail
\end{document}
%end generated latex
