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 implement simple data monitoring with JS

Table of contents Overview first step Step 2 Why ...

vue_drf implements SMS verification code

Table of contents 1. Demand 1. Demand 2. SDK para...

React ref usage examples

Table of contents What is ref How to use ref Plac...

Detailed explanation of CSS3 rotating cube problem

3D coordinate concept When an element rotates, it...

What does input type mean and how to limit input

Common methods for limiting input 1. To cancel the...

js to achieve interesting countdown effect

js interesting countdown case, for your reference...

How to configure anti-hotlinking for nginx website service (recommended)

1. Principle of Hotlinking 1.1 Web page preparati...

Detailed steps to use Redis in Docker

1. Introduction This article will show you how to...

Simple summary of tomcat performance optimization methods

Tomcat itself optimization Tomcat Memory Optimiza...

MySQL export of entire or single table data

Export a single table mysqldump -u user -p dbname...

Question about custom attributes of html tags

In previous development, we used the default attr...

Node.js+express message board function implementation example

Table of contents Message Board Required librarie...

MySQL horizontal and vertical table conversion operation implementation method

This article uses examples to illustrate how to i...