The cart is empty

Utilizing dynamic virtual hosts in the Apache web server enables administrators to efficiently manage a large number of domains without manually configuring each one individually. In this article, we'll focus on configuring dynamic virtual hosts in Apache on the CentOS 7 operating system using the mod_vhost_alias module.

Prerequisites

Before starting the configuration, ensure that:

  • Apache web server is installed on CentOS 7.
  • You have access to the server with superuser (root) privileges.

Installation and Activation of mod_vhost_alias

  1. Install Apache: If Apache is not yet installed, install it using the yum command:
    sudo yum install httpd
    ​
  2. Activate mod_vhost_alias: The mod_vhost_alias module is part of the standard Apache installation on CentOS 7. Verify that the module is installed and activated using the command:
    httpd -M | grep vhost_alias_module
    ​
    If the module is installed, this command will return output confirming its activation. If not, you'll need to recompile Apache with this module, which is beyond the scope of this article.

 

Configuring Dynamic Virtual Hosts

  1. Editing the configuration file: Open the main Apache configuration file for editing:

    sudo vi /etc/httpd/conf/httpd.conf
    
  2. Adding configuration for dynamic virtual hosts: Append the following directives to the end of the file to enable dynamic virtual host configuration:

    UseCanonicalName Off
    VirtualDocumentRoot /var/www/vhosts/%0/public_html
    

    In this example, %0 represents the full domain name of the request (e.g., example.com), and /var/www/vhosts/%0/public_html is the path where Apache looks for files for that domain. Adjust the path according to your directory structure.

  3. Restart Apache: After saving the changes in the configuration file, restart Apache to apply the new configuration:

    sudo systemctl restart httpd
    

Testing the Configuration

  1. Adding a test domain: For testing purposes, add a line to the /etc/hosts file on your test machine that points the test domain to the IP address of your server. For example:
    192.0.2.1 example.com
    ​
  2. Creating directory structure: On the server, create a directory corresponding to the test domain and place an index.HTML file inside it:
    sudo mkdir -p /var/www/vhosts/example.com/public_html
    echo "Test page for example.com" | sudo tee /var/www/vhosts/example.com/public_html/index.html
    ​
  3. Testing in the browser: Open a web browser and navigate to http://example.com. You should see the content of the index.html file.

Configuring dynamic virtual hosts in Apache on CentOS 7 using the mod_vhost_alias module allows for efficient management of web domains without the need for manual configuration for each one. With dynamic mapping, you can easily expand your web services by adding new domains and corresponding directories without further server configuration.