Tutorials

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

Table of Contents

Introduction

Virtual hosting is a method for hosting multiple websites on a single machine. There are two types of virtual hosting: Name-based virtual hosting and IP-based virtual hosting. IP-based virtual hosting is a technique to apply different directives based on the IP address and port a request is received on. You can assign a separate IP for each website on a single server using IP-based virtual hosting. This is mainly used to host different websites on different ports or IP addresses.

In this article we will be creating:

Example 1: IP-based hosting
URL: www.ip-vhost.com
IP address: 192.168.1.42
Port: 80

Example 2: Port-based hosting
URL: www.port-vhost.com 
IP address: 192.168.1.43`
Port: 8080

Requirements

  • A server running CentOS v. 7 with Apache installed
  • A desktop machine running Linux
  • A static IP address for each site you want to host.

Set up multiple IP addresses on a single network interface

To set up IP-based virtual hosting, you need to have more than one IP address assigned to your server. Setting up multiple IP addresses on a single network interface is called "IP aliasing." It is very useful, particularly if your server only has one network interface card (NIC).

To set up multiple IPs, you need to edit the ifcfg-eth0 file:

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0

Add/edit the following lines:

DEVICE="eth0"
ONBOOT=yes
BOOTPROTO=static
IPADDR0=192.168.1.42
IPADDR1=192.168.1.43
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8

Save and close the file when you are finished. Then restart the network service to make these changes take effect.

sudo service network restart

Set up multiple instances of Apache

By default Apache listens for incoming connections on port 80. For port-based virtual hosting, you need to tell Apache to listen for IP address 192.168.1.42 on port 80 and for IP address 192.168.1.43 on port 8080.

To set up multiple ports, you need to edit the httpd.conf file:

sudo nano /etc/httpd/conf/httpd.conf

Add/edit the following lines:

Listen 192.168.1.42:80
Listen 192.168.1.43:8080

Save and close the file, then restart Apache to make these changes take effect.

sudo systemctl restart httpd

Create the directory structure

First, you need to make a directory structure which will hold the web pages. This directory is known as "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.ip-vhost.com and www.port-vhost.com in the default Apache document root directory:

sudo mkdir -p /var/www/html/www.ip-vhost.com
sudo mkdir -p /var/www/html/www.port-vhost.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.ip-vhost.com ip virtual host.

sudo nano /var/www/html/www.ip-vhost.com/index.html

Add the following content.

<html>

<head>
  <title>www.ip-vhost.com</title>
</head>

<body>
  <h1>The ip-vhost.com virtual host is working!</h1>
</body>

</html>

Save and close the file when you are finished.

Similarly, create an index.html file for the www.port-vhost.com virtual host.

sudo nano /var/www/html/www.port-vhost.com/index.html

Add the following content.

<html>

<head>
  <title>www.port-vhost.com</title>
</head>

<body>
  <h1>The port-vhost.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 chown command.

sudo chown -R apache:apache /var/www/html/www.ip-vhost.com
sudo chown -R apache:apache /var/www/html/www.port-vhost.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 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 website www.ip-vhost.com.

sudo nano /etc/httpd/conf.d/ip-vhost.com.conf

Add the following content.

<VirtualHost 192.168.1.42:80>

  ServerName www.ip-vhost.com
  ServerAlias ip-vhost.com
  DocumentRoot /var/www/html/www.ip-vhost.com
  ErrorLog /var/www/html/www.ip-vhost.com/error.log
  CustomLog /var/www/html/www.ip-vhost.com/access.log combined

</VirtualHost>

Save and close the file when you are finished.

Similarly, create a virtual host file for website www.port-vhost.com.

sudo nano /etc/httpd/conf.d/port-vhost.com.conf

Add the following content.

<VirtualHost 192.168.1.42:8080>

  ServerName www.port-vhost.com
  ServerAlias port-vhost.com
  DocumentRoot /var/www/html/www.port-vhost.com
  ErrorLog /var/www/html/www.port-vhost.com/error.log
  CustomLog /var/www/html/www.port-vhost.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

Allow Apache through the firewall

Now, you need to allow the Apache port 80 and 8080 using FirewallD.

You can do this by running following commands:

sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=8080/tcp

Now, reload the firewall service for the changes to take effect.

sudo firewall-cmd --reload

Test the virtual hosts

Now on the desktop Linux computer, open your web browser and go to the URLs http://192.168.1.42:80 and http://192.168.1.43:8080. You should see sample pages that look like this:

www.ip-vhost.com sample page:

Sample page for vhost 1

www.port-vhost.com demo page:

Sample page for vhost 2

 
  • Hey, Here you will be glad to know that here we are having one of the best portal how to check computer specs windows 10 which will help you to know about ways for knowing computer specs in windows 10.

Log In, Add a Comment