apache虛擬web主機構建

ZhulongQ發表於2020-11-27

一、web虛擬主機

1.1 概念

  • 虛擬Web主機
    在同一臺伺服器中執行多個Web站點,其中每一個站點並不獨立佔用一臺真正的計算機機
  • httpd支援的虛擬主機型別
    ​ 基於域名的虛擬主機
    基於IP地址的虛擬主機
    基於埠的虛擬主機
  • 環境
    CentOS 7.6
    Win 10

二、 基於域名

2.1 配置檔案

	#安裝httpd跟bind軟體包
	[root@localhost ~]# yum -y install httpd bind

	#修改主配置檔案
	[root@localhost ~]# vim /etc/named.conf 
	......
	listen-on port 53 { any; };
	......
	allow-query     { any; };
	......

	#修改區域配置檔案
	[root@localhost ~]# vim /etc/named.rfc1912.zones 
	......
	zone "test01.com" IN {
 		   type master;
		    file "test01.com.zone";
  		  allow-update { none; };
	};

	zone "test02.com" IN {
	        type master;
    	    file "test02.com.zone";
  	    allow-update { none; };
	};
	......

	#進入目錄進行後續操作
	[root@localhost ~]# cd /var/named/

	#複製區域配置模板
	[root@localhost named]# cp -p named.localhost test01.com.zone

	#修改區域配置模板
	[root@localhost named]# vim test01.com.zone
	......
	www     IN      A       20.0.0.126		===>追加到末尾

	#複製一份剛才修改好的區域配置檔案
	[root@localhost named]# cp -p test01.com.zone test02.com.zone

	#進入該目錄進行後續操作
	[root@localhost ~]# cd /etc/httpd/conf

	#修改虛擬主機配置檔案
	[root@localhost conf]# vim vhost.conf
	<VirtualHost *:80>
	  DocumentRoot "/var/www/html/test01"
	  ServerName www.test01.com
	  ErrorLog "logs/www.test01.com.error_log"
	  CustomLog "logs/www.test01.com.access_log" common
	    <Directory "/var/www/html">
	      Require all granted
	    </Directory>
	</VirtualHost>

	<VirtualHost *:80>
	  DocumentRoot "/var/www/html/test02"
	  ServerName www.test02.com
	  ErrorLog "logs/www.test02.com.error_log"
	  CustomLog "logs/www.test02.com.access_log" common
	    <Directory "/var/www/html">
   Require all granted
    </Directory>
	</VirtualHost>
		#進入站點目錄
	[root@localhost conf]# cd /var/www/html
	
	#建立對應的站點目錄
	[root@localhost html]# mkdir test01 test02

	#配置index.html測試使用
	[root@localhost html]# vim test01/index.html
		<h1>this is test01 web</h1>
		[root@localhost html]# vim test02/index.html
	<h1>this is test02 web</h1>

		#修改主配置檔案
	[root@localhost html]# vim /etc/httpd/conf/httpd.conf
	......
	Include conf/vhost.conf			===>配置檔案末尾追加這一條指向虛擬主機

	#開啟服務,關閉防火牆,關閉核心防護
	[root@localhost html]# systemctl start httpd
	[root@localhost html]# systemctl restart named
	[root@localhost html]# systemctl stop firewalld
	[root@localhost html]# setenforce 0

2.2 結果

在瀏覽器上輸入"www.test01.com"
在這裡插入圖片描述在瀏覽器上輸入"www.test02.com"
在這裡插入圖片描述

三、基於埠

3.1 配置命令

在上面基於域名的基礎上修改虛擬主機配置檔案

[root@localhost html]# vim /etc/httpd/conf/vhost.conf 
<VirtualHost *:80>
  DocumentRoot "/var/www/html/test01"
  ServerName www.test01.com
  ErrorLog "logs/www.test01.com.error_log"
  CustomLog "logs/www.test01.com.access_log" common
<Directory "/var/www/html">
  Require all granted
</Directory>
</VirtualHost>

<VirtualHost *:8080>
  DocumentRoot "/var/www/html/test02"
  ServerName www.test01.com
  ErrorLog "logs/www.test02.com.error_log"
  CustomLog "logs/www.test02.com.access_log" common
<Directory "/var/www/html">
  Require all granted
</Directory>
</VirtualHost>

#修改主配置檔案
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
......
Listen 20.0.0.126:80
Listen 20.0.0.126:8080
......

#開啟服務,關閉防火牆,關閉核心防護
[root@localhost html]# systemctl start httpd
[root@localhost html]# systemctl restart named
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0

3.2 結果

在Win10瀏覽器上面輸入"www.test01.com:8080"
在這裡插入圖片描述

四、基於IP地址

4.1 配置檔案

#修改虛擬主機配置檔案
[root@localhost conf]# vim /etc/httpd/conf/httpd.conf
<VirtualHost 20.0.0.26:80>
  DocumentRoot "/var/www/html/test01"
  ErrorLog "logs/www.test01.com.error_log"
  CustomLog "logs/www.test01.com.access_log" common
<Directory "/var/www/html">
  Require all granted
</Directory>
</VirtualHost>

<VirtualHost 20.0.0.126:80>
  DocumentRoot "/var/www/html/test02"
  ErrorLog "logs/www.test02.com.error_log"
  CustomLog "logs/www.test02.com.access_log" common
<Directory "/var/www/html">
  Require all granted
</Directory>
</VirtualHost>

#修改主配置檔案
[root@localhost conf]# vim /etc/httpd/conf/httpd.conf 
......
	Listen 20.0.0.26:80
Listen 20.0.0.126:80
......

#開啟服務,關閉防火牆,關閉核心防護
[root@localhost html]# systemctl start httpd
[root@localhost html]# systemctl restart named
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0

4.2 結果

在這裡插入圖片描述
在這裡插入圖片描述

相關文章