虚拟主机——在一台www服务器上运行多个网站
前提条件
- 正确安装httpd
- 主机IP:192.168.0.225
- 防火墙关闭(或者已经开放相应端口)
- 分别在www目录下新建两个目录:mkdir god.com hell.com,并分别在god.com和hell.com的目录下新建index.html文件如下
god.com目录下的index.html
<html><head>hell</head><br></br><body>this is hell area</body></html>
hell.com目录下的index.html
<html><head>hell</head><br></br><body>this is hell area</body></html>
配置虚拟主机
两种方式:基于IP和基于主机名
- 基于IP
1、基于同IP不同端口(在默认的配置前提下:已有80端口)
vim /etc/httpd/conf/httpd.conf
添加监听端口:listen 8080
注释ServerAdmin:#ServerAdmin root@localhost
注释documentRoot:#DocumentRoot "/var/www/html"
在配置文件最后添加以下内容:
NameVirtualHost *:80<VirtualHost 192.168.0.225:80> ???????ServerAdmin admin@god.com ???????DocumentRoot /var/www/god.com ???????Servername www.god.com ???????errorlog logs/god.com-error_log ???????customlog logs/god.com-access_log common</VirtualHost><VirtualHost 192.168.0.225:8080> ???????ServerAdmin admin@hell.com ???????DocumentRoot /var/www/hell.com ???????Servername www.hell.com ???????errorlog logs/hell.com-error_log ???????customlog logs/hell.com-access_log common</VirtualHost>
测试:分别访问192.168.0.225和192.168.0.225:8080,结果如下:
2、基于同端口不同IP(同1,针对有两个IP的服务器,配置中只是IP不同)
- 基于主机名(ing)
重启httpd
service httpd start/stop/restart/status
apache httpd 虚拟主机配置(基于centos 7)
原文地址:https://www.cnblogs.com/feer/p/9837439.html