Share this page:
Apache can be installed from repositories of most Linux distributions. On Debian based versions of Linux, use this command to install Apache:
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'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.
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.
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.
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.
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:
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:
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.
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:
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:
A Location is similar to a Directory, but instead of applying to directories, they are sets of rules applied to a URL.
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:
The Options directive is used to enable CGI execution.
This is the default virtual host file installed with Apache 2.2:
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:
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: