Linux Server How To

How To Setup, Configure, Manage and Secure a Linux Server


Linux Server How To - Install the Apache2 Web Server From Source


Installing the Apache2 Web Server From Source

Compiling the Apache2 web server from source is not anywhere near as hard as it may first sound. We will assume that the reader has no prior knowledge of compiling from source but has root privileges and the basic skills required to operate their Linux server. There are several reasons why you may want to compile Apache2 from source, your chosen distribution may not have support for apt or rpm packages or you may simply require a higher level of control over the level of functionality is needed and where things are installed.

Get the Latest Apache2 Source Code

To compile your Apache2 http server you will require the latest source distribution from The Apache Software Foundation Website. The easiest way to get the source distribution onto your Linux server is by using wget but alternately you may prefer to download the source elsewhere and FTP it to your server or copy it to disk or USB drive. Once you have the source distribution you should unpack it using tar and gzip into a directory on your machine so that you can start working with it. Apache2 must be installed as root but you can compile it as an ordinary user. This is what the process of downloading and unpacking Apache 2.2.14 looked like on our Slackware Linux server. Please note that the $ and # simply indicate that there is a prompt and what type of prompt it is, they should not be typed in.

$ mkdir /home/john/apache2
$ cd /home/john/apache2
$ wget http://*Apache 2 Mirror*/httpd-2.2.14.tar.gz
$ gzip -cd httpd-2.2.14.tar.gz | tar xvf -
$ ls
httpd-2.2.14 httpd-2.2.14.tar.gz
$ cd httpd-2.2.14

So on our Slackware Linux server we now have the Apache2 source distribution unpacked and ready to be compiled in the /home/john/apache2 directory. Lets take a look at two typical installations, one with a basic install of Apache2 and the other with PHP support.

Compile a Basic Apache2

Lets take a look at how to compile a basic installation of Apache2. To compile most programs and applications there are usually only three commands involved for the actual compiling, though if you are not logged in as root you will need to use the su command to change to the root user.

$ ./configure
$ make
$ su
Password:
# make install

The configure script performs a series of checks that determine what compiler is being used, what operating system the software is being installed on and what additional options you require so that it may prepare the makefile used by the make program to compile your software. If no additional options are used the configure scripts will fall back to defaults which will generally allow the program to compile successfully but it may be installed somewhere other than where you prefer and lack some functionality you might require. For example without the --prefix= option specified Apache2 will automatically default to installing in /usr/local/apache2 and will create this directory if it does not exist. Please consult our list of Apache2 Compile Options for further information.

A better basic install of Apache2 would be to specify where you want Apache2 to be installed with the --prefix= option. The --enable-so includes DSO capability, allowing the web server to load additional components as modules. A prime example of this would be support for PHP, which can be added to your install of Apache2 down the track as a module without having to recompile it.

$ ./configure --prefix=/usr/local/apache2 --enable-so
$ make
$ su
Password:
# make install

Your freshly compiled install of Apache2 will now be found in /usr/local/apache2 in this example. Your web pages can be placed in the /usr/local/apache/htdocs directory and the web server started by issuing-

# /usr/local/apache2/bin/apachectl start

Compile PHP as a DSO for Apache2

Now we have Apache2 installed on our Linux server we can consider adding support for additional features using dynamic shared objects, which are loadable modules that can allow additional functionality to be added to your webserver with a minimum of fuss. We shall use PHP as an example module, lets start by downloading and uncompressing it much the same as we did with Apache2. The latest source code for PHP can be found at www.php.net.

$ cd /home/john/apache2
$ wget http://au.php.net/get/php-5.3.0.tar.gz/from/this/mirror
$ gzip -cd php-5.3.0.tar.gz | tar xvf -


The above three commands will download and uncompress the PHP source distribution into the /home/john/apache2 directory. From there we must change to the PHP source directory and compile it much the same as we compiled Apache2.

$ cd php-5.3.0
$ ./configure --with-gd --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/php --with-mysql


To explain the configure options a little bit more thoroughly lets take a look at each one.
  • --with-gd - GD allows PHP to process and display images. This can be very useful in a variety of applications from forums to online shops.
  • --prefix=/usr/local/php - Specifies the directory where we want to install PHP. It can be useful to install it in a location that is seperate to other libraries on our Linux server so that it can be moved later should we elect to upgrade it.
  • --with-apxs2=/usr/local/apache2/bin/apxs - specifies that we wish to install PHP as a loadable module and tells the configure script the location of Apache2's APXS.
  • --with-config-file-path=/usr/local/php - This particular option may not be necessary for some Linux server admins, installs the php.ini file within the directory that would be moved during an upgrade.
  • --with-mysql - tells the configure script to include mysql support, very necessary if you intend to have a mysql database backend for your site.
Next we must use the make command to compile PHP, test your build and install it.

$ make
$ make test
$ su
Password:
# make install


We now just have a few more steps to perform before our PHP enabled Apache2 web server is ready to run. Although we have specified the location of the PHP configuration file php.ini the file is not actually installed by the make program. In the top level of the source directory where we initially compiled PHP there are two files, php.ini-production and php.ini-development. Either of these two files will get our PHP installation up and running, with the production file having safer options which we can adjust down the track if necessary. We can copy this file into /usr/local/php and rename it to php.ini.

$ cp /home/john/apache2/php-5.3.0/php.ini-production /usr/local/php/php.ini


We must also make sure that the Apache2 webserver is aware of the PHP module and can load it as required. This is accomplished by adding LoadModule php5_module modules/libphp5.so to Apache2's main configuration file, httpd.conf. In our sample installation this file is located in the /usr/local/apache2/conf directory. There will likely be a section set aside for loading modules as shown below, if there isnt you should be able to add it to the end of httpd.conf without any issues. It is quite possible that this line was added when we installed PHP.

# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
LoadModule php5_module modules/libphp5.so


And the final step is to make sure that Apache2 invokes the PHP parser whenever it encounters a file with a .php extension. This is done by adding AddType application/x-httpd-php .php into httpd.conf in the same section as you see other applications added using the AddType directive. Once again, if you cant find this section add it to the end of httpd.conf.

# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php