How to Setup Apache Virtual Hosts in CentOS 7

May 25, 2023 / Apache Web server

In this tutorial, we will explain how to create Apache virtual hosts on CentOS7.

If you are looking to host more domains on your server, then you need to create corresponding hosts on your web server. In this way, your server can deliver different content for different requests.

How to Install Apache

Before we start, make sure that you have root access to your VPS or server using an SSH connection.

To Install Apache on your CentOS7, follow the command:

sudo yum -y install httpd

Follow the following steps to setup Apache Virtual Hosts in CentOS7

Once it is installed, you need to enable Apache as a CentOS service:

sudo systemctl enable httpd.service

Now, visit your server’s IP address and check whether Apache is running or not. Your page should look like this:

Now, you need to create a directory tree.

Here, a directory tree is used to hold website data and you need to set the working directory to /var/www by running this command:

cd /var/www/

You need to use a unique document root for each virtual host:

mkdir -p

Note: To replace with your actual domain name.

Try to make the directory accessible to Apache. Now run chown to change the ownership and chmod to set the correct permissions for the whole web directory.

Apache now has the necessary rights to add new directories and serve content in response to incoming queries.

Create a Demo Page

It is suggested to create a demo page for each of your Apache virtual hosts. By doing so, you can test the host’s functionality before actually moving your website’s data. Here’s how to do it:

Now, use the nano editor to create an index.html file in directory:


Copy the following content to the file:

    <title>This is a test page</title>
    <h1>It works!</h1>

Save the file by pressing CTRL + X and then Y.

Create the Virtual Host

Create a new virtual host .conf file in the Apache configuration directory:

nano /etc/httpd/conf.d/

Now, insert the following content into the .conf file:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ErrorLog /var/www/
    CustomLog /var/www/ combined

In this above example, we are telling Apache that we will be using port 80 for the communication and that is the name of the virtual host. Also, we specify directories for the website files (document root) and error logs.

Restart Apache

systemctl restart httpd.service

That is it, in this way, you can create an Apache virtual host for your domain!

Leave a Reply

Your email address will not be published. Required fields are marked *