How to install Apache on Linux

FREE

Become An Insider

Sign up now and get free access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content from the best tech brands on the Internet: CIO, CITEworld, CSO, Computerworld, InfoWorld, ITworld and Network World. Learn more.

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 -

[ Also on InfoWorld: How to get started with MySQL | 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.

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 www.test.com, to the server's IP address, and then http://www.test.com/ will work.

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

service httpd start

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:

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 webmaster@www.test.com
    DocumentRoot /var/www/test
    ServerName www.test.com
    ServerAlias test.com
    ErrorLog logs/www.test.com-error_log
    CustomLog logs/www.test.com-access_log combined
</VirtualHost>

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

<VirtualHost *:80>
    ServerAdmin webmaster@www.test.com
    DocumentRoot /var/www/test
    ServerName www.test.com
    ServerAlias test.com
    ErrorLog ${APACHE_LOG_DIR}/www.test.com-error_log
    CustomLog ${APACHE_LOG_DIR}/www.test.com-access_log 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 www.test.com, and specifies where the logs should be placed. It then ends the configuration snippet with the </VirtualHost> directive.

Apache on Linux: Beyond the basics
What we've laid out above is an extremely simple example of an Apache configuration. While most installations will require only minimal modifications to the default configurations, there are a few details to know beyond the basics.

You may recall that we also installed PHP in the initial steps. Because of this, PHP has already been configured for use with Apache. You can test this by creating a file in the document root called test.php and typing in the following PHP code:

<?php
phpinfo();
?>

Save the file, then open your browser and access http://<server ip address>/test.php. You should get a listing of all the PHP environment variables, modules, and versions. This means that PHP is installed and functional.

An important Apache configuration element to know is the <Directory> statement. This allows you to configure specific permissions on directories shared by Apache. For instance, within our VirtualHost directive example, we might add the following:

To continue reading, please begin the free registration process or sign in to your Insider account by entering your email address:
Join the discussion
Be the first to comment on this article. Our Commenting Policies