版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cx55887/article/details/85795209
本片博文是博文apache配置文件httpd.conf----小白福利中配置15:配置虚拟主机功能的案例。
规划:
网站1:
域名:www.jd.com
根目录:/var/www/html/jd
日志文件:/var/www/html/jd/logs/{access.log, error.log}
默认首页:index.html
网站2:
域名:www.2048.com
根目录:/var/www/html/2048
日志文件:/var/www/html/2048/logs/{access.log, error.log}
默认首页:2048.html
网站3:
域名:www.plane.com
根目录:/var/www/html/plane
日志文件:/var/www/html/plane/logs/{access.log, error.log}
默认首页:fly_fight.html
一、准备环境
- 关闭防火墙
# service iptables stop
- 关闭selinux
# setenforce 0
setenforce: SELinux is disabled
二、修改httpd.conf
NameVirtualHost *:80 <<< 配置基于域名的虚拟主机,必须开启这行
<VirtualHost *:80>
DocumentRoot /var/www/html/jd/
ServerName www.jd.com
Directoryindex index.html
ErrorLog /var/www/html/jd/logs/error.log
CustomLog /var/www/html/jd/logs/access.log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/2048/
ServerName www.2048.com
Directoryindex 2048.html
ErrorLog /var/www/html/2048/logs/error.log
CustomLog /var/www/html/2048/logs/access.log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/plane/
ServerName www.plane.com
Directoryindex sky_fight.html
ErrorLog /var/www/html/plane/logs/error.log
CustomLog /var/www/html/plane/logs/access.log common
</VirtualHost>
三、创建网站目录
[root@web2 ~]# mkdir -pv /var/www/html/{jd,2048,plane}/logs
四、检查语法是否有错
[root@web2 jd]# httpd -t
五、重启httpd
[root@web2 jd]# service httpd restart
[root@web2 jd]# chkconfig httpd on
六、部署网站源码
我已经将源码上传至网站资源,也可直接联系我[email protected]获取。
源码部署非常简单,直接将对应的源码压缩包解压后,将解压后目录里的内容复制或移动到/var/www/html
下对应的目录里即可。
七、实现名称解析
修改本机的hosts文件实现域名解析,windows的hosts文件放在C:/windows/system32/drivers/etc/
,需要注意的是该hosts文件不能直接修改,可以先将hosts文件拉到桌面,改完之后再放回去。
在hosts文件中添加以下三行
10.220.5.191 www.jd.com
10.220.5.191 www.2048.com
10.220.5.191 www.plane.com
测试:ping www.jd.com
八、用浏览器访问jd 2048 plane
用浏览器分别访问:
www.jd.com
www.2048.com
www.plane.com
最终效果图:www.jd.com
www.plane.com
三个网站域名不同,其实使用的同一个主机。
------做运维之前很矫情的小年轻-----