1 Apache虚拟主机的实现方式有3种。

  • 基于IP的虚拟主机
  • 基于端口的虚拟主机
  • 基于域名的虚拟主机

2.1 启用虚拟主机的准备工作

2.1.1安装httpd

[root@mail httpd]# yum install httpd -y

2.1.2禁用默认的主机模式

[root@mail httpd]# vim /etc/httpd/conf/httpd.conf
注释下面这行内容
#DocumentRoot "/var/www/html"

2.2基于IP的虚拟主机配置

2.2.1为主机添加多个IP

[root@localhost conf.d]# ip addr show dev eth0            #查看原有IP
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:77:77:7d brd ff:ff:ff:ff:ff:ff
    inet 192.168.137.200/24 brd 192.168.137.255 scope global eth0
    inet6 fe80::20c:29ff:fe77:777d/64 scope link
       valid_lft forever preferred_lft forever
[root@localhost conf.d]# ip addr add 192.168.137.201/24 dev eth0 #添加一个IP
[root@localhost conf.d]# ip addr show dev eth0 #查看添加后的IP信息, 此时有2个IP地址了。 200,201
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:77:77:7d brd ff:ff:ff:ff:ff:ff
    inet 192.168.137.200/24 brd 192.168.137.255 scope global eth0
    inet 192.168.137.201/24 scope global secondary eth0
    inet6 fe80::20c:29ff:fe77:777d/64 scope link
       valid_lft forever preferred_lft forever

2.2.2添加虚拟主机配置文件

[root@mail conf.d]# cd /etc/httpd/conf.d/      #进入配置目录
[root@mail conf.d]# vim virtualhost.conf #创建一个配置文件, 编辑内容如下
[root@mail conf.d]# cat virtualhost.conf #查看并检查配置文件
<VirtualHost 192.168.137.200:80>
DocumentRoot "/var/www/test200"
ServerName www.test200.com
</VirtualHost> <VirtualHost 192.168.137.201:80>
DocumentRoot "/var/www/test201"
ServerName www.test201.com
</VirtualHost>
[root@mail conf.d]# cd /var/www            #切换目录
[root@mail www]# mkdir test200 test201 #创建目录
[root@mail www]# echo test200 >>./test200/index.html #创建IP为200的主页
[root@mail www]# echo test201 >>./test201/index.html #创建IP为200的主页

2.2.3测试

[root@localhost www]#  service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]
我们这里使用elinks进行测试, 当然用浏览器测试是一样的
[root@localhost conf]# elinks -source 192.168.137.200
test200
[root@localhost conf]# elinks -source 192.168.137.201
test201

2.3基于端口的虚拟主机配置

2.3.1在主配置文件添加监听端口

[root@localhost conf]# vim /etc/httpd/conf/httpd.conf
在原有行Listen 80行的基础上, 在添加一行
Listen 8080

2.3.2添加8080的端口虚拟配置

[root@localhost conf.d]# cat virtualhost.conf
<VirtualHost 192.168.137.200:80>
DocumentRoot "/var/www/test200"
ServerName www.test200.com
</VirtualHost> <VirtualHost 192.168.137.201:80>
DocumentRoot "/var/www/test201"
ServerName www.test201.com
</VirtualHost>
#下面的内容是在上面的配置的基础上添加的。
<VirtualHost 192.168.137.201:8080>
DocumentRoot "/var/www/test201-8080"
ServerName www.test201-8080.com
</VirtualHost>
[root@localhost conf.d]# cd /var/www/           #切换目录
[root@localhost www]# mkdir test201-8080 #创建目录
[root@localhost www]# echo "test201-8080" >>./test201-8080/index.html #创建主页

2.3.2测试

[root@localhost www]#  service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
[root@localhost conf]# elinks -source 192.168.137.201:80
test201
[root@localhost conf]# elinks -source 192.168.137.201
test201
[root@localhost conf]# elinks -source 192.168.137.201:8080
test201-8080

2.4基于域名的虚拟主机配置

2.4.1 添加域名的虚拟主机配置

[root@localhost conf.d]# vim virtualhost.conf      #编辑虚拟主机配置文件
[root@localhost conf.d]# cat virtualhost.conf #内容如下, 红色部分是在上面的基础上添加的
NameVirtualHost 192.168.137.200:80
<VirtualHost 192.168.137.200:80>
DocumentRoot "/var/www/test200"
ServerName www.test200.com
</VirtualHost> <VirtualHost 192.168.137.200:80>
DocumentRoot "/var/www/test200net"
ServerName www.test200.net
</VirtualHost> <VirtualHost 192.168.137.201:80>
DocumentRoot "/var/www/test201"
ServerName www.test201.com
</VirtualHost> <VirtualHost 192.168.137.201:8080>
DocumentRoot "/var/www/test2018080"
ServerName www.test2018080.com
</VirtualHost>
[root@localhost conf.d]# !ser
service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
[root@localhost conf.d]# cd /var/www            #切换目录
[root@localhost www]# mkdir test200net #创建目录
[root@localhost www]# echo "test200net" >>./test200net/index.html #创建主页

2.4.2 测试

2.4.2.1 添加域名解析

这里我们没有提供dns去解析,简单的使用hosts文件区解析就可以了。

[root@localhost www]# vim /etc/hosts      编辑hosts文件, 添加两行
[root@localhost www]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.137.200 www.test200.com
192.168.137.200 www.test200.net

接下来就可以测试了

[root@localhost www]# elinks -source http://www.test200.com       #测试.com域
test200
[root@localhost www]# elinks -source http://www.test200.net #测试.net域
test200net

Apache配置虚拟主机的三种方法(基于IP、端口、域名)的更多相关文章

  1. apache配置虚拟主机的三种方式

    Apache 配置虚拟主机三种方式   一.基于IP 1. 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP: [root@localhos ...

  2. Centos7 Apache配置虚拟主机的三种方式

    https://blog.csdn.net/tladagio/article/details/80760261 一.虚机主机的三种方式 1.基于IP 2.基于IP+端口 3.基于域名 官网文档:htt ...

  3. Nginx下配置虚拟主机的三种方法

    Nginx下,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的 ...

  4. nginx 配置虚拟主机的三种方法

    nginx,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的管 ...

  5. linux虚拟主机的三种方法

    虚拟主机虚拟主机是将一台(或者一组)服务器的资源(系统资源.网络带宽.存储空间等)按照一定的比例分割成若干相对独立的“小主机”的技术.每一台这样的“小主机”在功能上都可以实现WWW.FTP.Mail等 ...

  6. 【转】Apache 配置虚拟主机三种方式

    Apache 配置虚拟主机三种方式  原文博客http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html 一.基于IP 1. 假 ...

  7. lamp apache配置虚拟主机

    You don't have permission to access /index.php on this server

  8. apache配置虚拟主机后,启动速度慢

    apache配置虚拟主机后,启动速度慢且提示“the requested operation has failed” 可以通过在cmd下启动,来查找问题(命令中的“apache2.2”,是服务名,根据 ...

  9. CentOS 5上Apache配置虚拟主机范例

    昨天实践了下在CentOS 5上通过Apache直接配置虚拟主机,服务器没有安装面板软件,所以只能通过SSH远程连接操作了.Apache安装在/etc/httpd目录下,这个即是Apache的根目录, ...

随机推荐

  1. 转载 HTTPS 之fiddler抓包、jmeter请求

    转载自 http://suixiang0923.github.io/2016/01/12/%E6%B5%85%E8%B0%88HTTPS%E4%BB%A5%E5%8F%8AFiddler%E6%8A% ...

  2. Wamp2.5 64bit,无法改动MySQL datadir位置

    今天偶然想到去更新一下机子里面PHP的版本号,然后又一次去wamp官网下载了WAMP(wamp 64  Apache : 2.4.9 MySQL : 5.6.17 PHP : 5.5.12 PHPMy ...

  3. jdk 配置时时区设置

    在eclipse中的 Default VM Arguments:添加 -Duser.timezone=Aisa/Shanghai

  4. oracle数据库查询时间sql

    select * from cc_picture_info where PICTURE_SOURCE = 3 AND UPLOAD_TIME > to_date('2017-03-29 16:5 ...

  5. gulp报错160

    gulp报错: 这种提示,说明端口被占用,并且要改端口号,首先,我需要把Apache服务器关掉, 然后在gulpfile.js里: 把8080的端口号加进去.就解决了

  6. 分布式项目中 linux 服务器 部署jar 应用脚本 deploy.sh

    在实际项目的部署中,尤其是分布式项目,有很多服务的jar包需要 部署,这里抽取出公用的 deploy的脚本 下面是不含jdk配置的 #!/bin/bash JAVA_OPTIONS_INITIAL=- ...

  7. MyBatis笔记——EhCache二级缓存

    介绍 ehcache是一个分布式缓存框架. 我们系统为了提高系统并发,性能.一般对系统进行分布式部署(集群部署方式)  不使用分布缓存,缓存的数据在各各服务单独存储,不方便系统开发.所以要使用分布式缓 ...

  8. Maven的使用入门

    0.什么是maven? 它是一个软件开发管理工具,主要管理工作是:依赖管理,项目一键构建 1.我们为什么要使用maven? 使用maven构建的项目不包含jar包文件,所以整个项目的体积非常小 mav ...

  9. 任务调度quartz整理

    一张图,了解quartz运行机制: 此图表示:Scheduler是容器,Trigger是多个触发器,jobDetail是多个任务,Calendar是多个日历. jobDetail任务,需要指定类实现J ...

  10. ZOJ 3932 Deque and Balls

    There are n balls, where the i-th ball is labeled as pi. You are going to put n balls into a deque. ...