Apache实现一台服务器上运行多个网站
总共有三种方法:通过不同的IP地址
通过不同的域名
通过不同的端口号
(1).通过不同的IP地址实现
例如一台CentOS7有两个IP:192.168.5.101和192.168.5.103
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@youxi1 conf]# cd /etc/httpd/conf.d/ [root@youxi1 conf.d]# vim test.conf //新建一个配置文件 <VirtualHost 192.168.5.101:9988> //IP和端口 ServerAdmin webmaster@dummy-host.example.com DocumentRoot / var /www/html/ //指向的路径 ServerName dummy-host.example.com ErrorLog logs/dummy-host.example.com-error_log CustomLog logs/dummy-host.example.com-access_log common </VirtualHost> <VirtualHost 192.168.5.103:9988> //IP和端口 ServerAdmin webmaster@dummy-host.example.com DocumentRoot / var /www/html/test/ //指向的路径 ServerName dummy-host.example.com ErrorLog logs/dummy-host.example.com-error_log CustomLog logs/dummy-host.example.com-access_log common </VirtualHost> [root@youxi1 conf.d]# vim / var /www/html/index.php / var /www/html/index.html [root@youxi1 conf.d]# vim / var /www/html/test/index.html / var /www/html/test/index.html [root@youxi1 conf.d]# systemctl restart httpd.service |
实验结果:
(2).通过不同的域名实现
首先需要在域名管理系统中将两个域名解析的IP地址指向同一个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@youxi1 conf.d]# vim test.conf //新建一个配置文件 <VirtualHost *:9988> ServerAdmin webmaster@dummy-host.example.com DocumentRoot / var /www/html/ //指向的路径 ServerName www.youxi1.com ErrorLog logs/www.youxi1.com-error_log CustomLog logs/www.youxi1.com-access_log common </VirtualHost> <VirtualHost *:9988> ServerAdmin webmaster@dummy-host.example.com DocumentRoot / var /www/html/test/ //指向的路径 ServerName test.youxi1.com ErrorLog logs/test.youxi1.com-error_log CustomLog logs/test.youxi1.com-access_log common </VirtualHost> [root@youxi1 conf.d]# systemctl restart httpd.service |
由于我没有DNS服务器,所以修改hosts文件
1 2 3 4 5 | [root@youxi1 conf.d]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.5.101 www.youxi1.com 192.168.5.101 test.youxi1.com |
Windows修改在C:\Windows\System32\drivers\etc目录下的hosts文件
1 2 | 192.168.5.101 www.youxi1.com 192.168.5.101 test.youxi1.com |
实验结果:
(3).通过不同的端口号实现
修改/etc/httpd/conf/https.conf文件,添加监听端口
1 2 | Listen 9988 Listen 9989 |
接着再修改新建的test.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [root@youxi1 conf.d]# vim test.conf <VirtualHost *:9988> ServerAdmin webmaster@dummy-host.example.com DocumentRoot / var /www/html/ ServerName www.youxi1.com ErrorLog logs/www.youxi1.com-error_log CustomLog logs/www.youxi1.com-access_log common </VirtualHost> <VirtualHost *:9989> ServerAdmin webmaster@dummy-host.example.com DocumentRoot / var /www/html/test/ ServerName www.youxi1.com ErrorLog logs/www.youxi1.com-error_log CustomLog logs/www.youxi1.com-access_log common </VirtualHost> [root@youxi1 conf.d]# systemctl restart httpd.service [root@youxi1 conf.d]# vim /etc/hosts 192.168.5.101 www.youxi1.com |
Windows修改在C:\Windows\System32\drivers\etc目录下的hosts文件
1 | 192.168.5.101 www.youxi1.com |
实验结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性