Ugacomp

A Beginner’s Guide to Apache Configuration Files

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

The Apache configuration file, often named httpd.conf, is a crucial component of the Apache HTTP Server setup. It is a plain text file that contains directives, which are instructions or settings that define how the server operates. These directives specify various aspects of the server’s behavior, such as how it should handle incoming requests, where to find web content, security settings, and more.

Location of the Apache Configuration File

The location of Apache configuration files can vary depending on the operating system and how Apache is installed. Here are common locations for Apache configuration files on different systems:

Ubuntu/Debian

The Apache configuration file in Debian-based systems like Ubuntu is located under this path:

/etc/apache2/apache2.conf

Additional configuration files (included in the main file): /etc/apache2/conf-enabled/

Red Hat/Fedora/CentOS

The main Apache configuration file in Red Hat/Fedora/CentOS is under the following path

/etc/httpd/conf/httpd.conf

Additional configuration files (included in the main file): /etc/httpd/conf.d/

Arch Linux

The main Apache configuration file in Arch Linux is located under the following path:

/etc/httpd/conf/httpd.conf

Additional configuration files (included in the main file): /etc/httpd/conf/extra/

FreeBSD

The main apache configuration file in FreeBSD is located under this path:

/usr/local/etc/apache24/httpd.conf

Additional configuration files (included in the main file): /usr/local/etc/apache24/Includes/

Mac OS X

The main Apache configuration file in Mac OS X is under this path:

/etc/apache2/httpd.conf

Additional configuration files (included in the main file): /etc/apache2/other/

The Apache configuration file is written in a language known as Apache Configuration Language (ACL). The syntax involves directives, which are keywords followed by arguments and often enclosed in angle brackets. Comments begin with a hash (#) and are ignored by the server.

# This is a comment

<DirectiveName argument1 argument2>
  # Directive block
</DirectiveName>

ServerRoot Directive

The ServerRoot directive in Apache is used to specify the base directory under which the server’s configuration files, modules, and other assets are located. It sets the top-level directory of the Apache HTTP Server installation.

The syntax for the ServerRoot directive is as follows:

ServerRoot path
  • path: Specifies the directory path to the Apache server’s root.

Here’s an example:

ServerRoot "/etc/httpd"

In this example, the ServerRoot is set to the directory /etc/httpd, indicating that this is the top-level directory where Apache should look for its configuration files, modules, and other related files.

Listen directive

The Listen directive in Apache is used to specify the IP address and port on which the server should listen for incoming requests. It defines the network address and port number on which Apache will listen for requests from clients. The syntax for the Listen directive is as follows:

Listen [IP_address:]port_number [protocol]
  • IP_address (optional): Specifies the specific IP address or hostname on which the server should listen. If not specified, the server listens on all available interfaces.
  • port_number: Specifies the port number on which the server should listen for incoming connections.
  • protocol (optional): Specifies the network protocol to use, such as “tcp” or “udp”. If not specified, the default is “tcp”.

Here are some examples of how the Listen directive can be used:

Set Apache to listen on Port 80

The default listening port for Apache HTTP Server is port 80 for unencrypted (HTTP) connections. This is the standard port for serving web content over the HTTP protocol.

In the Apache configuration files, you’ll typically find the default Listen directive set to port 80, like this:

Listen 80

Set Apache to listen on Port 443

If you want to serve content over HTTPS, which is the secure version of HTTP, the default port is 443. HTTPS encrypts the communication between the client and the server using SSL/TLS protocols.

If you enable SSL/TLS for HTTPS, you might see an additional Listen directive for port 443:

Listen 443 https

Set Apache to listen on a custom port

For some reason, you may want Apache to listen on a custom port other than the default port 80

Now, you need to look for the existing Listen directive in the file. If it’s not there, you can add it. For example, to set Apache to listen on port 8080 for all available interfaces, here is how you configure it:

Listen 8080

This means that Apache will no longer listen through port 80 but will accept all connections through the custom port you’ve defined.

Set Apache to listen on a custom port for a specific IP address

Specify the desired IP address and port number. For example, to set Apache to listen on IP address 192.168.1.2 and port 8080:

Listen 192.168.1.2:8080

This means that Apache will be accessible only through the specified IP address and will handle web traffic on an alternative port, 8080, instead of the default HTTP port 80. Clients attempting to access the web server will need to specify the custom port in their URLs (e.g., http://192.168.1.2:8080) to establish a connection.

RECOMMENDED READING:

Virtual Hosts

Virtual Hosts in Apache allow you to host multiple websites or domains on a single server. This enables you to run multiple websites with different domain names, each with its own configurations and content, all on the same server

Virtual hosts are defined within the <VirtualHost> and </VirtualHost> tags.

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot "/var/www/html"
</VirtualHost>

In this example, requests to example.com are directed to the specified document root.

Adding multiple virtual hosts in the Main configuration file

You can add various Virtual Hosts in a single Apache configuration file. The typical approach is to include each Virtual Host configuration within the main Apache configuration file (e.g., httpd.conf), or you can create a separate file for Virtual Hosts and include it in the main configuration file.

Here’s an example of adding multiple Name-based Virtual Hosts directly in the main Apache configuration file:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/path/to/website1"
    ServerName www.example1.com
    ErrorLog "logs/example1-error_log"
    CustomLog "logs/example1-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/path/to/website2"
    ServerName www.example2.com
    ErrorLog "logs/example2-error_log"
    CustomLog "logs/example2-access_log" common
</VirtualHost>

In this example, two Virtual Hosts are defined within the same configuration file.

Alternatively, you can create a separate file for Virtual Hosts (e.g., httpd-vhosts.conf) and include it in the main configuration file. In the main configuration file, you would add:

Include conf/extra/httpd-vhosts.conf

And in the httpd-vhosts.conf file, you can define multiple Virtual Hosts as needed

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/path/to/website1"
    ServerName www.example1.com
    ErrorLog "logs/example1-error_log"
    CustomLog "logs/example1-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/path/to/website2"
    ServerName www.example2.com
    ErrorLog "logs/example2-error_log"
    CustomLog "logs/example2-access_log" common
</VirtualHost>

Directory Configuration

The <Directory> directive is used to apply configuration settings to a specific directory. This is useful for fine-grained control.

<Directory "/var/www/html">
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>

Here, we allow directory indexing and override settings in .htaccess files.

Modules and Extensions

Apache functionality is extended through modules. They are loaded using the LoadModule directive.

LoadModule rewrite_module modules/mod_rewrite.so

In this example, the mod_rewrite module is loaded to enable URL rewriting.

Access Control

Access control can be configured using the Require directive within <Directory> or <Location> blocks.

<Directory "/var/www/html/private">
  Require ip 192.168.1.0/24
</Directory>

Here, only clients from the specified IP range are allowed access.

RECOMMENDED READING: How can I block IP addresses in Apache?

SSL Configuration

For secure communication, SSL/TLS can be configured. The necessary directives and certificates are specified within <VirtualHost>.

<VirtualHost *:443>
  ServerName secure.example.com
  DocumentRoot "/var/www/html/secure"
  SSLEngine on
  SSLCertificateFile "/path/to/certificate.crt"
  SSLCertificateKeyFile "/path/to/private.key"
  SSLCertificateChainFile "/path/to/chainfile.pem"
</VirtualHost>

RECOMMENDED READING:

Logging

Apache logs events and errors, providing valuable information for troubleshooting. Log configuration involves ErrorLog and CustomLog directives.

ErrorLog "/var/log/httpd/error_log"
CustomLog "/var/log/httpd/access_log" common

Here, errors are logged to error_log, and access requests are logged to access_log in the “common” format.

RECOMMENDED READING:

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.