Apache Virtual Hosts on CentOS

  1. Knowledge Base
  2. Software
  3. Systems Administration
  4. Apache
  5. Apache Virtual Hosts on CentOS

Create a new virtual host file on your web server using the vi program:

vi /etc/httpd/conf.d/vhosts.conf

The primary host on the web server should be the first entry. Change the word “Hostname” to whatever your web server host name should be. Be sure that your DNS is set up correctly to point to the IP address.

<VirtualHost *:80>
    ServerName Hostname 
    DocumentRoot "/var/www/html"
</VirtualHost>

As you can see in the config code above, the HTML, CSS, JS, img, and PHP files for the primary host will reside in:

/var/www/html

Let’s add another virtual host to your Apache web server.

Create a new folder structure for your new virtual host, for example:

mkdir -p /var/www/YourDomain.com/public_html

Place the HTML, PHP files and other such files into the public_html folder.

Now we will create the entry in the vhost.conf config file. Always be sure to add the A record to your DNS server for each virtual host that you want to create.

Create a config record for each site in the /etc/httpd/conf.d/vhost.conf file. The additional virtual host record should look as follows.

<VirtualHost *:80>
   ServerName YourDomain.com
   DocumentRoot "/var/www/YourDomain.com/public_html"
</VirtualHost>

After adding or modify8ing the vhost file, restart the Apache service:

systemctl restart httpd.service

Leave a Reply

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