How to configure Apache virtual hosts on AlmaLinux

Reading Time: 6 minutes

Apache — one of the most popular web server applications — empowers you to host multiple websites on a single server through a feature called virtual hosts. In this guide, we'll walk you through the process of configuring Apache virtual hosts on AlmaLinux, making your web hosting experience more flexible and efficient.

Don't forget to explore our thread for an in-depth comparison of NGINX vs. Apache, providing the insights you need to optimize your web hosting infrastructure.

Understanding Apache virtual hosts

The following visual can help you grasp the concepts involved with the setup of multiple virtual hosts on a single web server. Having this mental model established as a frame of reference will help as you proceed to configure Apache virtual hosts:

So, what exactly are virtual hosts? Imagine your server as a bustling neighborhood and each site you host as a unique house. Virtual hosts act as signposts directing visitors to the correct house based on the domain they enter.

In simpler terms, Apache virtual hosts enable a single server to host multiple sites, each with its own configuration and content. This capability is crucial for anyone wanting to manage multiple sites efficiently and economically. Instead of using a separate server for each, virtual hosts allow you to use a single server's resources to run multiple websites.

Apache virtual hosts example with setup options explained

Let's walk through creating your first Apache virtual host on AlmaLinux, following the standard conventions for Apache configurations.

Preflight prerequisites

For this guide, we'll cover configurations that are applicable for AlmaLinux distribution of Linux. Here are the prerequisites for configuring Apache virtual hosts AlmaLinux:

  • Login credentials based on root access or using the sudo command to acquire administrative access to your operating system.
  • Apache HTTP Server (as known as Apache Web Server) should be installed on your server. This software is represented by the "A" in the LAMP stack.
  • Having a domain name or IP address ready for your virtual host. Replace your_domain in the guide with your actual domain or IP address.

If you are researching how to configure Apache virtual hosts on the Ubuntu distribution, check out our tutorial that covers the procedural for the configuration of Apache virtual hosts on Ubuntu-based servers.

Step#1. Navigate to the Apache configuration directory

Start by navigating to the Apache configuration directory on your AlmaLinux server. This directory is typically located at /etc/httpd/conf.d/. Use the following command:

cd /etc/httpd/conf.d/

Step #2. Create a new virtual host configuration file

Use a text editor like nano or vi to configure Apache virtual hosts. Replace your_domain with your domain name:

vi your_domain.conf

Inside the configuration file, structure the virtual host with this basic Apache virtual hosts example. You must replace your_domain with your actual domain:

<VirtualHost *:80>
    ServerAdmin webmaster@your_domain
    DocumentRoot /var/www/your_domain/public_html
    ServerName your_domain
    ErrorLog /var/log/httpd/your_domain-error.log
    CustomLog /var/log/httpd/your_domain-access.log combined

Here are the values shown in the example above described in more detail:

  • ServerAdmin: Your email address for server administration.
  • DocumentRoot: The directory where your website files are located, typically in /var/www/your_domain/public_html.
  • ServerName: Your domain name.

Save your changes, followed by exiting the text editor. For vi, press the Esc key, then type :wq followed by pressing the Enter key. For nano, press the Ctrl + X keys, then the Y key to confirm changes, and press the Enter key to exit.

Step #3. Create document root directory

Create the public_html folder for your_domain:

mkdir -p /var/www/your_domain/public_html

Ensure proper ownership and permissions for the DocumentRoot directory to avoid potential issues:

chown -R apache:apache /var/www/your_domain/public_html
chmod -R 755 /var/www/your_domain/public_html

Next, create a simple index.html file for our your_domain (replace your_domain with your domain name in the following command):

echo "testing for your_domain" > /var/www/your_domain/public_html/index.html

Remember that index.html is a default filename commonly used for the main page file of a website.

Step #4. Test and apply Apache virtual host configuration

Before applying the changes, test your new configuration:

apachectl configtest

If you receive an error during the syntax test, carefully review the error message for troubleshooting. If you receive a "Syntax OK" message, restart Apache to apply the new virtual host configuration:

systemctl restart httpd

Congratulations! You've successfully created your first Apache virtual host on AlmaLinux. When visitors access your server using the specified domain, Apache will direct them to the designated DocumentRoot at /var/www/your_domain/public_html.

Step #5. Test the domain in a browser

Testing the domain with your browser can ensure everything is working as expected. Open your web browser (for example, Safari, Chrome, or any other preferred web browser) on your machine. In the browser’s address bar, type the domain you specified when configuring your virtual host and press the Enter key.

If everything is configured correctly, you should see the content from the DocumentRoot directory you specified during virtual host configuration, which is "testing for your_domain" (where your_domain is your actual domain name).

Troubleshooting steps to try if the page fails to load

If the page doesn’t load as expected, consider the following troubleshooting steps:

1. Ensure that the domain you entered resolves to the correct IP address. DNS propagation can take some time, so if you’ve recently configured DNS records, give it more time.

2. Verify that your Apache web server is running:

systemctl status httpd

3. Review the virtual host configuration file for syntax errors:

apachectl configtest

Following these steps, you can confirm that your Apache virtual host is operational and visitors accessing your server using the specified domain are directed to the designated DocumentRoot. Refer to the error messages or logs if you experience any other issues during testing.

Advanced configurations for Apache virtual hosts

Defining a virtual host configuration with <VirualHost>

So <VirualHost> is the starting point for defining a virtual host configuration. It encapsulates the settings specific to a particular host or website. Within this block, you configure the Apache virtual host with specific directives:

<VirtualHost *:80>

Using the asterisk character, colon, and port number

The asterisk (*) character is a wildcard that signifies the IP address to listen on. When set to (*), it means the virtual host will respond to requests on any available server IP address. This step allows the virtual host to be accessible via all network interfaces. The :80 (colon and port number) characters designate the port on which the virtual host listens for incoming requests, where 80 is the default port number for unencrypted HTTP traffic. It’s the standard port used by web browsers to communicate with web servers.

Using the ServerName directive

The ServerName directive is used to configure a Apache virtual host for a specific domain or IP address. This directive plays a crucial role in configuring virtual hosts, where several websites/web applications can be managed on a single web server. When a request is received, Apache uses the Host header from the HTTP request to determine which virtual host should handle the request.

Using the ServerAlias directive

The ServerAlias directive assumes a critical role, especially when aiming to configure Apache virtual hosts precisely. This directive expands the repertoire of server identities, enabling the server to respond to multiple domain names:

ServerAlias www.your_domain 

This example adds www.your_domain as an additional identity for the virtual host.

Using the ErrorLog directive

The ErrorLog directive specifies the file where Apache records error messages and diagnostic information. It allows administrators to identify and fix issues that may occur during the server's operation:

ErrorLog filename | pipe command

Here are the values shown in the example above described in more detail:

  • filename: Specifies the path to the error log file, for example, /var/log/httpd/error_log.
  • pipe command: Allows logging errors to a command or script for further processing.

Using the CustomLog directive

The CustomLog directive defines the log format and destination for recording access events and provides information about requests made to the server. This log is valuable for analyzing website traffic and identifying patterns:

CustomLog filename format | pipe command

Here are the values shown in the example above described in more detail:

  • filename: Specifies the path to the access log file, or example, /var/log/httpd/access_log.
  • format: Defines the log format. Apache provides predefined formats like combined (standard log format with additional information), or you can create custom formats using variables.
  • pipe command: Allows logging access events to a command or script for further processing.

Implementing SSL/TLS for enhanced security

With regard to your assessment of a potential SSL/TLS configuration, you can improve the security of your virtual hosts by implementing SSL/TLS encryption, ensuring the confidentiality and integrity of data exchanged between the server and clients. These benefits make SSL/TLS is essential for your website:

  • SSL/TLS keeps sensitive information protected during transmission, preventing user data interception by hackers.
  • Implementing SSL/TLS provides visual cues such as the padlock image and "https://" being shown in the web browser's address bar, indicating a secure connection.
  • The search results from search websites, such as Google, give priority to secure websites in the page rankings returned. Having SSL/TLS encryption implemented can positively impact your website's search engine rankings.

For detailed instructions on setting up SSL/TLS on virtual hosts, refer to our article on SSL/TLS configuration. This guide covers SSL/TLS implementation on Apache virtual hosts, and the crucial steps to enhance your website's security.

Final thoughts as you configure Apache virtual hosts on AlmaLinux

Apache virtual hosts play a crucial role in optimizing server resources and providing a customized hosting environment for each site. To maximize the potential of your web hosting, consider exploring our various cloud-based hosting solutions or contact our support team for assistance.

Whether you're starting a blog, launching a business site, or managing a portfolio, we have hosting solutions to meet your requirements. Our AlmaLinux-based hosting plans are designed to offer reliable performance and support for a variety of web projects.

Avatar for Luke Cavanagh

About the Author: Luke Cavanagh

Product Operations Manager at Liquid Web. Devoted husband and Tween wrangler. Synthwave enthusiast. Jerry Goldsmith fan. Doctor Who fan and related gubbins.

Latest Articles

Using a Cloudflare Argo Tunnel with load balancing

Read Article

Subdomain takeover — protect your website against it!

Read Article

Controlling PHP settings with a custom php.ini file

Read Article

Linux dos2unix command syntax — removing hidden Windows characters from files

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article