Three ways to implement virtual hosts under Linux7

Three ways to implement virtual hosts under Linux7

1. Same IP address, different port numbers

Virtual host 1: The host IP address is 172.16.30.20, the port number is 80 (default port), the DocumentRoot is /var/www/vhost1, and the virtual host site homepage file is created under the DocumentRoot directory.

[root@rhel7 ~]# cd /var/www/
[root@rhel7 www]# mkdir vhost1
[root@rhel7 www]# cd vhost1/
[root@rhel7 vhost1]# vim index.html
[root@rhel7 vhost1]# cat index.html
this is the vhost1

Virtual host 2: The host IP address is 172.16.30.20, the port number is 8080, the DocumentRoot is /var/www/vhost2, and the virtual host site homepage file is created under the DocumentRoot directory.

[root@rhel7 ~]# cd /var/www/
[root@rhel7 www]# mkdir vhost2
[root@rhel7 www]# cd vhost2/
[root@rhel7 vhost2]# vim index.html
[root@rhel7 vhost2]#
[root@rhel7 vhost2]# cat index.html
this is the vhost2

Edit the virtual machine host configuration file httpd-vhosts.conf

[root@rhel7 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost 172.16.30.10:80>
  DocumentRoot "/var/www/vhost1"
</VirtualHost>
Listen 8080
<VirtualHost 172.16.30.10:8080>
  DocumentRoot "/var/www/vhost2"
</VirtualHost>
[root@rhel7 ~]# systemctl restart httpd
[root@rhel7 ~]# firewall-cmd --add-port=8080/tcp --permanent
success
[root@rhel7 ~]# firewall-cmd --reload
success

Access Test:


2. Different IP addresses, same port number

Virtual host 1: The host IP address is 172.16.30.20, the port number is 80 (default port), the DocumentRoot is /var/www/vhost1, and the virtual host site homepage file is created under the DocumentRoot directory.

Virtual host 2: The host IP address is 172.16.30.200, the port number is 80 (default port), the DocumentRoot is /var/www/vhost2, and the virtual host site homepage file is created under the DocumentRoot directory.

[root@rhel7 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=bde41fa3-f559-4de2-ba9a-857fed211aac
DEVICE=ens33
ONBOOT=yes
DNS1=127.0.0.1
ZONE=public
IPADDR=172.16.30.10
PREFIX=24
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPADDR1=172.16.30.100
PREFIX1=24
[root@rhel7 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost 172.16.30.10:80>
  DocumentRoot "/var/www/vhost1"
</VirtualHost>

<VirtualHost 172.16.30.100:80>
  DocumentRoot "/var/www/vhost2"
</VirtualHost>

[root@rhel7 ~]# systemctl restart httpd

Access Test:



3. Same IP address, same port number, different FQDN

Virtual host 1: The host IP address is 172.16.30.20, the port number is 80 (default port), the FQDN is vhost1.example.com, the DocumentRoot is /var/www/vhost1, and the virtual host site homepage file is created in the DocumentRoot directory.

Virtual host 2: The host IP address is 172.16.30.20, the port number is 80 (default port), the FQDN is vhost2.example.com, the DocumentRoot is /var/www/vhost2, and the virtual host site homepage file is created in the DocumentRoot directory.

[root@rhel7 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost 172.16.30.10:80>
  DocumentRoot "/var/www/vhost1"
  ServerName vhost1.example.com
</VirtualHost>
<VirtualHost 172.16.30.10:80>
  DocumentRoot "/var/www/vhost2"
  ServerName vhost2.example.com
</VirtualHost>

Note: The corresponding resolution content needs to be added in the DNS server.


Access Test:


Remember to restart the service every time you modify the configuration file.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Perfect solution for not being able to delete virtual host files or folders under Linux server
  • In-depth introduction to the method of configuring virtual hosts for Apache multi-port in Linux environment
  • Linux virtual host panel kloxo installation and Chinese tutorial sharing
  • How to configure virtual host under linux apache

<<:  How to view mysql binlog (binary log)

>>:  Solve the cross-domain problem of get and post requests of vue $http

Recommend

How to handle super large form examples with Vue+ElementUI

Recently, due to business adjustments in the comp...

How to generate Vue user interface by dragging and dropping

Table of contents Preface 1. Technical Principle ...

HTML simple web form creation example introduction

<input> is used to collect user information ...

Detailed steps for developing Java payment interface for Alipay

Table of contents first step Step 2 Step 3 Step 4...

How to set static IP in CentOS7 on VirtualBox6 and what to note

Install CentOS 7 after installing VirtualBox. I w...

Detailed tutorial on installing mysql 8.0.20 on CentOS7.8

1. Install MySQL software Download and install My...

Graphic tutorial on installing CentOS7 on VMware 15.5

1. Create a new virtual machine in VMware 15.5 1....

MySQL 5.6.23 Installation and Configuration Environment Variables Tutorial

This article shares the installation and configur...

Introduction to general_log log knowledge points in MySQL

The following operation demonstrations are all ba...

HTML weight loss Streamline HTML tags to create web pages

HTML 4 HTML (not XHTML), MIME type is text/html, ...

Summary of essential knowledge points for MySQL query optimization

Preface Query optimization is not something that ...

MySQL 8.0.23 free installation version configuration detailed tutorial

The first step is to download the free installati...

In-depth analysis of Nginx virtual host

Table of contents 1. Virtual Host 1.1 Virtual Hos...