Apache虚拟主机设置(基于端口、IP、域名)
封面图
•
2025-11-25
•
APACHE
231
0
0
0
Apache 是一个非常流行的 Web 服务器软件,支持多种虚拟主机(Virtual Host)配置方式。常见的虚拟主机配置包括: 1. **基于端口(Port-based Virtual Hosting)** 2. **基于 IP 地址(IP-based Virtual Hosting)** 3. **基于域名(Name-based Virtual Hosting)** 下面分别介绍这三种方式的配置方法。 --- ## 一、基于端口的虚拟主机(Port-based) ### 配置步骤: 1. **修改 Apache 的监听端口** 在 `httpd.conf` 或 `ports.conf` 中设置多个监听端口: ```apache Listen 80 Listen 8080 Listen 8081 ``` 2. **创建虚拟主机配置文件** 在 `/etc/apache2/sites-available/` 目录下创建两个配置文件: - `001-port-80.conf` - `002-port-8080.conf` 示例:`001-port-80.conf` ```apache <VirtualHost *:80> ServerAdmin admin@example.com DocumentRoot /var/www/html/port80 ServerName example.com ServerAlias www.example.com <Directory /var/www/html/port80> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> ``` 示例:`002-port-8080.conf` ```apache <VirtualHost *:8080> ServerAdmin admin@example.com DocumentRoot /var/www/html/port8080 ServerName example.com ServerAlias www.example.com <Directory /var/www/html/port8080> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error8080.log CustomLog ${APACHE_LOG_DIR}/access8080.log combined </VirtualHost> ``` 3. **启用虚拟主机** ```bash a2ensite 001-port-80.conf a2ensite 002-port-8080.conf ``` 4. **重启 Apache** ```bash systemctl restart apache2 ``` 5. **访问测试** - `http://localhost:80` - `http://localhost:8080` --- ## 二、基于 IP 地址的虚拟主机(IP-based) ### 配置步骤: 1. **为服务器分配多个 IP 地址** 确保你的服务器有多个 IP 地址,比如: - 192.168.1.100 - 192.168.1.101 2. **配置虚拟主机** 创建两个虚拟主机配置文件: - `001-ip-192.168.1.100.conf` - `002-ip-192.168.1.101.conf` 示例:`001-ip-192.168.1.100.conf` ```apache <VirtualHost 192.168.1.100:80> ServerAdmin admin@example.com DocumentRoot /var/www/html/ip100 ServerName example.com ServerAlias www.example.com <Directory /var/www/html/ip100> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/ip100-error.log CustomLog ${APACHE_LOG_DIR}/ip100-access.log combined </VirtualHost> ``` 示例:`002-ip-192.168.1.101.conf` ```apache <VirtualHost 192.168.1.101:80> ServerAdmin admin@example.com DocumentRoot /var/www/html/ip101 ServerName example.com ServerAlias www.example.com <Directory /var/www/html/ip101> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/ip101-error.log CustomLog ${APACHE_LOG_DIR}/ip101-access.log combined </VirtualHost> ``` 3. **启用并重启 Apache** ```bash a2ensite 001-ip-192.168.1.100.conf a2ensite 002-ip-192.168.1.101.conf systemctl restart apache2 ``` 4. **访问测试** - `http://192.168.1.100` - `http://192.168.1.101` --- ## 三、基于域名的虚拟主机(Name-based) 这是最常用的方式,适用于多个域名共享同一个 IP 地址的情况。 ### 配置步骤: 1. **确保 DNS 解析正确** 域名需要指向服务器的 IP 地址。 2. **配置虚拟主机** 创建两个虚拟主机配置文件: - `001-example.com.conf` - `002-test.com.conf` 示例:`001-example.com.conf` ```apache <VirtualHost *:80> ServerAdmin admin@example.com DocumentRoot /var/www/html/example.com ServerName example.com ServerAlias www.example.com <Directory /var/www/html/example.com> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined </VirtualHost> ``` 示例:`002-test.com.conf` ```apache <VirtualHost *:80> ServerAdmin admin@test.com DocumentRoot /var/www/html/test.com ServerName test.com ServerAlias www.test.com <Directory /var/www/html/test.com> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/test.com-error.log CustomLog ${APACHE_LOG_DIR}/test.com-access.log combined </VirtualHost> ``` 3. **启用并重启 Apache** ```bash a2ensite 001-example.com.conf a2ensite 002-test.com.conf systemctl restart apache2 ``` 4. **访问测试** - `http://example.com` - `http://test.com` --- ## 补充说明: - 如果使用的是 **Ubuntu/Debian**,通常使用 `a2ensite` 和 `a2dissite` 来管理站点。 - 如果使用的是 **CentOS/RHEL**,则直接在 `/etc/httpd/conf.d/` 下创建 `.conf` 文件即可。 - 默认情况下,Apache 使用第一个定义的虚拟主机作为默认主机(即没有匹配到任何 `ServerName` 时的默认页面)。 --- 如需配置 HTTPS(SSL/TLS),可以使用 `mod_ssl` 模块,并在 `<VirtualHost *:443>` 中配置证书路径。 如果你需要具体的 SSL 配置示例,也可以告诉我!
上一篇:
Apache 2.x on Unix systems
下一篇:
windows下apache开启gzip压缩 加快网页下载速度
标题录入,一次不能超过6条
留言
评论