当前位置:文档之家› Apache基于IP、基于port和基于域名的三种虚拟主机的配置方法(linux下)

Apache基于IP、基于port和基于域名的三种虚拟主机的配置方法(linux下)

1.基于相同IP不同Port的虚拟主机
1)vi /etc/httpd/conf/httpd.conf (建议使用nano或gedit等编辑器,下同)
2)将Listen字段改为
Listen 80
Listen 8080
(以上设置表示使用80以及8080端口,好像8888端口有冲突)
3)更改虚拟主机部分为:

DocumentRoot /var/www/html/website1


DocumentRoot /var/www/html/website2

4)保存以上设置
5)创建目录以及页面文件:
#mkdir –p /var/www/html/website1
#mkdir –p /var/www/html/website2
#cd /var/www/html/website1
#cat >index.html <>website1
>EOF
#cd /var/www/html/website2
#cat >index.html <>website2
>EOF
(注:在/etc/httpd/conf/httpd.conf中有DirectoryIndex index.html index.html.var,表示只读index.html,而不读index.htm,切记)
6)service httpd restart
完成以上设置后,可以通过以下方式访问:
1)打开浏览器
2)输入http://192.168.0.1:80以及http://192.168.0.1:8080

2.基于相同Port不同IP的虚拟主机
1)不同IP地址的配置:
#cd /etc/sysconfig/network-scripts
#cp ifcfg-eth0 ifcfg-eth0:1
#vi ifcfg-eth0:1
将eth0:1更改为:
DEVICE=eth0:1
ONBOOT=YES
BOOTPROTO=static
IPADDR=192.168.0.2
NETMASK=255.255.255.0
2)service network restart
3)vi /etc/httpd/conf/httpd.conf
4)更改虚拟主机部分为:

DocumentRoot /var/www/html/website1


DocumentRoot /var/www/html/website2

5)创建目录以及页面文件:
#mkdir –p /var/www/html/website1
#mkdir –p /var/www/html/website2
#cd /var/www/html/website1
#cat >index.html <>website1
>EOF
#cd /var/www/html/website2
#cat >index.html <>website2
>EOF
完成以上设置后,可以通过以下方式访问:
1)打开浏览器
2)输入http://192.168.0.1:80以及http://192.168.0.2:80

3.基于域名的虚拟主机的访问
1)vi /etc/http/conf/httpd.conf
2)更改虚拟主机部分为:
NameVirtualHost 192.168.0.1

DocumentRoot /var/www/html/website1
ServerName https://www.doczj.com/doc/372542457.html,


DocumentRoot /var/www/html/website2
ServerName https://www.doczj.com/doc/372542457.html,

(注:以上设置中NameVirtualHost不可以省略)
3)创建目录以及页面文件:
#mkdir –p /var/www/html/website1
#mkdir –p /var/www/html/website2
#cd /var/www/html/website1
#cat >index.html <>website1
>EOF
#cd /var/www/html/website2
#cat >index.html <>website2
>EOF
4)完成以上设置后,可以通过以下方式访问:
1)打开浏览器
2)输入https://www.doczj.com/doc/372542457.html, 以及 https://www.doczj.com/doc/372542457.html,

注意:如果虚拟主机的目录不在“/var/www”下面,则需要关闭SELINUX,方法如下:
把/etc/selinux/config 中的
SELINUX=enforci

ng
改成
SELINUX=permissive
然后重启。
如果想立即生效,则执行:
# setenforce 0
命令就可以了。

相关主题
文本预览
相关文档 最新文档