当前位置: 首页 > 编程文章 >

Apache虚拟主机配置详解:基于IP、端口与域名的实例

Apache虚拟主机配置详解:基于IP、端口与域名的实例

作者:陆宁时间:2026-09-13 14:51:21浏览:486

本文总结了Apache服务器中三种主要的虚拟主机配置方式,包括基于IP地址、基于IP和多端口以及基于域名的配置方法。通过具体的配置文件示例和XAMPP环境下的实验步骤,详细展示了如何修改httpd.conf和httpd-vhosts.conf文件来实现不同域名或IP的独立访问,并强调了Directory权限设置的重要性以避免访问被拒。

本文目录收起

1、基于IP地址的虚拟主机配置

复制代码 代码如下: Listen 80 DocumentRoot /home/httpd/html1 ServerName www.ok1.com ErrorLog /usr/local/apache/logs/error1_log CustomLog /usr/local/apache/logs/access1_log combined DocumentRoot /home/httpd/html2 ServerName www.ok2.com ErrorLog /usr/local/apache/logs/error2_log CustomLog /usr/local/apache/logs/access2_log combined

2、基于IP和多端口的虚拟主机配置

复制代码 代码如下: Listen 172.20.30.40:80 Listen 172.20.30.40:8080 Listen 172.20.30.50:80 Listen 172.20.30.50:8080 DocumentRoot /www/example1-80 ServerName www.example1.com DocumentRoot /www/example1-8080 ServerName www.example1.com DocumentRoot /www/example2-80 ServerName www.example1.org DocumentRoot /www/example2-8080 ServerName www.example2.org

3、单个IP地址服务器上基于域名的虚拟主机配置 复制代码 代码如下: # Ensure that Apache listens on port 80 Listen 80 # Listen for virtual host requests on all IP addresses NameVirtualHost *:80 DocumentRoot /www/example1 ServerName www.example1.com ServerAlias example1.com. *.example1.com # Other directives here DocumentRoot /www/example2 ServerName www.example2.org # Other directives here 如果您感觉上面的文章还不够详细可以看下下面的文章: 实验目标:在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,内容 -

复制代码 代码如下:


hello~, 这是[对应的网站名,用以区别].net


 

实验步骤:

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 ServerAdmin XXXXXXXX DocumentRoot "XXXXXXXX" ServerName XXXXXXX ServerAlias XXXXXX ErrorLog "logs/XXXXXX-error.log" CustomLog "logs/XXXXXXX-error.log" combined

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

复制代码 代码如下: 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 order allow,deny allow from all 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 order allow,deny allow from all

注意,如果不在各VirtualHost中定义Directory的可访问性,你将遇到的是Access Forbidden!就连原来的localhost也是。 4. 由于之前注释掉了httpd.conf中的ServerName, DocumentRoot等,为了仍然能以localhost访问原XAMPP欢迎页,就在增加一个VirtualHost,如下 -

复制代码 代码如下: ServerAdmin adm@localhost DocumentRoot "C:/xampp/htdocs" ServerName localhost ErrorLog "logs/localhost-error.log" CustomLog "logs/localhost-access.log" combined order allow,deny allow from all

为了避免出错,把它放置在第一个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 # # 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 block. #

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 order allow,deny allow from all

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 order allow,deny allow from all

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

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

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 order allow,deny allow from all

上一篇
Linux 7.4安装配置Oracle 11.2.0.4图文教程

本文详细介绍在Linux 7.4操作系统上安装和配置Oracle 11.2.0.4数据库的完整流程。内容涵盖从配置本地Yum源、关闭SELinux、安装桌面环境及必要软件包,到创建用户组、设置目录权限、调整内核参数及环境变量等关键步骤,最后通过图形界面完成安装部署。

下一篇
Linux系统下安装rz/sz命令及使用方法说明

rz和sz命令用于在本地与Linux服务器之间传输文件,但多数系统默认未安装。可通过编译源码或yum工具进行安装。sz命令用于将服务器文件发送至本地,执行后需选择保存路径;rz命令用于从本地上传文件至服务器,触发后会弹出文件选择框。

相关文章

更多
Apache实现部署多个网站(一个IP部署多域名)的方法详解

在IPv4资源有限的场景下,通过Apache实现单IP对应多站点是常见需求。本文基于Apache 2.4.7版本,详细讲解如何通过修改sites-available目录下的配置文件或新建虚拟主机文件并创建软链接至sites-enabled,来绑定不同域名和文档根目录。此外,还涵盖了启用proxy模块、配置httpd-vhosts.conf进行反向代理及域名重定向的具体步骤,以及调整访问权限以允许外部访问的设置方法。

2026.10.01 988
Apache与PHP整合配置指南

本文介绍在Windows环境下将Apache 2.2.21与PHP 5.3.8进行整合的具体步骤。主要涉及修改php.ini配置文件以设置扩展目录和时区,以及在Apache的httpd.conf中加载php5apache2_2.dll模块、声明.php文件类型及指定配置文件路径。针对引入特定模块(如curl)可能引发的依赖缺失报错,提供了安装VC运行库的解决方案。

2025.12.22 946
PHP网站物理路径泄露漏洞利用方法汇总

本文整理了多种导致PHP网站暴露服务器物理路径的常见场景。包括通过单引号注入、构造错误参数触发报错、利用Google快照搜索、访问常见的测试文件如phpinfo,以及探测phpMyAdmin特定文件获取路径。此外,还涵盖了通过读取配置文件(如php.ini、httpd.conf)和Nginx文件类型解析漏洞发现路径的方法,旨在帮助安全人员识别此类信息泄露风险。

2025.12.04 913
同一台服务器部署多个PHP站点:基于域名的虚拟主机配置

在 Nginx 中为同一 IP 配置多个 PHP 站点,核心是修改配置文件中的 server_name 并正确传递 $fastcgi_script_name 。确保每个站点的 location ~ \.php$ 块内包含 include fastcgi_params; 或 include fastcgi.conf; ,

2026.10.14 906
Windows下Apache配置PHP:修改httpd.conf加载模块步骤

在 httpd.conf 中加载 PHP 模块并配置解析,即可让 Apache 处理 .php 文件。

2025.06.17 826
Nginx虚拟主机配置详解:基于域名、端口与IP

Nginx支持基于域名、IP地址和端口的三种虚拟主机部署方式。本文详细演示了如何通过编译安装Nginx及配置DNS服务,实现不同域名指向同一服务器的独立访问;通过修改listen指令中的端口号,实现单域名多端口隔离;以及通过绑定不同IP地址,实现多IP环境下的站点分离。掌握这些配置技巧可有效提升服务器资源利用率。

2025.02.25 736

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。