Creating Apache Virtual Hosts with Enable_Disable Vhosts Options in RHEL_CentOS

Apache Virtual Hosts are a powerful feature that allows you to run multiple websites on a single server. With virtual hosts, you can configure Apache to serve different content based on the hostname or IP address of the incoming request. This makes it possible to host multiple websites on a single server, each with their own domain name, content, and settings.

In this tutorial, we will show you how to create Apache virtual hosts on a RHEL/CentOS 8.0 server. We will cover the basic concepts of virtual hosts and walk you through the steps of creating virtual hosts using the Apache configuration files. We will also show you how to use the Enable/Disable Vhosts options to manage your virtual hosts more easily.

Prerequisites

Before we begin, you will need the following

  • A RHEL/CentOS 8.0 server with Apache installed and running.

  • A domain name pointing to your server's IP address. You can use a subdomain if you don't have a separate domain name.

  • Basic knowledge of Linux command line interface and Apache web server.

How Apache Virtual Hosts Work

Apache Virtual Hosts Architecture Client Request Apache Server site1.com site2.com site3.com Request Routes to appropriate virtual host

Creating Virtual Hosts with Apache

Follow the steps outlined below to create your virtual host with Apache.

Step 1: Create a Directory for the Website Files

The first step in setting up a virtual host is to create a directory to hold the website files. This directory will be the document root for the virtual host, which is the top-level directory where the website files will be stored.

For this tutorial, we will create a directory called 'example.com' in the /var/www/html/ directory. This directory will hold the website files for our virtual host.

To create the directory, run the following command

sudo mkdir /var/www/html/example.com

Next, set the appropriate permissions on the directory so that Apache can read and write to it

sudo chown -R apache:apache /var/www/html/example.com
sudo chmod -R 755 /var/www/html/example.com

Step 2: Create a Virtual Host Configuration File

The next step is to create a virtual host configuration file for Apache. This file will tell Apache how to handle requests for our website. Create a new virtual host configuration file for our website using the following command

sudo nano /etc/httpd/conf.d/example.com.conf

Add the following code to the file, replacing example.com with your own domain name

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/html/example.com
    ServerName example.com
    ServerAlias www.example.com
    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>

This code creates a virtual host that listens on port 80 (the default HTTP port) and serves files from the /var/www/html/example.com directory. It also specifies the domain name for the virtual host, as well as the error and access log files.

Save and exit the file.

Step 3: Enable the Virtual Host

Once you have created the virtual host configuration file, you need to enable it in Apache. This tells Apache to start serving content from the virtual host when it receives requests for that domain name.

To enable the virtual host, run the following command

sudo systemctl enable httpd
sudo systemctl restart httpd

This will enable the virtual host and restart Apache so that the changes take effect.

Step 4: Test the Virtual Host

To test the virtual host, create a simple HTML file in the document root and open a web browser to test it

echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/html/example.com/index.html

Open a web browser and enter your domain name in the address bar. If everything is configured correctly, you should see the welcome message.

If you don't see the website, make sure that

  • Your domain name is pointing to the correct IP address.

  • The virtual host configuration file has been saved in the correct location.

  • The virtual host configuration file contains the correct information and syntax.

If you make any changes to the virtual host configuration file, remember to reload Apache for the changes to take effect

sudo systemctl reload httpd

You can also use the apachectl command to test the virtual host configuration before reloading Apache

sudo apachectl configtest

This command will check the syntax of the virtual host configuration files and report any errors.

Enable and Disable Virtual Hosts

Note: The a2ensite and a2dissite commands are specific to Debian/Ubuntu systems with Apache2. On RHEL/CentOS systems, virtual hosts are managed differently

To disable a virtual host on RHEL/CentOS, rename or move the configuration file

sudo mv /etc/httpd/conf.d/example.com.conf /etc/httpd/conf.d/example.com.conf.disabled
sudo systemctl reload httpd

To enable a disabled virtual host, rename it back

sudo mv /etc/httpd/conf.d/example.com.conf.disabled /etc/httpd/conf.d/example.com.conf
sudo systemctl reload httpd

Alternatively, you can create a simple management script to enable/disable virtual hosts by moving configuration files in and out of the /etc/httpd/conf.d/ directory.

Creating Multiple Virtual Hosts

If you need to host multiple websites or web applications on the same server, you can create multiple virtual hosts. To do this, you can create additional virtual host configuration files in the /etc/httpd/conf.d/ directory. Each configuration file should contain the settings for a single virtual host.

Here's an example of how to create a second virtual host

Create a new directory and virtual host configuration file

sudo mkdir /var/www/html/example2.com
sudo chown -R apache:apache /var/www/html/example2.com
sudo nano /etc/httpd/conf.d/example2.com.conf

Add the following content to the configuration file

<VirtualHost *:80>
    ServerName example2.com
    ServerAlias www.example2.com
    DocumentRoot /var/www/html/example2.com
    ErrorLog /var/log/httpd/example2.com_error.log
    CustomLog /var/log/httpd/example2.com_access.log combined
</VirtualHost>

Replace example2.com with the domain name for the second website and adjust the directory paths as needed. Save and close the file, then restart Apache to apply the changes

sudo systemctl restart httpd

You can repeat these steps to create additional virtual hosts for as many websites or web applications as you need.

Troubleshooting Virtual Hosts

If you're having issues with a virtual host not working as expected, there are a few things you can check

  • DNS configuration Make sure that your domain name is correctly configured and pointing to the correct IP address.

  • File permissions Ensure that the files and directories for your virtual host have the correct ownership and permissions. Apache needs to be able to read the files and directories to serve the website.

  • Apache logs Check the Apache error logs (/var/log/httpd/error_log) for any error messages related to the virtual host. This can provide useful information about the issue.

  • Configuration syntax Make sure that your virtual host configuration file is free of syntax errors. You can use the apachectl configtest command to check the syntax of your configuration files.

  • Firewall settings Check that your firewall is not blocking access to your virtual host. You may need to open ports 80 (HTTP) and 443 (HTTPS) if you are using SSL/TLS.

Best Practices for Managing Virtual Hosts

To manage virtual hosts effectively, consider the following best practices

  • Use descriptive names for virtual hosts and configuration files to make it easier to identify them.

  • Keep virtual host configurations simple and avoid unnecessary complexity.

  • Test virtual host configurations regularly to ensure that they are working correctly.

  • Use SSL/TLS to enable secure communication between clients and the server.

  • Regularly monitor your server to ensure that your virtual hosts are performing well and that there are no security issues.

Conclusion

Apache virtual hosts allow you to run multiple websites on a single server, which can be a cost-effective solution for small businesses and website owners. By following the steps in this tutorial, you can create, test, and manage virtual hosts on your RHEL/CentOS 8.0 server with ease. Remember that RHEL/CentOS systems manage virtual hosts differently than Debian-based systems, using direct configuration file management rather than a2ensite/a2dissite commands.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements