首页 > 其他分享 >Apache 配置虚拟主机三种方式

Apache 配置虚拟主机三种方式

时间:2022-11-30 12:04:44浏览次数:42  
标签:test1 www test2 虚拟主机 ServerName 三种 DocumentRoot Apache com

一、基于IP

 1. 假设服务器有个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

 2. 修改hosts文件,添加三个域名与之一一对应:

192.168.1.11   www.test1.com
192.168.1.12   www.test2.com
192.168.1.13   www.test3.com

 3. 建立虚拟主机存放网页的根目录,如在/www目录下建立test1、test2、test3文件夹,其中分别存放1.html、2.html、3.html

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html

 

 4. 在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>


 5. 大功告成,测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com

 

二、基于主机名

 1. 设置域名映射同一个IP,修改hosts:

192.168.1.10  www.test1.com
192.168.1.10  www.test2.com
192.168.1.10  www.test3.com

 2. 跟上面一样,建立虚拟主机存放网页的根目录

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html

 

 

  下一步就是为你建立的每个虚拟主机设定<VirtualHost>配置块,<VirtualHost>的参数与NameVirtualHost指令的参数是一样的。每个<VirtualHost>定义块中,至少都会有一个ServerName指令来指定伺服哪个主机和一个DocumentRoot指令来说明这个主机的内容存在于文件系统的什么地方。

  如果在现有的web服务器上增加虚拟主机,必须也为现存的主机建造一个<VirtualHost>定义块。其中ServerName和DocumentRoot所包含的内容应该与全局的保持一致,且要放在配置文件的最前面,扮演默认主机的角色。


NameVirtualHost *:80
<VirtualHost *:80>  

  ServerName *

  DocumentRoot /www/ 

</VirtualHost>

<VirtualHost *: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 *:80>

  ServerName www.test2.com

  DocumentRoot /www/test2/

  <Directory "/www/test2">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  </Directory>

<VirtualHost *:80>

  ServerName www.test3.com

  DocumentRoot /www/test3/

  <Directory "/www/test3">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  </Directory>


 4. 大功告成,测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com

 

三、基于端口

1.  修改配置文件

  将原来的

    Listen 80
      改为
      Listen 80
      Listen 8080

2. 更改虚拟主机设置:


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


源码面前,了无秘密

标签:test1,www,test2,虚拟主机,ServerName,三种,DocumentRoot,Apache,com
From: https://blog.51cto.com/zhenghongxin/5898202

相关文章

  • C#中声音播放的三种方式
    C#中声音的播放主要有三种方法:1.使用API函数。2.使用SoundPlayer类播放。3.使用DirectX进行播放。一、使用API函数进行播放。 windows操作系统中的winmm.dll文件中......
  • Apache Flink 误用之痛
    摘要:本文根据FlinkForward全球在线会议·中文精华版整理而成,围绕着项目的开始、需求分析、开发,以及测试、上线、运维整个生命周期展开,介绍了ApacheFlink实践中的一......
  • 免费虚拟主机和免费云服务器申请
    出于学习原因,想找到一台配置低的免费云服务器,阿里,京东,腾讯免费云服务器需要企业认证,偶然找到了这个云平台,个人账号就可以申请免费虚拟主机和免费云服务器网站地址: https:......
  • json字符串转json对象三种方式
    //1,js自带的eval函数,其中需要添加小括号eval('('+str+')');functionstrToJson(str){varjson=eval('('+str+')');returnjson;}//2,newFunction形式fun......
  • Apache ab 开始
    下载Download-TheApacheHTTPServerProjectFilesforMicrosoftWindowsApacheHausSHA1Checksum   选项包括:-n个请求要执行的请求数-c并发......
  • Apache Pulsar 社区年度峰会 Pulsar Summit Asia 2022 即将召开
    关于 PulsarSummitPulsarSummit 是 ApachePulsar 社区年度盛会,它将分布在世界各地的 ApachePulsar 项目 Contributor、Committer 和各企业 CTO/CIO、开发者、......
  • 使用Apache PDFBox实现拆分、合并PDF
    目录使用ApachePDFBox实现拆分、合并PDF问题背景ApachePDFBox介绍拆分PDF合并PDF拆分+合并完整代码参考:使用ApachePDFBox实现拆分、合并PDF问题背景如何拆分PDF?......
  • Spring Scheduled 三种定时方式
    SpringSchedule配置时间的三种方式fixRate:间隔时间,会根据首次执行时间预先安排固定的时间,错过立即执行fixDelay:间隔时间,上次结束时间后多少间隔后执行cron:指定......
  • 面试官:介绍一下 Redis 三种集群模式
    小码今天去面试。面试官:给我介绍一下Redis集群,小码:啊,平时开发用的都是单机Redis,没怎么用过集群了。面试官:好的,出门右转不谢。小码内心困惑:在小公司业务量也不大,单机的......
  • Java基础_Spring三种构造方法
    第一种构造方法接口:FirstDaopackagecom.leehl.springgitider.dao;publicinterfaceFirstDao{publicvoidsave();}主方法:FirstDaoimplpacka......