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.

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, open a web browser and enter your domain name in the address bar. If everything is configured correctly, you should see the website you created in Step 1.

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

In addition to creating virtual hosts, you can also enable and disable them as needed. This is useful when you want to temporarily take a website offline or bring it back online.

To enable a virtual host, use the a2ensite command followed by the name of the virtual host configuration file −

sudo a2ensite example.com.conf

This command creates a symbolic link from the sites-available directory to the sites-enabled directory, effectively enabling the virtual host.

To disable a virtual host, use the a2dissite command followed by the name of the virtual host configuration file −

sudo a2dissite example.com.conf

This command removes the symbolic link from the sites-enabled directory, effectively disabling the virtual host.

When you enable or disable a virtual host, remember to reload Apache for the changes to take effect −

sudo systemctl reload httpd

Reloading Apache after enabling or disabling a virtual host ensures that the changes take effect immediately.

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 virtual host configuration file in the /etc/httpd/conf.d/ directory −

sudo nano /etc/httpd/conf.d/example2.com.conf
  • Add the following content to the configuration file −

<VirtualHost *:80>
  ServerName example2.com
  DocumentRoot /var/www/example2.com/public_html
  ErrorLog /var/www/example2.com/error.log
  CustomLog /var/www/example2.com/access.log combined
</VirtualHost>

    Replace example2.com with the domain name for the second website and /var/www/example2.com/public_html with the directory where the website files are located.

  • Save and close the file.

  • 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.

By following these best practices, you can ensure that your virtual hosts are configured correctly and functioning optimally.

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, enable, and disable virtual hosts on your RHEL/CentOS 8.0 server with ease. Remember to keep your virtual host configuration files organized and up to date, and to reload Apache whenever you make changes.

Updated on: 23-Jun-2023

492 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements