How To Compile And Install Apache 2.2
Introduction
The Apache HTTP Server is the world’s most popular web server. Initially released in 1995, it dramatically increased in popularity and now is used to host many of the world’s most popular websites.
Although Microsoft’s IIS has remained popular and various other web servers such as nginx have appeared in recent years, Apache still retains that top position and should be your first consideration for a Linux server. It’s popular for a reason, after all. Let’s get started!
Why compile from source?
Many guides exist on the internet that show how to install Apache on a Linux system via a package manager such as Synaptic or Aptitude, so what are the advantages to compiling from source? The answer is relatively simple. Compiling Apache from source provides you with a flexibility and freedom to manipulate the installation in a way that packaged versions (binaries) do not. You’re able to explicitly define what you want included, therefore avoiding the installation of things you’ll never need or use. Additionally, a packaged version usually won’t be the latest release; it may lack features, security updates and other components that the latest source release includes. Other configuration options such as being able to specify where you want it to be installed are possible when compiling from source.
Requirements
Apache runs on a variety of Operating Systems (including Windows), but this article will concentrate on Linux. So obviously you’ll need a Linux machine with roughly 50MB of disk space available. When it’s installed it takes up considerably less, but 50MB are required for temporary files during the installation process. Let’s list this:
* Linux. For this article, I’ve used a default installation of Debian 5.
* A server with at least 50MB of free disk space. These days, this shouldn’t be a problem.
* An ANSI-C compiler. GCC is the most commonly used and is the one I use here.
* Basic linux tools such as make. These days, your linux distribution should come with them.
* Utilities such as tar and gunzip.
* Root access
Note: Although I’m using Debian 5 here, the same instructions will apply on virtually any Linux distribution.
Getting started
Step 1: Get the Apache HTTP server source
You could use a browser to do this or a tool such as wget. As root, I’ve browsed to /usr/src on my server and used wget to download the latest version of the source code from the Apache website. Right now this is 2.2.17.
wget http://apache.mirror.anlx.net//httpd/httpd-2.2.17.tar.gz
Note: The URL may be different for you.
Step 2: Unpack the archive
The .tar.gz extension tells us that it’s a compressed tar archive, so we’re going to need the tar tool to decompress and extract it. The tar command with the z, x and f flags will do this. If you list the directory using ls, you’ll see that a new directory has been created. In my case this is httpd-2.2.17 but the version may be different for you.
tar zxf httpd-2.2.17.tar.gz
Step 3: Configuration
The configuration is probably the trickiest part of the whole process. You need to have an idea of your requirements before attempting this stage. For example, do you need the ability to support SSL? Do you want to install Apache somewhere other than the default location? If you’re unsure, I suggest you spend a bit of time thinking about it, as you don’t want to have to recompile at a later date if you forget something now. The configuration script Apache uses is called just that – configure – and it’s located in the source directory that was just created. Let’s browse to that directory.
As you can see, the configuration script is there. Now let’s look at the help it provides.
./configure –help
That’s a lot of help! It lists and gives helpful explanations of all the available configuration parameters. For example, for SSL support, we can look down the list and see that –enable-ssl enables the mod_ssl module. So when configuring our installation, the command might look like this:
./configure –enable-ssl
A detailed look at all the available options is a long way outside the scope of this article, but you should hopefully have some idea of what you might need. If in doubt, Google it. For example, if you know that you’ll be using PHP, a quick search will tell you the Apache configuration requirements needed. The official Apache documentation can be a great place to look, also. You can find it at http://httpd.apache.org/docs/2.2/.
I’m going to include the SSL module, the Proxy module and the ability to rewrite URLs in my installation, so my command will look like this:
./configure –enable-ssl –enable-proxy –enable-rewrite
Lots of information will start scrolling up the screen as the configuration script checks that it has everything it needs. You’ll see a lot of things that it doesn’t have but don’t worry, this is normal. If something goes wrong and you end up with an error at the bottom telling you that configuration has failed, don’t panic. In most cases this is because something simple has been overlooked – that you don’t have GCC installed, for example. The messages are usually fairly comprehensive, so just read it and work out what’s missing. If you’re stuck, Google will certainly be able to help you.
Our installation is now configurated and all that’s left to do is to compile it and install it. On to Step 4!
Step 4: Compilation
To compile Apache, a utility called make is used. All we have to do is issue the command. Make sure you’re still in the source directory otherwise make won’t work.
make
You’re going to see lots of scrolling information as Apache compiles and depending on your server, it might take a little while.
Step 5: Installation
This is the last point at which you can go back, reconfigure and recompile if you’ve forgotten something. Once it’s installed, you’d have to remove everything and start again, so make sure you’ve included everything you need! The make install command will install Apache.
make install
Again, there’ll be lots of scrolling information and probably a few warnings too. Don’t worry about those, Apache will let you know if anything goes drastically wrong. The installation doesn’t take nearly as long as the compilation so within a minute or so it should have finished.
You’ve just compiled and installed Apache!
Step 6: Check it works!
Every installation of any kind should include this step.
As I left the installation path as the default, Apache should be installed in the /usr/local/apache2 directory.
If you navigate there, you’ll see that the directory structure is fairly comprehensive. You can see the conf/ directory (where obviously the configuration files are located), bin/ (which holds various binaries, including the one we’ll use to start and stop Apache) and various others. I recommend reading through the Apache documentation to gain a good understanding of what everything is.
The htdocs/ directory is where your website files will go, and if you take a look in there you’ll see index.html is already there. This is a test page included by default so you know that your Apache installation has completed successfully. Let’s start up Apache and check. As mentioned, the bin/ directory holds the binary we need to start and stop Apache. It’s called apachectl. The following command will start the server:
/usr/local/apache2/bin/apachectl start
If we then list the processes on the system and grep for apache, we should be able to see if it’s running or not:
ps aux | grep apache
If all looks well, we should be able to browse to index.html. If you take a look in httpd.conf within the conf/ directory, you’ll be able to see that by default, Apache will run on localhost port 80. So let’s go there. Type the following into a browser of your choice:
Congratulations, you’ve successfully compiled and installed Apache 2.2! To stop the Apache server you can use the same command as above, but with stop instead of start.
I hope this how-to has been useful. Please feel free to comment.
Useful Links
Apache 2.2 documentation – http://httpd.apache.org/docs/2.2/
Apache mailing lists – http://httpd.apache.org/lists.html
Written by Mike Evans
Freelance writer
