Linux 项目实战之基于域名的虚拟主机
前言:
因为独立的一套网站系统(独立的服务器、独立的公网IP)花费高,效率低(网站对硬件资源的利用率低),对于个人或中小型企业一般无力承担这样的配置。所以由“虚拟主机”技术来解决这个问题,它可以实现一台服务器运行多个网站,且多个网站共用一个公网IP,访问时不同域名共用一个IP,访问不同的网站,大大节省了开销。
前提:
安装了httpd 服务 #yum -y install httpd*
实验需求:
当用户访问www.tarena.com的时候访问tarena网站
当用户访问www.baidu.com的时候访问baidu网站
当用户访问www.google.com的时候访问google网站
步骤:
(1) 配置客户端hosts文件(以win7为例)
192.168.1.254 www.tarena.com
192.168.1.254 www.baidu.com
192.168.1.254 www.google.com
若客户端为Linux则修改/etc/hosts配置文件
(2)建立网站存放路径(Linux服务器端)
#mkdir -p /data/web/{tarena,baidu,google} //分别创建对应网站的存放的目录
#vim /data/web/tarena/index.html //创建并编辑测试网页
<html>
<head>
<title>This is a test Page!!!</title>
<body>
<h1>This www.tarena.com test Page!!</h1>
</body>
</head>
</html>
#vim /data/web/baidu/index.html //创建并编辑测试网页
<html>
<head>
<title>This is a test Page!!!</title>
<body>
<h1>This www.baidu.com test Page!!</h1>
</body>
</head>
</html>
#vim /data/web/google/index.html //创建并编辑测试网页
<html>
<head>
<title>This is a test Page!!!</title>
<body>
<h1>This www.google.com test Page!!</h1>
</body>
</head>
</html>
(3)修改Apache的httpd主配置文件/etc/httpd/conf/httpd.conf中的子配置文件目录/etc/httpd/conf.d/
[root@localhost ~]# vim /etc/httpd/conf.d/virt.conf //创建并编辑virt.conf子配置文件
内容来自/etc/httpd/conf/httpd.conf中的模板
NameVirtualHost *:80 //虚拟主机开关——必写
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.tarena.com //对虚拟主机”www.tarena.com”配置
ErrorLog logs/tarena.com-error_log
CustomLog logs/tarena.com-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /data/web/baidu
ServerName www.baidu.com//对虚拟主机”www.baidu.com”配置
ErrorLog logs/baidu.com-error_log
CustomLog logs/baidu.com-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /data/web/google
ServerName www.google.com//对虚拟主机”www.google.com”配置
ErrorLog logs/google.com-error_log
CustomLog logs/google.com-access_log common
</VirtualHost>
(4)启动服务
[root@localhost ~]# service httpd restart
*注:应用虚拟主机技术后,默认站点不生效,若想让默认 站点生效,需将其添加到/etc/httpd/conf.d/virt.conf中
(5)客户端测试
http://www.google.com
转载于:https://blog.51cto.com/zhangguangjun/1397514