How to Set Up Name-Based Virtual Hosting (Vhosts) With Apache Web Server on CentOS 7

Introduction

The Apache web server is the most popular web server all over the world. Virtual hosting is a method for hosting multiple websites (domains) on a single server. You can host multiple websites on a single machine with a single IP using virtual hosting. All domains on that server will be sharing a single IP. Virtual hosting is very useful in shared web hosting environments, where hundreds of websites are hosted on a single dedicated server.

In this article we will be hosting two websites (www.vhost1.com and www.vhost2.com) on a single IP address (192.0.2.0).

Requirements

  • A server running CentOS v. 7 with Apache installed
  • A desktop machine running Linux
  • A static IP address.

Create the directory structure

First, you need to make a directory structure which will hold the web pages. This directory is known as the document root for the domain.

In CentOS 7 the default Apache document root directory is /var/www/html/.

Now, create two directory for websites www.vhost1.com and www.vhost2.com under Apache default document root directory.

sudo mkdir -p /var/www/html/www.vhost1.com
sudo mkdir -p /var/www/html/www.vhost2.com

Create test web pages for each virtual host

Now, you need to create an index.html file for each website which will identify that specific domain.

Let’s create an index.html file for the www.vhost1.com virtual host.

sudo nano /var/www/html/www.vhost1.com/index.html

Add the following content.

<html>

<head>
 <title>www.vhost1.com</title>
</head>

<body>
 <h1>The vhost1.com virtual host is working!</h1>
</body>

</html>

Save and close the file when you are finished.

Similarly, create av index.html file for the www.vhost2.com virtual host.

sudo nano /var/www/html/www.vhost2.com/index.html

Add the following content.

<html>

<head>
 <title>www.vhost2.com</title>
</head>

<body>
 <h1>The vhost2.com virtual host is working!</h1>
</body>

</html>

Save and close this file as well. Now, you have the pages to test the virtual host configuration.

Set up ownership and permissions

In CentOS 7 by default the Apache service runs as the user apache. You must change the ownership of these two virtual directories to apache, so that Apache can read and write data.

You can change the ownership with the chown command.

sudo chown -R apache:apache /var/www/html/www.vhost1.com
sudo chown -R apache:apache /var/www/html/www.vhost2.com

You should also make the Apache document root /var/www/html directory world readable, so that everyone can read files from that directory.

sudo chmod -R 755 /var/www/html

Now your web server has the permissions it needs to serve content.

Create virtual host configuration files

The next step is to create a virtual host configuration file for each website. The name of each configuration file must end with .conf.

Let’s create a virtual host file for the website www.vhost1.com.

sudo nano /etc/httpd/conf.d/vhost1.com.conf

Add the following content.

<VirtualHost *:80>

  ServerName www.vhost1.com
  ServerAlias vhost1.com
  DocumentRoot /var/www/html/www.vhost1.com
  ErrorLog /var/www/html/www.vhost1.com/error.log
  CustomLog /var/www/html/www.vhost1.com/access.log combined

</VirtualHost>

Save and close the file when you are finished.

Similarly, create a virtual host file for the website www.vhost2.com.

sudo nano /etc/httpd/conf.d/vhost2.com.conf

Add the following content.

<VirtualHost *:80>

  ServerName www.vhost2.com
  ServerAlias vhost2.com
  DocumentRoot /var/www/html/www.vhost2.com
  ErrorLog /var/www/html/www.vhost2.com/error.log
  CustomLog /var/www/html/www.vhost2.com/access.log combined

</VirtualHost>

When you are finished, it is a good idea to check the syntax of the configuration. 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

Test the virtual hosts

Now, you need to create DNS record for www.vhost1.com and www.vhost2.com to test the virtual hosts. One easy way is to add a “domain name to IP mapping” entry to the /etc/hosts file.

To do this, on the desktop Linux machine that you want to access the www.vhost1.com and www.vhost2.com websites from, open the /etc/hosts file:

sudo nano /etc/hosts

Add the following lines, using the static IP address of your server:

192.0.2.0 www.vhost1.com
192.0.2.0 www.vhost2.com

Save and exit the file.

Now on the desktop Linux computer, open your web browser and go to the URLs https://www.vhost1.com and https://www.vhost2.com. You should see sample pages that look like this:

www.vhost1.com sample page:

Sample page for vhost1.com

www.vhost2.com sample page:

Sample page for vhost2.com

 

Sumber: https://devops.profitbricks.com/tutorials/how-to-set-up-name-based-virtual-hosting-vhosts-with-apache-web-server-on-centos-7-1/

Leave a Reply