Apache Compile HOWTO (Linux edition)

Luc de Louw

           luc at delouw.ch
        

Revision History
Revision 1.9.122002-04-22
Added mod_gzip and mod_gunzip, Corrected some typos, updates of software mentioned in the HOWTO, separated the additional modules into an own section.
Revision 1.9.112002-04-07
Corrected lots of typos (non-technical), updates of software mentioned in the HOWTO
Revision 1.9.11-pre12002-03-15
Corrected some grammar, updates of software mentioned in the HOWTO
Revision 1.9.102002-03-09
Corrected some grammar, updates of software mentioned in the HOWTO
Revision 1.9.92002-02-11
Fixed a major bug in openssl config, restructured the document, added sources for further informations
Revision 1.9.82002-02-08
Updates of software mentioned in the HOWTO, and fixed some bugs
Revision 1.9.72001-12-26
Updates of software mentioned in the HOWTO, tested the HOWTO procedures on Linux running on IBM S/390 (zSeries) Machines (See "platforms" for more info), Added some basic support for Tomcat (Binaries only)
Revision 1.9.62001-10-27
Updates of software mentioned in the HOWTO, and fixed some bugs
Revision 1.9.52001-08-27
Yet another rewrite in DocBook 3.1
Revision 1.9.42001-08-26
Updated the Software-Versions mentioned in the document, corrected some typos
Revision 1.9.32001-06-23
Current Version 2.0.0-pre3 in Linux DocBook format
Revision 1.0.02000-08-05
First publication of the html-based document

Table of Contents
1. Introduction
1.1. Contributors and Contacts
1.2. Why I wrote this document
1.3. What this document is supposed to be
1.4. What this document doesn't do for you
1.5. Platforms
1.6. Copyright Information
1.7. Disclaimer
1.8. New Versions
1.9. Credits
1.10. Feedback
1.11. Translations
1.12. About the author
2. Prerequisites
2.1. General
2.2. Distribution specific
2.3. OpenSSL
2.4. MySQL
2.5. Building mm
3. Getting, build and install Apache with its basic modules
3.1. Get and untar the Apache Source
3.2. mod_ssl
3.3. mod_perl
3.4. Configure and build Apache
4. Additional modules
4.1. mod_dav
4.2. auth_ldap
4.3. mod_auth_mysql
4.4. mod_dynvhost
4.5. mod_roaming
5. Compressed delivery
5.1. mod_gzip
5.2. mod_gunzip
6. mod_php and its prerequisites
6.1. What is mod_php
6.2. Prerequisites
6.3. Building and installing PHP4
7. PHP extensions
7.1. APC (Alternative PHP-cache)
7.2. Zend-Optimizer (Do _NOT_ combine with APC-Cache!)
8. Server Side Java
8.1. mod_jserv
8.2. jakarta-tomcat
9. Further Information
9.1. News groups
9.2. Mailing Lists
9.3. HOWTO
9.4. Local Resources
9.5. Web Sites
10. Questions and Answers

1. Introduction


1.11. Translations

At the moment there are translations available for:

Translations to other languages are always welcome. If you translated this document, please let me know, so I can set a link here


2. Prerequisites


3. Getting, build and install Apache with its basic modules

3.1. Get and untar the Apache Source


3.1.3. Patch for large-scale sites

If your webserver should answer very much requests at the same time, and your machine is strong enough to serve such an amount of requests, you can change the limit of maximum running processes

Download the patch from: http://www.delouw.ch/linux/apache-patch_HARD_SERVER_LIMIT.txt

--- httpd.h     Thu Mar 21 18:07:34 2002
+++ httpd.h-new Sun Apr  7 13:34:11 2002
@@ -320,7 +320,7 @@
 #elif defined(NETWARE)
 #define HARD_SERVER_LIMIT 2048
 #else
-#define HARD_SERVER_LIMIT 256
+#define HARD_SERVER_LIMIT 512
 #endif
 #endif

This patch does increase the maximum concurrent accessing clients to 512. Feel free to increase it further, if you hacked your kernel and edited your /etc/security/limits.conf (this is ONLY for experienced users! With wrong settings this could end as a “self-denial-of-service-attack”!! Be sure you have enough processes left for root)

Apply the patch using:

cd /usr/local/apache_1.3.24/src/include

patch -p0 < apache-patch_HARD_SERVER_LIMIT.txt

3.4. Configure and build Apache

Now the two static modules mod_ssl and mod_perl are configured and the Apache Source has been patched, and we can proceed with building Apache.


3.4.1. Building and installing


EAPI_MM="/usr/local/mm-1.1.3" SSL_BASE="/usr/local/ssl" \
./configure \
--enable-module=unique_id \
--enable-module=rewrite \
--enable-module=speling \
--enable-module=expires \
--enable-module=info \
--enable-module=log_agent \
--enable-module=log_referer \
--enable-module=usertrack \
--enable-module=proxy \
--enable-module=userdir \
--enable-module=so \
--enable-shared=ssl \
--enable-module=ssl \
--activate-module=src/modules/perl/libperl.a \
--enable-module=perl

make
make install

3.4.2. Create self-signed SSL-certificate


cd /usr/local/ssl/bin

./openssl req -new > new.cert.csr
./openssl rsa -in privkey.pem -out new.cert.key
./openssl x509 -in new.cert.csr -out new.cert.cert \
-req -signkey new.cert.key -days 999

cp new.cert.key /usr/local/apache/conf/ssl.key/server.key
cp new.cert.cert /usr/local/apache/conf/ssl.crt/server.crt

Notice: OpenSSL asks for different things. A common error is to enter a wrong "common name". This should be the FQHN (Fully Qualified HostName) of your Server, i.e www.foo.org


4. Additional modules


4.3. mod_auth_mysql


4.3.3. Building and installing


gunzip mod_auth_mysql.c.gz

/usr/local/apache/bin/apxs \
-c -I/usr/local/mysql/include \
-L/usr/local/mysql/lib/mysql \
-lmysqlclient -lm mod_auth_mysql.c

cp mod_auth_mysql.so /usr/local/apache/libexec/

Add this line in your httpd.conf:

LoadModule mysql_auth_module libexec/mod_auth_mysql.so

And where the other modules are added:

AddModule mod_auth_mysql.c

Take care that the path of Mysql libs and includes are correct!

Notice: Be sure that /usr/local/mysql/lib/mysql is in /etc/ld.so.conf before compiling

Use AuthMySQLCryptedPasswords Off or it does not work! (under investigation why not)


4.3.4. Sample configuration

4.3.4.1. /usr/local/apache/conf/httpd.conf


<location /manual/>
  AuthType Basic
  AuthUserfile /dev/null
  AuthName Testing
  AuthGroupFile /dev/null
  AuthMySQLHost localhost
  AuthMySQLCryptedPasswords Off
  AuthMySQLUser root
  AuthMySQLDB users
  AuthMySQLUserTable user_info
  <Limit GET POST>
    require valid-user
  </limit>
</location>

4.3.4.2. Script for creating the MySQL-Database

just type:


mysql < authmysql.sql

The File authmysql.sql contents:


  create database http_users;
  connect http_users;

  CREATE TABLE user_info (
  user_name CHAR(30) NOT NULL,
  user_passwd CHAR(20) NOT NULL,
  user_group CHAR(10),
  PRIMARY KEY (user_name);

5. Compressed delivery

There are basically two modules available for output compression: mod_gzip and mod_gunzip. They are using different approaches to reach the the goal of bandwidth reduction.

mod_gunzip expects compressed file on the filesystem, and uncompress them if the browser cannot handle compressed data. The benefit is a low cpu-usage, because most browsers are capable to handle gzipped content. On the oder side, most of today's content is served dynamically i.e. PHP, and this content will be delivered uncompressed.

mod_gzip does not need compressed files on the system, all defined content will be compressed before delivery. The benefit is to have the dynamically generated content also compressed, the other side is a higher cpu-usage, because every request has to be compressed on-the-fly. Mod_gzip can handle already compressed data i.e. index.html.gz and send it as-is.

The conclusion: You carefully have to make a decision which of the two modules makes more sense for you. If you have to pay for every GB delivered and CPU-power does not matter, then mod_gzip is the choice for you. If response time matters (delay between request and delivery), and your bandwidth is cheap or unlimited, mod_gunzip matches your needs better.

A good page that helps you to make this decision is Martin Kiff's document about mod_gunzip http://www.innerjoin.org/apache-compression/howto.html


5.1. mod_gzip


5.1.2. Building and installing

/usr/local/apache/bin/apxs -i -a -c -lz mod_gzip.c

5.1.3. Sample configuration

Put the following in your /usr/local/apache/conf/httpd.conf:

mod_gzip_on                 Yes
mod_gzip_can_negotiate      Yes
mod_gzip_dechunk            Yes
mod_gzip_minimum_file_size  600
mod_gzip_maximum_file_size  0
mod_gzip_maximum_inmem_size 100000
mod_gzip_keep_workfiles     No
mod_gzip_temp_dir           /usr/local/apache/gzip
mod_gzip_item_include       file \.html$
mod_gzip_item_include       file \.txt$
mod_gzip_item_include       file \.jsp$
mod_gzip_item_include       file \.php$
mod_gzip_item_include       file \.pl$
mod_gzip_item_include       mime ^text/.*
mod_gzip_item_include       mime ^application/x-httpd-php
mod_gzip_item_include       mime ^httpd/unix-directory$
mod_gzip_item_include       handler ^perl-script$
mod_gzip_item_include       handler ^server-status$
mod_gzip_item_include       handler ^server-info$
mod_gzip_item_exclude       file \.css$
mod_gzip_item_exclude       file \.js$
mod_gzip_item_exclude       mime ^image/.*

6. mod_php and its prerequisites


6.2. Prerequisites

Depending on your needs there are some software to install first. One already installed Software according this document is MySQL, because its needed by mod_auth_mysql.


6.3. Building and installing PHP4


cd /usr/local

tar -xvzf php-4.1.2.tar.gz

cd php-4.1.2


./configure \
--with-apxs=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-pgsql=/usr/local/pgsql \
--enable-track-vars \
--with-openssl=/usr/local/ssl \
--with-imap=/usr/local/imap-2001a \
--with-gd --with-ldap \
--enable-ftp \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-pdflib=/usr/local \
--with-gettext \
--with-mm=/usr/local/mm-1.1.3 \
--with-jpeg-dir=/usr/lib \
--with-zlib-dir=/usr/local \

make
make install

After installing your httpd.conf is modified by axps. It should now look as follows:


<IfDefine SSL>
LoadModule ssl_module         libexec/libssl.so
LoadModule php4_module        libexec/libphp4.so
</IfDefine>

If you compiled Apache with mod_ssl then the php-module will only be loaded when staring Apache with ssl (apachectl startssl). If you will start Apache without ssl support (but compiled like described in this document) you need to change this:


<IfDefine SSL>
LoadModule ssl_module         libexec/libssl.so
</IfDefine>
LoadModule php4_module        libexec/libphp4.so

Copy the sample php.ini to /usr/local/lib/php.ini


cp /usr/local/php-4.1.2/php.ini-dist /usr/local/lib/php.ini

uncomment (remove the # at begin of line) the following lines in /usr/local/apache/conf/httpd.conf

Apache 1.3.24 default httpd.conf does lack of this entries. You have to add them instead of uncommenting


AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

you may wish to add the following:


AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .php3

Restart apache by issuing the following command:


/usr/local/apache/bin/apachectl restart

7. PHP extensions

There are many different extensions available for php, which can be added in your php.ini


7.1. APC (Alternative PHP-cache)

7.1.1. What is APC

Quoting www.apc.communityconnect.com/

APC is the Alternative PHP Cache. It was conceived of to provide a free, open, and robust framework for compiling and caching php scripts. APC was conceived of to provide a way of boosting the performance of PHP on heavily loaded sites by providing a way for scripts to be cached in a compiled state, so that the overhead of parsing and compiling can be almost completely eliminated. There are commercial products which provide this functionality, but they are neither open-source nor free. Our goal was to level the playing field by providing an implementation that allows greater flexibility and is universally accessible. We also wanted the cache to provide visibility into it's own workings and those of PHP, so time was invested in providing internal diagnostic tools which allow for cache diagnostics and maintenance. Thus arrived APC. Since we were committed to developing a product which can easily grow with new version of PHP, we implemented it as a zend extension, allowing it to either be compiled into PHP or added post facto as a drop in module. As with PHP, it is available completely free for commercial and non-commercial use, under the same terms as PHP itself. APC has been tested under PHP 4.0.3, 4.0.3pl1 and 4.0.4. It currently compiles under Linux and FreeBSD. Patches for ports to other OSs/ PHP versions are welcome.

From the authors point of view:

The author made some performance-Tests with apc and it was real surprise. A PHP-Webpage with mysql-queries in a loop (total 10 queries) was more than 50% faster

Contra APC: If you have other users on the system coding php they maybe are not comfortable with APC, because the changes are all ignored unless you reset the cache or restart Apache. The other way, namely that APC checks the php-script for a newer version before every run costs speed.


7.1.3. Building and installing


cd /usr/local

tar -xvzf apc-cvs.tar.gz

cd apc

./configure --enable-apc --with-php-config=/usr/local/bin/php-config

make
make install

cp modules/php_apc.so /usr/local/lib/php/extensions

echo 'zend_extension="/usr/local/lib/php/extensions/php_apc.so"' \
>> /usr/local/lib/php.ini
echo “apc.mode = shm” >> \
apc.mode = shm

Restart your Apache-Webserver. Try it out, create a php-file with the following content:


<?php
apcinfo();
?>

8. Server Side Java

8.1. mod_jserv


8.1.4. Building and installing


cd /usr/local

tar -xvzf ApacheJServ-1.1.2.tar.gz
cd ApacheJServ-1.1.2

./configure \
--prefix=/usr/local/jserv \
--with-apxs=/usr/local/apache/bin/apxs \
--with-JSDK=/usr/local/JSDK2.0 \
--enable-EAPI \

make
make install

It is important to use flag --enable-EAPI if apache is compiled with mod_ssl!


8.2. jakarta-tomcat


8.2.4. Installing the binaries


cd /usr/local

tar -xvzf jakarta-tomcat-4.0.3.tar.gz

cd jakarta-tomcat-4.0.3

cd bin

rm *.bat

echo export JAVA_HOME=/usr/lib/java/ >> /etc/profile
. /etc/profile

Now you should be able to startup tomcat:

/usr/local/apache/jakarta-tomcat-4.0.3/bin/startup.sh

You should now be able to connect to: http://localhost:8080/index.html

For the further steps like installing your servlets and jsp-files, you are responsible by yourself...

Notice: Since the author is NOT a Java-Programmer he will not be able to help you with Java-Problems! The author is an experienced Sysadmin and could you give some hints: Be sure your CLASSPATH Variable is set right. This is the most common error done. Have fun, and as mentioned above, the author welcomes some feedback to be able to provide more information to the community.


9. Further Information

Here are some other resources available on the internet


9.1. News groups

Some of the most interesting news groups are:

Also check out your country newsgroups e.g ch.comp.os.linux

Most newsgroups have their own FAQ that are designed to answer most of your questions, as the name Frequently Asked Questions indicate. Fresh versions should be posted regularly to the relevant newsgroups. If you cannot find it in your news spool you could go directly to the FAQ main archive FTP site. The WWW versions can be browsed at the FAQ main archive WWW site.


9.2. Mailing Lists


9.2.1.

Send an empty email to

Before writing to the list, check out the archive: http://marc.theaimsgroup.com/?l=apache-httpd-users


9.2.2.

Send an mail to with the content (not subject):
   subscribe modperl

Before writing to the list, check out the archive: http://outside.organic.com/mail-archives/modperl/


9.2.3.

Send an mail to with the content (not subject):
   subscribe openssl-users

Before writing to the list, check out the archive: http://www.mail-archive.com/openssl-users@openssl.org/


9.2.4.

Send an mail to with the content (not subject):
   subscribe modssl-users

Before writing to the list, check out the archive: http://www.mail-archive.com/modssl-users@modssl.org/


9.2.5.

Send an empty mail to

Before writing to the list, check out the archive: http://lists.mysql.com/cgi-ez/ezmlm-cgi/


9.2.6.

Fill out the subscription form at http://developer.postgresql.org/mailsub.php

Before writing to the list, check out the archive: http://archives.postgresql.org/pgsql-general/


9.2.7.

Fill out the subscription form at http://www.php.net/mailing-lists.php

There are several php related mailinglist to subscribe, some of them are also available on php.net's newsserver

Before writing to the list, check out the archive that are linked also on the subscription-page


9.2.8.

Send an mail to with the content (not subject):
   subscribe


10. Questions and Answers

Here I answer the questions which I got from users. If you don't find an answer feel free to contact me