InfoWorld how-to's

How to install Apache on Linux

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

InfoWorld how-to's

Show More
1 2 3 4 5 Page 3
Page 3 of 5

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 www.test.com.

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

1 2 3 4 5 Page 3
Page 3 of 5