Share this page:

Apache Basics

Install Apache

Apache can be installed from repositories of most Linux distributions.  On Debian based versions of Linux, use this command to install Apache:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2

On Fedora:

sudo yum update
sudo yum install apache2

By default Apache uses the /var/www directory to store content.  Once you have installed Apache, a sample HTML file will be stored in /var/www/index.html.

Apache configuration files

Apache's configuration files are stored in /etc/apache2.  The main configuration is apache2.conf, which contains global timeout and keepalive settings.

Apache2.conf contains settings for the number of threads and processes that Apache uses to reply to incoming connections.  These options are useful for tuning Apache's performance.

Virtual hosts

A single installation of Apache can contain multiple virtual hosts.  Each virtual host is like a virtual instance of Apache with its own configuration and domain name.

Virtual hosts are stored in files in /etc/apache2/sites-available.  Each virtual host usually has its own file.  In order to enable a virtual host file, the a2ensite command must be used to create a link to the virtual host file in /etc/apache2/sites-enabled.

Modules

Apache modules can be enabled and disabled using the a2enmod and a2dismod commands. The a2enmod command creates a link from /etc/apache2/mods-available to /etc/apache2/mods-enabled.  Some modules have configuration files that can be found in /etc/apache2/mods-available.

Logs

Apache's log files are stored in /var/logs/apache2/.  Every request that Apache serves successfully is recorded in a file named access.log.  Information about errors is stored in /var/logs/apache2/error.log.

Apache's logs are a great source of information if you're debugging problems with your site. 


Comments

Apache 2.2 Virtual Hosts

Apache can serve several web sites on a single server.  Each site may have different configurations.  Apache uses virtual hosts to group configuration settings for each site. Virtual host settings are enclosed in virtual host tags:

<VirtualHost *:80>

</VirtualHost>

An IP address can be used instead of a wildcard.  This determines which of the server's network interfaces the virtual host uses to listen for HTTP requests.  

Suppose a server has two ethernet interfaces with IP addresses 192.168.1.23 and 192.168.1.24. The following virtual hosts would serve domain1.com on the first IP address and domain2.com on the second IP address:

<VirtualHost 192.168.1.23:80>
    ServerName www.domain1.com
    DocumentRoot /var/www/www.domain1.com
</VirtualHost>

<VirtualHost 192.168.1.24:80>
    ServerName www.domain2.com
    DocumentRoot /var/www/www.domain2.com
</VirtualHost>

The port number can be changed if you want Apache to use a different port.

Each virtual host should have a DocumentRoot directive, which specifies the directory where the server's content is stored.  The default directory is /var/www, but different virtual hosts can have different root directories.

ServerName and ServerAlias

When a web browser requests a page from a server, it sends and HHTP request packet where the HTTP host field contains the domain name of the site being requested.  When Apache receives a request, it checks the HTTP host field and looks for a virtual host with a matching ServerName.  The ServerName corresponds to the domain name of a web site.  The ServerName must not be confused with the server's host name.  

The ServerAlias directive is used to match alternate domain names to a virtual host. For example, if you want a virtual host to match mydomain.com and www.mydomain.com, you would need to use the ServerAlias directive like this:

<VirtualHost 192.168.1.24:80>
    ServerName mydomain.com
    ServerAlias www.mydomain.com

    DocumentRoot /var/www/mydomain.com
</VirtualHost>

Directory and Location directives

The Directory directive applies a set of rules to a directory.  For example, the following code restricts access to a directory so that it can only be accessed from IP address 192.168.0.3:

<Directory /var/www/mydirectory>
    Order Allow,deny
    allow from 192.168.0.3
</Directory>

A Location is similar to a Directory, but instead of applying to directories, they are sets of rules applied to a URL.

The CGI Directory

By default, Apache uses /usr/lib/cgi-bin to store CGI scripts.  The ScriptAlias directive tells Apache that files in /usr/lib/cgi-bin/ should be handled as CGI scripts.  URLs whose path starts with /cgi-bin/ will be mapped on to files in /usr/lib/cgi-bin/.  For example, consider a site named mydomain.com that contains a script called myscript.cgi stored in /usr/lib/cgi-bin.  This script could be reached through http://mydomain.com/cgi-bin/myscript.cgi.

The following code is used to define the CGI directory and allow CGI scripts to be executed:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

The Options directive is used to enable CGI execution.  

Default virtual host file

This is the default virtual host file installed with Apache 2.2:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Virtual Host Files

Virtual host files are stored in /etc/apache2/sites-available, but Apache only looks for virtual host files in /etc/apache2/sites-enabled.  The a2ensite command must be used to create a link from files in the sites-available directory to the sites-enabled directory.

If I had a virtual host file named mydomain.conf stored in /etc/apache2/sites-available, I would enable it with this command:

sudo a2ensite mydomain.conf

This command creates a file-system link making mydomain.conf visible in /etc/apache2/sites-enabled. When Apache starts up, it loads virtual host files from /etc/apache2/sites-enabled.

The site can be disabled with this command:

sudo a2dissite mydomain.conf

Comments