Multiple Virtual Hosts on CentOS 7 or RHEL 7

11174

This article tends to help you to set up multiple websites (virtual hosts) on single 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, etc.

The idea lies in the fact that 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.

For Ubuntu version follow this link: https://www.linux.com/blog/shared-hosting-your-web-server-multiple-virtual-hosts. 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 your 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 web server’s configuration.

Let’s assume we want to build  website called cutepuppies.com  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)

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
Assuming you have fresh Apache installation, it most likely doesn't know that there will be more than one website yet. 
We need to make your webserver to look for configuration files for each website created in the future. Go to /etc/httpd/conf/httpd.conf and add this line into the file:
IncludeOptional sites-enabled/*.conf

Check Apache configuration folder /etc/httpd. You need to craete two folders if they don’t exist: /etc/httpd/sites-available and /etc/httpd/sites-enabled. Go to /etc/httpd/sites-available and create configuration file cutepuppies.conf in there. In order to make your website working, this file should contain at least following data:

<VirtualHost *:80>
        ServerName www.cutepuppies..com
        ServerAlias cutepuppies.com
        DocumentRoot    /var/www/cutepuppies/
</VirtualHost>

Once your configuration file is ready, you need to enable it. Create sympolic link to your configuration file in sites-enabled folder for that:

ln -s /etc/httpd/sites-available/cutepuppies.conf /etc/httpd/sites-enabled/cutepuppies.conf

Now to make it work after changes, you need to reload configuration of your 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.

If you have anything to add or clarify, please feel free to comment and contibute.