Set up Apache Virtual Host File on Centos 7

In this article, we want to teach you How to set up an Apache virtual host file on Centos 7. Let’s see what a virtual host means and go through the setup guide.

What is a virtual host?

The concept of virtual hosts allows more than one Web site on one system or Web server. The servers are different by their hostname. Visitors to the Web site are routed by hostname or IP address to the correct virtual host. Virtual hosting allows companies to share one server to each have their own domain names.

Steps to Set up Apache virtual host file on Centos 7

Before you start setting up Apache virtual host on Centos 7 you need some requirements. Let’s what we need.


You must have access to your server a root or non-root user with sudo privileges. To do this, you can check this guide on the initial server setup with Centos 7.

Also, you need to install Apache on your server and set up a basic firewall. To do this, you can visit this guide on Install Apache on Centos 7.

Then, you need a valid domain name that is pointed to your server’s IP address.

Now proceed to the following steps to complete this guide.

Apache Virtual Host File Setup

To set up Apache virtual host on Centos 7 follow these steps:

1. Create the HTML directory for your domain name.

sudo mkdir -p /var/www/

Note: You have to put your domain name instead of

2. Create an additional directory to store log files for the site:

sudo mkdir -p /var/www/

3. Set ownership of the HTML directory with the $USER environmental variable:

sudo chown -R $USER:$USER /var/www/

4. Set default permissions for your webroot:

sudo chmod -R 755 /var/www

5. create a sample index.html page using Vi editor:

sudo vi /var/www/

Type the following content in your file:

<title> Here you are in!</title>
<h1> The virtual host is done!</h1>

Then save and close your file.

To set up an Apache virtual host on Centos 7, you need a sites-available and sites-enabled directory.

6. Create a sites-available directory and sites-enabled. Create both directories with the following command:

sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled

7. In this step you need to edit Apache’s main configuration file and add a line declaring an optional directory for additional configuration files:

sudo vi /etc/httpd/conf/httpd.conf

Add this line to the end of the file:

IncludeOptional sites-enabled/*.conf

Save and close the file.

8. Now you should create your Apache virtual host file on Centos 7:

sudo vi /etc/httpd/sites-available/

Type this in your editor:

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

9. Create a symbolic link for each virtual host in the sites-enabled directory:

sudo ln -s /etc/httpd/sites-available/ /etc/httpd/sites-enabled/

10. Run the following command to set a universal Apache policy:

sudo setsebool -P httpd_unified 1

Note: if you disable SELinux before, you don’t need to run this command.

11. check the context type that SELinux gave the /var/www/ directory:

sudo ls -dZ /var/www/

12. Generate and append to web application log files:

sudo semanage fcontext -a -t httpd_log_t "/var/www/*)?"

13. use the restorecon command to apply these changes:

sudo restorecon -R -v /var/www/

Restart your web server with the following command:

sudo systemctl restart httpd

Now you are ready to test your Apache virtual host configuration on Centos 7. In your desired web browser, type your domain name:

You will see a page similar to the image below:

Test Apache virtual host

That’s it you are done.


At this point, you have learned to Set up the Apache Virtual Host file on Centos 7 step by step. You just need to have a valid domain name and follow the instructions. Hope you enjoy it. Also, you may interested in these articles:

Configure Apache on an Ubuntu or Debian VPS

Install LAMP stack on Centos 7

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!