apache基于端口的虚拟主机核心是显式监听多个非标准端口(如8080、8081),为每个端口配置独立documentroot、directory权限(require all granted)及日志路径,并通过apachectl configtest验证后reload生效。

Apache 配置基于端口的虚拟主机,核心是让服务器监听多个非标准 HTTP 端口(如 8080、8081),并为每个端口绑定独立的网站根目录和访问控制。它不依赖域名或 IP,只靠用户显式输入端口号区分站点,适合内网管理后台、测试环境或多项目并行开发。
1. 添加监听端口
编辑主配置文件(常见路径:/etc/httpd/conf/httpd.conf 或 /etc/apache2/apache2.conf),在文件开头或 Listen 区域添加你要使用的端口:
Listen 8080Listen 8081- 如有防火墙(如 firewalld 或 ufw),需同步放行这些端口
2. 创建网站目录与测试页
为每个端口准备独立的文档根目录,并写入简单页面便于验证:
sudo mkdir -p /var/www/site8080 /var/www/site8081echo "<h1>Site on port 8080</h1>" > /var/www/site8080/index.htmlecho "<h1>Site on port 8081</h1>" > /var/www/site8081/index.html
3. 编写 VirtualHost 配置
新建配置文件(如 /etc/apache2/sites-available/port-vhosts.conf 或 /etc/httpd/conf.d/port-vhosts.conf),内容如下:
Apache Superset 是一个广泛采用的开源 BI 平台,用于 SQL 探索、图表构建和仪表板交付。当代理需要查询仓库数据、组装仪表板或使用成熟的分析界面解释指标而不是临时笔记本代码时,此技能非常有用。
<virtualhost>
DocumentRoot "/var/www/site8080"
ErrorLog "${APACHE_LOG_DIR}/site8080-error.log"
CustomLog "${APACHE_LOG_DIR}/site8080-access.log" common
<directory>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</directory></virtualhost><virtualhost>
DocumentRoot "/var/www/site8081"
ErrorLog "${APACHE_LOG_DIR}/site8081-error.log"
CustomLog "${APACHE_LOG_DIR}/site8081-access.log" common
<directory>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</directory></virtualhost>
注意:Apache 2.4+ 必须包含 <directory></directory> 块并使用 Require all granted,否则会返回 403 错误。
4. 启用配置并重启服务
根据系统类型执行对应操作:
- Debian/Ubuntu:
sudo a2ensite port-vhosts.conf && sudo systemctl reload apache2 - RHEL/CentOS:
sudo systemctl reload httpd(确保配置已放入conf.d/目录) - 检查语法:
sudo apachectl configtest,无报错再重启
完成后,浏览器访问 http://your-server-ip:8080 和 http://your-server-ip:8081 即可看到不同页面。










