網站首頁 編程語言 正文
記事本打開httpd.conf文件 ,該文件在apache的目錄下,如: D:\AppServ\Apache2.2\conf,修改如下兩處:
LoadModule vhost_alias_module modules/mod_vhost_alias.so //去掉前面的#,意思是啟用apache的虛擬主機功能,第203行
Include conf/extra/httpd-vhosts.conf //去掉#的意思是從httpd-vhosts.conf這個文件導入虛擬主機配置
配置虛擬主機后 不能用localhost 訪問
只需要把httpd.conf文件的ServerName localhost:80 那行注釋掉 就可以了
一、基于IP
假設服務器有個IP地址為192.168.1.10,使用ifconfig在同一個網絡接口eth0上綁定3個IP:
[root@localhost root]# ifconfig eth0:1 192.168.1.11
[root@localhost root]# ifconfig eth0:2 192.168.1.12
[root@localhost root]# ifconfig eth0:3 192.168.1.13
修改hosts文件,添加三個域名與之一一對應:
192.168.1.11 www.test1.com
192.168.1.12 www.test2.com
192.168.1.13 www.test3.com
建立虛擬主機存放網頁的根目錄,如在/www目錄下建立test1、test2、test3文件夾,其中分別存放1.html、2.html、3.html
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
在httpd.conf中將附加配置文件httpd-vhosts.conf包含進來,接著在httpd-vhosts.conf中寫入如下配置:
<VirtualHost 192.168.1.11:80> ServerName www.test1.com DocumentRoot /www/test1/ <Directory "/www/test1"> ? Options Indexes FollowSymLinks ?AllowOverride None ?Order allow,deny ? Allow From All ? ?</Directory> </VirtualHost> <VirtualHost 192.168.1.12:80> ServerName www.test1.com DocumentRoot /www/test2/ <Directory "/www/test2"> ? Options Indexes FollowSymLinks ?AllowOverride None ?Order allow,deny ? Allow From All ? ?</Directory> </VirtualHost> <VirtualHost 192.168.1.13:80> ServerName www.test1.com DocumentRoot /www/test3/ <Directory "/www/test3"> ? Options Indexes FollowSymLinks ?AllowOverride None ?Order allow,deny ? Allow From All ? ?</Directory> </VirtualHost>
大功告成,測試下每個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com
二、基于主機名
設置域名映射同一個IP,修改hosts:
127.0.0.1 gm.998gx.com
127.0.0.1 www.998gx.com
127.0.0.1 r.998gx.com
127.0.0.1 localhost
跟上面一樣,建立虛擬主機存放網頁的根目錄
/www/dxGM/index.php
/www/dxskadmin/index.php
/www/88qp/index.php
在httpd.conf中將附加配置文件httpd-vhosts.conf包含進來,接著在httpd-vhosts.conf中寫入如下配置:
為了使用基于域名的虛擬主機,必須指定服務器IP地址(和可能的端口)來使主機接受請求。可以用NameVirtualHost指令來進行配置。 如果服務器上所有的IP地址都會用到, 你可以用*作為NameVirtualHost的參數。在NameVirtualHost指令中指明IP地址并不會使服務器自動偵聽那個IP地址。 這里設定的IP地址必須對應服務器上的一個網絡接口。
下一步就是為你建立的每個虛擬主機設定配置塊,的參數與NameVirtualHost指令的參數是一樣的。每個定義塊中,至少都會有一個ServerName指令來指定伺服哪個主機和一個DocumentRoot指令來說明這個主機的內容存在于文件系統的什么地方。
如果在現有的web服務器上增加虛擬主機,必須也為現存的主機建造一個定義塊。其中ServerName和DocumentRoot所包含的內容應該與全局的保持一致,且要放在配置文件的最前面,扮演默認主機的角色。
<VirtualHost *:80>? ? ? DocumentRoot "D:/phpstudy/WWW/dxGM"? ? ? ServerName gm.998gx.com? </VirtualHost> <VirtualHost *:80>? ? ? DocumentRoot "D:/phpstudy/WWW/88qp"? ? ? ServerName www.998gx.com? </VirtualHost> <VirtualHost *:80>? ? ? DocumentRoot "D:/phpstudy/WWW/dxskadmin"? ? ? ServerName r.998gx.com? </VirtualHost> <VirtualHost *:80>? ? ? DocumentRoot "D:/phpstudy/WWW"? ? ? ServerName localhost? </VirtualHost>
4. 大功告成,測試下每個虛擬主機,分別訪問gm.998gx.com、www.998gx.com、r.998gx.com
三、基于端口
修改配置文件
將原來的
Listen 80
改為
Listen 80
Listen 8080
更改虛擬主機設置:
<VirtualHost 192.168.1.10:80> ? ? DocumentRoot /var/www/test1/ ? ? ServerName www.test1.com </VirtualHost> <VirtualHost 192.168.1.10:8080> ? ? DocumentRoot /var/www/test2 ? ? ServerName www.test2.com </VirtualHost>
原文鏈接:https://blog.csdn.net/qq_38083665/article/details/80720226
相關推薦
- 2022-12-02 C語言#define定義宏的使用詳解_C 語言
- 2022-06-23 C#中的小數和百分數計算與byte數組操作_C#教程
- 2022-09-14 python與xml數據的交互詳解_python
- 2022-04-11 一篇文章帶你入門python之推導式_python
- 2023-05-31 E:?無法定位軟件包?python3-pip問題及解決_python
- 2022-06-17 C#中Abstract方法和Virtual方法的區別_C#教程
- 2022-11-11 Python深度學習之FastText實現文本分類詳解_python
- 2022-05-23 iOS實現垂直滑動條效果_IOS
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支