红联Linux门户
Linux帮助

CentOS 6.5 Apache搭建虚拟主机

发布时间:2015-04-08 09:44:26来源:linux网站作者:jinjianjun

实验要求:在一台主机上搭建2个网站服务

www.linux.com  

www.linuxmi.com

实现基于域名的虚拟主机

实现基于IP的虚拟主机

实现基于端口的虚拟主机


一.创建实验环境

1.实现通过域名访问站点需要DNS的解析,可修改DNS数据库文件记录将2个站点IP都指向本机,

也可通过修改/etc/hosts文件来简单实现

# vim /etc/hosts

www.linux.com  192.168.100.10

www.linuxmi.com  192.168.100.10

2.创建网站存放目录及测试页

# mkdir -p /data/web/{linux,linuxmi}

# echo "<h1>www.linux.com</h1>" /data/web/linux/index.html

# echo "<h1>www.linuxmi.com</h1>" /data/web/linuxmi/index.html

3.安装软件包

# yum -y install httpd


二.搭建基于域名虚拟主机

1.修改主配置文件

# cd /etc/httpd/conf.d/

# vim vhost.conf

NameVirtualHost 192.168.100.10:80

<VirtualHost 192.168.100.10:80>

DocumentRoot /data/web/linux

ServerName www.linux.com

ErrorLog logs/www.linux.com-error_log

CustomLog logs/www.linux.com-access_log common

</VirtualHost>

<VirtualHost 192.168.100.10:80>

DocumentRoot /data/web/linuxmi

ServerName www.linuxmi.com

ErrorLog logs/www.linuxmi.com-error_log

CustomLog logs/www.linuxmi.com-access_log common

</VirtualHost>

2.启动服务

# service httpd start

# chkconfig httpd on

3.浏览器测试

http://www.linux.com

http://www.linuxmi.com


三.搭建基于IP的虚拟主机

1.修改主配置文件

# ifconfig eth0:0 192.168.100.20//设置另一个IP

# vim /etc/httpd/conf.d/vhost.conf

#NameVirtualHost *:80//不需要这行

<VirtualHost 192.168.10.10:80>

DocumentRoot /data/web/linux

ServerName 192.168.100.10

ErrorLog logs/192.168.100.10-error_log

CustomLog logs/192.168.100.10-access_log common

</VirtualHost>

<VirtualHost 192.168.100.20:80>

DocumentRoot /data/web/linuxmi

ServerName 192.168.100.20

ErrorLog logs/192.168.100.20-error_log

CustomLog logs/192.168.100.20-access_log common

</VirtualHost>

2.启动服务

# service httpd restart

# chkconfig httpd on

3.浏览器测试

http://192.168.100.10

http://192.168.100.20


四.搭建基于端口的虚拟主机

1.修改主配置文件

# vim /etc/httpd/conf/httpd.conf

...

134 Listen 80

135 Listen 81

# vim /etc/httpd/conf.d/vhost.conf

#NameVirtualHost *:80

<VirtualHost 192.168.100.10:80>

DocumentRoot /data/web/linux

ErrorLog logs/192.168.100.10:80-error_log

CustomLog logs/192.168.100.10:80-access_log common

</VirtualHost>

<VirtualHost 192.168.100.10:81>

DocumentRoot /data/web/linuxmi

ErrorLog logs/192.168.100.10:81-error_log

CustomLog logs/192.168.100.10:81-access_log common

</VirtualHost>

2.启动服务

# service httpd restart

# chkconfig httpd on

3.浏览器测试

http://192.168.100.10:80

http://192.168.100.10:81