How To Configure Apache Virtual Hosts on CentOS 7

How To Configure Apache Virtual Hosts on CentOS 7


Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off of one virtual private server.


  • A desktop machine running Linux
  • A static IP address.

Set Up

sudo yum update
sudo yum install httpd (for Centos )
sudo apt-get install httpd (for Ubuntu )
start Httpd service
/etc/init.d/httpd start ( Centos )
/etc/init.d/apache2 start  ( Ubuntu )

Create a New Directory

First,make a directory structure where we will keep the new website’s information.
mkdir -p /var/www/html/

Grant Permissions

chown -R apache:apache /var/www/html/\
chmod -R 755 /var/www/html

Create the Page

We need to create a new file called index.html within our configurations directory.
Vim /var/www/html/
Add the following content.
<h1>The virtual host is working!</h1>
Save and close the file when you are finished.

Turn on Virtual Hosts

The next step is to enter into the apache configuration file itself.
sudo vi /etc/httpd/conf/httpd.conf
Listen 80
<VirtualHost *:80>
DocumentRoot /var/www/html/
ErrorLog /var/www/html/
CustomLog /var/www/html/ combined

Save & Exit
When you are finished, You can check the syntax of files with the following command:
sudo apachectl configtest
After the syntax check is done, restart Apache to make these changes take effect.
sudo systemctl restart httpd

Setting Up the Local Hosts

Now, you need to create DNS record for to test the virtual hosts. One easy way is to add a "domain name to IP mapping" entry to the /etc/hosts file.
Add the following lines, using the static IP address of your server:
Save and exit the file.
Now on the desktop Linux computer, open your web browser and go to the URLs