Setting virtual hosts on Apache

Setting virtual hosts on Apache

This article is going to help you to set up multiple websites (virtual hosts) on single Apache web server running on Linux CentOS 7 or Red Hat 7.

It’s particularly useful if you don’t want to spend money on multiple Virtual Private Servers (VPS), but you’d like to have ability to run and build multiple websites with minimum cost on the same server. In real world scenario, most of those sites would be based on common content management systems such as WordPress, Drupal, Joomla, or even simple HTML template.

The idea is instead of having single root directory for your website (which is/var/www/html/ in our example), you can have multiple folders located in /var/www/ where each folder will contain separate website.

Note, this is basic setup tutorial and shouldn’t be used as a guide how to set up proper environment for big commercial projects.

What would you need to have to make it work:

  • web server which already has full LAMP stack on it. As example in this article, we use RHEL 7.2 and Apache (httpd) as web server.
  • root access to this web server

What we actually would do? When you want to add new website into your server, there are following three steps to consider:

  • pointing your domain into that folder
  • creating root folder for the website and setting up web server to recognize that folder

Note, it doesn’t matter in what sequence you are following these steps, as long as they all completed. And once all these steps completed, you are good to go with installation of your CMS as per its documentation.

All actions shown as performed under root account. Remember, it’s best practice to use regular account with sudo rather than root to perform any changes in your system.

Step 1: Point the domain

Point web traffic of your  domain on to your server as per requirements of  your domain provider. Essentially it means you would need to create A record which resolves into your web server’s IP.

Step 2: Adding virtual host into Apache server’s configuration.

Let’s assume we want to build  website called  and related configuration files and folders in our server will be named accordingly as cutepuppies

Make separate root directory for your new website:

mkdir  /var/www/cutepuppies

Give ownership of the directory to the Apache web user (which is apache in case of RHEL):

chown apache:apache -R /var/www/cutepuppies

Also, if you action not under root account, add your username to the web group:

usermod -aG apache YOUR_USER_NAME

Now check Apache configuration folder /etc/httpd/conf.d/.

Apache server configuration folderCreate configuration file for your virtual host and name it with .conf extension, for example cutepuppies.conf
Open that file and include at least the following:

<VirtualHost *:80>
        DocumentRoot  /var/www/cutepuppies/

Now to make everything working, you need to restart web server:

systemctl restart httpd

Now you are ready to unpack your website into root directory /var/www/cutepuppies/, the website should be accessible by your domain.

Please check other articles about Linux configuration.

Leave a Reply

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