How to install Apache on Linux

An easy step-by-step guide to setting up an Apache Web server on Fedora, CentOS, or Ubuntu

The installation, care, and feeding of an Apache Web server is not terribly difficult, but can seem so if you haven't ventured into those particular waters before. This quick-start guide will help you get your feet wet with Apache on a Linux server. You'll find it's relatively simple to get the Web server set up and running on your Linux of choice. We'll also install PHP and MySQL, though we won't be digging into MySQL configurations, as that deserves a quick start all its own.

The method of installing the Apache packages on a Linux server varies from distribution to distribution. We'll cover how to do this on Fedora and CentOS, as well as on Ubuntu. This is a server-centric walkthrough, so we'll use the command line exclusively. Naturally, you'll need root-level privileges. Open the terminal window and type:

su -

[ Prove your expertise with the free OS in InfoWorld's Linux admin IQ test round 1 and round 2. | Track the latest trends in open source with InfoWorld's Open Sources blog and Technology: Open Source newsletter. ]

Then enter the root password. Now we can get started.

First we'll install the packages themselves. For Fedora and CentOS, this is a simple step involving Yum, the package installer and updater. To install the basic Apache and PHP packages, run the following command:

yum install httpd php mysql mysql-server

Follow the prompts, as this tool will locate and install a base set of Apache and PHP packages.

For Ubuntu 10.04 servers and newer, you can install the whole LAMP (Apache, MySQL, and PHP) stack with two commands:

sudo apt-get install taskselsudo tasksel install lamp-server

While this guide does not cover MySQL, the above commands are a quick way to get all the necessary packages required for LAMP applications. Once the installation is complete, we can begin configuring the server.

For all file editing, on Fedora, CentOS, or Ubuntu, you may want to use nano:

nano /etc/httpd/conf/httpd.conf

This command will open the Apache configuration file in a basic editor. You can save the file with Ctrl-O and exit the editor with Ctrl-X.

Apache on Linux: Initial configuration While the Apache and PHP packages are essentially the same across the different distributions, there are differences in how they are actually installed on the file system. We'll start with Fedora and CentOS.

Fedora and CentOS. After installation, you'll find a new directory: /etc/httpd. Within this directory are all the Apache configuration files. The important subdirectories for our purposes are /etc/httpd/conf and /etc/httpd/conf.d. Within /etc/httpd you'll find the main Apache configuration file, httpd.conf. In /etc/httpd/conf.d you will find includes, or supplemental files that are included in the main configuration file.

Includes are a way to break out complex configurations into separate files for easy organization and management. For instance, if you have an Apache server that has 20 virtual hosts, then each virtual host should have a separate file in /etc/httpd/conf.d/ that contains its specific configuration parameters. In this way, you can easily add or remove virtual hosts without editing the main Apache configuration file.

In order for files to be included in the Apache configuration, they must have a filename that ends with .conf. If we have a virtual host named, all the configuration elements for that virtual host would reside in a file named test.conf or

You can see how these files are included in the main configuration file by looking at /etc/httpd/conf/httpd.conf. Press Ctrl-W to search for "Include conf.d" and you'll find this line:

Include conf.d/*.conf

That tells Apache to include all files matching *.conf in the /etc/httpd/conf.d folder into the main configuration.

There are many configuration elements present in the Apache configuration file, but beginners need to be concerned with only a few. These are the elements that control where our Web root is, how to handle virtual hosting, and a few other minor tweaks. To start with, the default settings present in this file will be fine.

By default, the Web root on Fedora and CentOS is /var/www/html. This means that any files placed under /var/www/html will be served via Apache when a Web browser connects to the server. If a file exists as /var/www/html/test.html, and you open a Web browser and connect to http://<server IP address>/test.html, the server will deliver that file to the browser. If the server will host only a single website, then you can put all of your content under this directory and configure DNS to map a name, such as, to the server's IP address, and then will work.

Controlling the Apache server is very simple. To start the server on Fedora or CentOS, run:

service httpd start

Stopping the server is equally simple:

service httpd stop

To restart the server, you can also use:

service httpd restart

For simply reloading the configuration file without restarting the service, use the following command:

service httpd reload

Another very handy control:

service httpd configtest

This tests the Apache configuration file and all included files for errors without starting or restarting the server. This allows you to make sure your configurations are valid before actually causing Apache to run with that configuration, which makes it easier to add and test configurations without disturbing the running server.

To make sure that Apache starts when the server boots, simply run:

chkconfig httpd on

This will add Apache to the list of services to start at boot time.

Ubuntu. Ubuntu follows the same basic procedure as Fedora and CentOS, but places the files in different directories. The global Apache configurations are found at /etc/apache2/apache2.conf. As in Fedora and CentOS, includes break out different Apache configuration elements to separate files, but go further in Ubuntu, breaking out many more sections of the global configuration to other files (such as ports.conf, which controls the ports Apache listens on). For beginners, these files will not need to be modified.

Unlike Fedora and CentOS, Ubuntu has a specific directory for virtual host configurations, /etc/apache2/sites-available, and a companion directory, /etc/apache2/sites-enabled. Configuration elements specific to individual virtual hosts are placed in sites-available, then symlinked from sites-enabled, making it easy to add and remove active virtual hosts from the configuration without deleting or renaming the files. Just add or remove a symlink to bring them to the configuration.

Ubuntu also has a different default Web root, /var/www, so if a file named test.html exists in /var/www, Apache will deliver that file to a Web browser that requests the URL http://<server IP address>/test.html. If the server will host only a single website, then you can put all content under this directory and configure DNS to map a name, such as, to the server's IP address, and then will work.

Controlling Apache in Ubuntu is as simple as in Fedora and CentOS, but generally relies on a different command-line method. Although some versions of Ubuntu support the service command, you can always count on the following commands.

To start Apache, run:

sudo /etc/init.d/apache2 start

To stop Apache, run:

sudo /etc/init.d/apache2 stop

To restart the server, run:

sudo /etc/init.d/apache2 restart

As in Fedora and CentOS, you can reload the configuration files without restarting the service, by running:

sudo /etc/init.d/apache2 reload

And you can test the configuration file syntax with:

sudo apachectl configtest

There are several different ways to configure Apache to start at boot with Ubuntu, but a general method is to use update-rc.d:

sudo update-rc.d apache2 defaults

This will cause Apache to start when the server boots. You might also look into using Upstart, which is a newer method of starting system services at boot.

Apache on Linux: General configuration Regardless of the Linux distribution, Apache's configuration follows standard concepts. A primary element is the use of opening and closing tags, which are patterned after HTML tags. For instance, if you have a <Directory> statement, everything after that statement is within the <Directory> context, which is terminated with a </Directory> tag. If you forget to add a closing tag, the configuration syntax will not be valid and the server will refuse to run.

It's a good idea to read through the default httpd.conf (Fedora and CentOS) or apache2.conf (Ubuntu) file and included files. While you may not understand everything, these files are well commented, providing context and sometimes even examples on proper usage. They will also give you an idea of how the overall configuration file is constructed and clue you in to different aspects of the configuration that might come in handy later on when you're attempting more advanced configuration tasks.

For our purposes here, we can leave the default configuration as-is and work on extending that configuration to suit our needs. The default settings for server operation are generally well suited for small and experimental servers, but production servers that are expected to handle heavy loads may require extensive modifications to the default settings. But let's start simple.

As mentioned above, you can place your content in Apache's document root directory and it will be served to clients, but in general it's better to configure virtual hosts. It makes no difference to the server if there's only one virtual host, and multiple virtual hosts can be easily added later using the same formula.

Configuring a virtual host is distribution-agnostic. Simply create a file in the default virtual host include directory for your distribution as described above.

We'll create the file test.conf. Within this file we will add all the configuration elements necessary to have the server deliver content for

Note: For Fedora and CentOS, you may have to edit the /etc/httpd/conf/httpd.conf file and remove the # character before NameVirtualHost *:80 near the bottom of the file. This will allow name-based virtual hosts to function. To do this, open the file in nano:

nano /etc/httpd/conf/httpd.conf

Then press Ctrl-W and locate NameVirtualHost by typing in that search string. Remove the # (comment character) from the beginning of the line and save the file.

Now we're ready to create our virtual host. Using Fedora, we'd open a new file in /etc/httpd/conf.d/:

nano /etc/httpd/conf.d/test.conf

For Ubuntu, we would run:

sudo nano /etc/apache2/sites-available/test

We now have an empty file that we need to fill up with the configuration for our virtual host. Here's an example file for Fedora or CentOS:

<VirtualHost *:80>    ServerAdmin    DocumentRoot /var/www/test    ServerName    ServerAlias    ErrorLog logs/    CustomLog logs/ combined</VirtualHost>

We need a slightly different file for Ubuntu due to different conventions for log file placement:

<VirtualHost *:80>     ServerAdmin     DocumentRoot /var/www/test     ServerName     ServerAlias     ErrorLog ${APACHE_LOG_DIR}/     CustomLog ${APACHE_LOG_DIR}/ combined </VirtualHost>

This configuration snippet tells Apache that we have a virtual host that will be listening on TCP port 80 (the standard HTTP port). It also tells Apache that the document root for this virtual host is /var/www/test, indicates that the server name is, and specifies where the logs should be placed. It then ends the configuration snippet with the </VirtualHost> directive.

In order for this virtual host to work, the directory /var/www/test must exist and have some contents, such as an index.html file, and DNS needs to be configured to point the DNS name to the server's IP address. Note the ServerAlias directive in the configuration file. This allows Apache to accept either or as a valid name when a browser requests the site. This is important, as many people omit the initial www when manually typing in website URLs. You can also use ServerAlias to allow the server to deliver content for a completely different name, such as, from the same document root.

Once you have modified the virtual host configuration file to your needs, such as changing the server name and the DocumentRoot directory, then press Ctrl-O to save the file and exit the editor.

At this point, you can go on to create other virtual hosts in the same manner. You can create the document root with the mkdir command on Fedora or CentOS:

mkdir /var/www/test

An alternative on Ubuntu:

sudo mkdir /var/www/test

Then place an HTML file in that directory called index.html:

nano /var/www/test/index.html

Next, type this into the file:

<html> This is a test. </html>

To test the configuration on Fedora or CentOS, use:

service httpd configtest

1 2 Page 1
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon