• Digital accessories
  • Server
  • Digital life
  • Privacy policy
  • Contact us
  1. Home
  2. Article
  3. How to Set Up IP and Port-Based Virtual Hosting (Vhosts) With ...

How to Set Up IP and Port-Based Virtual Hosting (Vhosts) With ...

Rsdaa 24/12/2021 1714

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 hostingURL: www.ip-vhost.comIP address: 192.168.1.42Port: 80Example 2: Port-based hostingURL: www.port-vhost.com IP address: 192.168.1.43`Port: 8080

Requirements

A server running CentOS v. 7 with Apache installedA desktop machine running LinuxA 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=yesBOOTPROTO=staticIPADDR0=192.168.1.42IPADDR1=192.168.1.43NETMASK=255.255.255.0GATEWAY=192.168.1.1DNS1=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:80Listen 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.comsudo 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.

www.ip-vhost.com

The ip-vhost.com virtual host is working!

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.

www.port-vhost.com

The port-vhost.com virtual host is working!

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.comsudo 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.

ServerName www.ip-vhost.comServerAlias ip-vhost.comDocumentRoot /var/www/html/www.ip-vhost.comErrorLog /var/www/html/www.ip-vhost.com/error.logCustomLog /var/www/html/www.ip-vhost.com/access.log combined

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.

ServerName www.port-vhost.comServerAlias port-vhost.comDocumentRoot /var/www/html/www.port-vhost.comErrorLog /var/www/html/www.port-vhost.com/error.logCustomLog /var/www/html/www.port-vhost.com/access.log combined

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/tcpsudo 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:

www.port-vhost.com demo page:


PREV: How To Get Around and Bypass a Ban in Discord - Alphr

NEXT: Chapter 7 Network Information Service (NIS) (Overview ...

Popular Articles

Hot Articles

Navigation Lists

Back to Top