分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

linux apache虚拟主机配置(基于ip,端口,域名)

发布时间:2023-09-06 01:24责任编辑:沈小雨关键词:apache配置虚拟主机

配置环境:

linux版本:Centos6.4

httpd版本:

[root@centos64Study init.d]# pwd
/etc/init.d
[root@centos64Study init.d]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: ??Oct 19 2017 16:43:38

1,安装httpd服务

yum install httpd -y

2,关闭selinux和防火墙

临时设置selinux为permissive( disabled )状态

[root@centos64Study ~]# getenforce

Enforcing

[root@centos64Study ~]# setenforce 0

[root@centos64Study ~]# getenforce

Permissive

永久修改方法:

[root@centos64Study ~]# vim /etc/sysconfig/selinux

SELINUX=permissive

3,关闭防火墙

service iptables stop

第一种,基于多ip访问的配置:

1,先把httpd.conf备份一下,以防出错,可以恢复

httpd.conf的文件路径( /etc/httpd/conf )

[root@centos64Study conf]# ls
httpd.conf ?httpd.conf.bak ?magic

2,如果需要启用虚拟主机配置,先把中心主机的配置注释,

#DocumentRoot "/var/www/html"

3,在httpd.conf中,会默认包conf.d目录中的所有 以.conf结尾的配置文件

Include conf.d/*.conf

所以,把虚拟主机的配置文件独立出来放在conf.d目录下

[root@centos64Study httpd]# ls
conf ?conf.d ?logs ?modules ?run
[root@centos64Study httpd]# pwd
/etc/httpd

[root@centos64Study httpd]# cd conf.d
[root@centos64Study conf.d]# pwd
/etc/httpd/conf.d
[root@centos64Study conf.d]# ls
README ?VirtualHost.conf ?welcome.conf

 

VirtualHost.conf 配置文件内容:

<VirtualHost 192.168.1.8:80>
???????ServerName www.7mxt.com
???????DocumentRoot "/www/7mxt.com"
</VirtualHost>


<VirtualHost 192.168.1.2:80>
???????ServerName www.7mxt.net
???????DocumentRoot "/www/7mxt.net"
</VirtualHost>

我的主机ip是192.168.1.8,添加另一个ip:

 ip addr add 192.168.1.2/24 dev eth0

在对应的目录建立文件:

[root@centos64Study www]# tree
.
├── 7mxt.com
│   └── index.html
└── 7mxt.net
???└── index.html

重启服务: service httpd restart,在浏览器中分别用这两个ip访问,就能看到对应的页面

第二种,基于多端口访问的配置:

1,在VirtualHost.conf中增加一项配置:

<VirtualHost 192.168.1.8:80>
???????ServerName www.7mxt.com
???????DocumentRoot "/www/7mxt.com"
</VirtualHost>
<VirtualHost 192.168.1.2:80>
???????ServerName www.7mxt.net
???????DocumentRoot "/www/7mxt.net"
</VirtualHost>
<VirtualHost 192.168.1.2:8080>
???????ServerName www.abc.net
???????DocumentRoot "/www/abc.net"
</VirtualHost>

2,在httpd.conf监听8080端口

Listen 80
Listen 8080

3,/www建立对应的目录和文件

重启服务: service httpd restart,在浏览器中用http://192.168.1.2:8080/  就能访问到abc.net目录下面的文件index.html内容

[root@centos64Study www]# tree
.
├── 7mxt.com
│   └── index.html
├── 7mxt.net
│   └── index.html
└── abc.net
???  └── index.html

第三种:基于域名的主机配置

1,NameVirtualHost:指定192.168.1.8:80这个ip地址使用域名解释

NameVirtualHost 192.168.1.8:80

<VirtualHost 192.168.1.8:80>
???????ServerName www.7mxt.com
???????DocumentRoot "/www/7mxt.com"
</VirtualHost>

<VirtualHost 192.168.1.8:80>
???????ServerName www.7mxt.org
???????DocumentRoot "/www/7mxt.org"
</VirtualHost>

<VirtualHost 192.168.1.2:80>
???????ServerName www.7mxt.net
???????DocumentRoot "/www/7mxt.net"
</VirtualHost>

<VirtualHost 192.168.1.2:8080>
???????ServerName www.abc.net
???????DocumentRoot "/www/abc.net"
</VirtualHost>

2,在对应的目录下建立文件

[root@centos64Study www]# tree
.
├── 7mxt.com
│   └── index.html
├── 7mxt.net
│   └── index.html
├── 7mxt.org
│   └── index.html
└── abc.net
???  └── index.html

3,在windows host文件中增加主机映射配置

【C:\Windows\System32\drivers\etc】

192.168.1.8 www.7mxt.com
192.168.1.8 ????www.7mxt.org

4,重启服务: service httpd restart,分别用域名www.7mxt.com, www.7mxt.org就能访问到对应的文件内容

使用ip: 192.168.1.8返回的是第一个虚拟主机的配置,即:www.7mxt.com对应目录下面的内容

linux apache虚拟主机配置(基于ip,端口,域名)

原文地址:http://www.cnblogs.com/ghostwu/p/7827104.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved