如何配置apache虚拟主机

实验目标:在apache实现基于域名的虚拟主机

实验用的XAMPP版本为1.7.7,内含apache版本为2.2.21

实验前准备:

1. 为了测试不同的域名,在Windows/System32/drivers/etc/下觅得hosts文件,在其中添加实验用的域名若干,如 -

127.0.0.1   test1.net

127.0.0.1   test2.net

如此,则在浏览器中输入该俩域名时,Windows将其解析为127.0.0.1本地地址。即,在浏览器中访问localhost, test1.net, test2.net均可访问XAMPP的欢迎页。

2. 在apache目录下建立目录,以放置您不同的网站。为保护XAMPP原有的htdocs中的欢迎页内容,实验另外建立了与htdocs平级的htdocs1目录,在其下建立了test1.net, test2.net两个子目录用以放置实验用的网站。如下 -

apache/htdocs1/test1.net - 放置test1.net网站内容

apache/htdocs1/test2.net - 放置test2.net网站内容

在这两个目录中各新建hello world一网页 index.html,内容 -

< HTML >
< HEAD ></ HEAD >
< BODY >
< H1 >hello~, 这是[对应的网站名,用以区别].net </ H1 ></ BODY >
</ HTML >  

实验步骤:

1. 找到apache/conf/httpd.conf, 将其中的

ServerAdmin

ServerName

DocumentRoot

注释掉。

2. 在httpd.conf中,找到行

Include "conf/extra/httpd-vhosts.conf"

如被注释则解注。该文件记载了虚拟主机的参数。[以前虚拟主机参数是直接填写在httpd.conf中的,为了更好地组织文件,将其分离出去,类似于某些编程语言一样。因此httpd.conf中include它,即相当于把它的内容填在了httpd.conf中。]

3. 这个httpd-vhosts.conf文件格式基本如下 -

#blah-blah
NameVirtualHost *:80

#blah-blah
#blah-blah
< VirtualHost  *:80 >
    ServerAdmin XXXXXXXX
    DocumentRoot "XXXXXXXX"
    ServerName XXXXXXX
    ServerAlias XXXXXX
    ErrorLog "logs/XXXXXX-error.log"
    CustomLog "logs/XXXXXXX-error.log" combined   
</ VirtualHost >

需要修改的,就是<VirtualHost>中的参数了。这个可以参见apache官方文档。根据实验域名,可以增加两个<VirtualHost>:

< VirtualHost  *:80 >
    ServerAdmin adm@test1.net
    DocumentRoot "C:/xampp/htdocs1/test1.net"
    ServerName test1.net
    ServerAlias www.test1.net
    ErrorLog "logs/test1-error.log"
    CustomLog "logs/test1-access.log" combined
    
     < Directory  "C:/xampp/htdocs1/test1.net" >
    order allow,deny
    allow from all
     </ Directory >    
</ VirtualHost >

< VirtualHost  *:80 >
    ServerAdmin adm@test2.net
    DocumentRoot "C:/xampp/htdocs1/test2.net"
    ServerName test2.net
    ServerAlias www.test2.net
    ErrorLog "logs/test1-error.log"
    CustomLog "logs/test1-access.log" combined
    
     < Directory  "C:/xampp/htdocs1/test2.net" >
    order allow,deny
    allow from all
     </ Directory >    
</ VirtualHost >

注意,如果不在各VirtualHost中定义Directory的可访问性,你将遇到的是Access Forbidden!就连原来的localhost也是。

4. 由于之前注释掉了httpd.conf中的ServerName, DocumentRoot等,为了仍然能以localhost访问原XAMPP欢迎页,就在增加一个VirtualHost,如下 -

< VirtualHost  *:80 >
    ServerAdmin adm@localhost
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
    
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" combined
    
     < Directory  "C:/xampp/htdocs" >
    order allow,deny
    allow from all
     </ Directory >    
</ VirtualHost >

为了避免出错,把它放置在第一个Virtualhost位置。

至此,apache基于域名的虚拟主机配置完成。可以通过http://localhost访问XAMPP欢迎页,通过http://test1.net和http://test2.net访问各自的主页。

#
# Virtual Hosts
#

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs"
    ServerName localhost
    ServerAlias www.skydao.com
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" combined
    
    <Directory "E:/skydao/apache2/htdocs">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/project1"
    ServerName project1.com
    ServerAlias www.project1.com
    ErrorLog "logs/project1-error.log"
    CustomLog "logs/project1-access.log" combined
    
    <Directory "E:/skydao/apache2/htdocs/project1">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/zendTest/public"
    ServerName zendTest.com
    ServerAlias www.zendTest.com
    DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>    
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/testRewrite"
    ServerName testRewrite.com
    ServerAlias www.testRewrite.com
    # DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>    
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot "E:/skydao/apache2/htdocs/test"
    ServerName test.com
    ServerAlias www.test.com
    ErrorLog "logs/zendTest-error.log"
    CustomLog "logs/zendTest-access.log" combined
    
    <Directory "E:/skydao/apache2/htdocs/test">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>

如何配置apache虚拟主机相关推荐

  1. 如何配置Apache虚拟主机?(基于IP、基于端口、基于域名)

    一.Apache虚拟机配置前的准备工作 1.下载yum源 2.安装yum包 3.安装httpd包 4.查看并关闭selinux 5.取消中心主机 cd /etc/httpd/conf vim http ...

  2. 在CentOS 8上安装与配置Apache虚拟主机

    实验环境 操作系统:Centos 8 web应用:apache 内网IP:192.168.3.21 shell执行:root 以root或具有sudo权限的用户身份登录执行如下操作. 主配置文件路径: ...

  3. Linux下配置Apache虚拟主机(基于IP地址)

    Linux下配置Apache虚拟主机(基于IP地址) 一.虚拟主机 虚拟主机,又称虚拟服务器,是一种在单一主机或主机群上,实现多网域服务的方法,可以运行多个网站或服务的技术.虚拟主机之间完全独立,并可 ...

  4. Ubuntu系统下配置Apache虚拟主机

    本文已在本人博客https://www.nsxsg.com/archives/125首发 文章目录 Ubuntu系统下配置Apache虚拟主机 说明 配置文件 配置多个虚拟主机 Ubuntu系统下配置 ...

  5. CentOS下配置apache虚拟主机

    1.apache虚拟主机配置: (1)基于域名的配置(同一个IP不同域名)方法:    ①添加域名到hosts文件中, 命令vim /etc/hosts 添加的内容: 192.168.142.140 ...

  6. 配置apache虚拟主机

    1,让apache服务器处于调试状态,以便对httpd.conf进行更改后,只要保存就立即生效 2,打开重写模块 3,虚拟路径测试 4,添加默认路径 5,结束,此文不研究.htaccess的重写 详细 ...

  7. Linux配置apache虚拟主机:静态文件

    1.win访问linux:在win的hosts文件中添加 #配置lanmp 192.168.106.141 www.cd.pro测试: ping 192.168.106.141应该可以ping通 2. ...

  8. Debian配置Apache虚拟主机(IP、端口、域名)

    apache2有三种方式实现虚拟主机,IP.端口和域名.用户输入不同的IP.端口.域名.访问到的就会是不同的站点. 域名方式: 服务器需要搭建DNS服务,本文省略搭建DNS服务的过程. 在/var/w ...

  9. mac下配置apache虚拟主机

    为什么要配置虚拟主机 访问web服务,本质上看(从协议上)是访问某个IP的主机上的某个端口(默认是80) 通常需要通过访问不同的域名或者端口实现对不同网站的访问(具体到服务器里就是不同目录),这个时候 ...

最新文章

  1. C语言 数组(做个笔记)
  2. 使用深度神经网络进行自动呼叫评分(二)
  3. ELF文件的加载和动态链接过程
  4. 皮一皮:这是结婚还是华山论剑...
  5. python语言程序设计实践教程上海交通大学_《C语言实验与等级考试指导》隋雪莉 闵 芳 沈国荣著【摘要 书评 在线阅读】-苏宁易购图书...
  6. 美团医美发起“至美行动”,单月拦截六万余条虚假医美评价
  7. 关于 jsp java servlet 中文汉字乱码的解决方法
  8. ajax 服务器响应,ajax-服务器响应
  9. vbScript: 编号成生不夠位數前面加零
  10. 矿机 矿场 矿池 关系和区别
  11. IMDB-WIKI人脸属性数据集解析,dob matlab序列号转为出生日期
  12. Android实现蝴蝶动画,抖音蝴蝶特效怎么弄的?抖音最近很火的蝴蝶特效制作|安卓手机给视频添加蝴蝶特效...
  13. Python3.x:第三方库简介
  14. numpy多维数组shape的理解
  15. 淘宝联盟实现第三方用户关联返佣订单的解决方案
  16. 让Windows 时间与Internet 时间服务器同步
  17. 【word】分节符的作用
  18. android开发 硬件加速,Android 开发之硬件加速
  19. STM32F103 485通信开发实例(二):与HMI建立初步通信
  20. 软件测试计划分哪五大块,[liu yanling]软件测试分为哪几个计划过程阶段

热门文章

  1. 华为模拟器eNSP基本命令
  2. 理解分布式一致性:Paxos协议之Multi-Paxos
  3. 将N阶矩阵M置成单位阵
  4. Docker国内镜像源设置
  5. 机器学习(三):线性模型
  6. 为啥一定要用残差图检查你的回归分析?
  7. select下拉框(支持筛选、多选)
  8. python选择哪个版本-Python下载哪个版本比较好?
  9. js-判断输入值是不是数字、字符串
  10. CodeForces 332B Maximum Absurdity