博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CetOS 7LAMP架构6】,Apache默认虚拟主机#171220
阅读量:6617 次
发布时间:2019-06-25

本文共 7967 字,大约阅读时间需要 26 分钟。

hot3.png

hellopasswd


httpd的默认虚拟主机

  • 一台服务器可以访问多个网站,每个网站都是一个虚拟机
  • 概念:域名(主机名)、DNS、解析域名、hosts
  • 任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机
  • vi /usr/local/apache2.4/conf/httpd.conf #搜索httpd/vhost,去掉#
  • vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf #改为如下 <VirtuaHost *:80> ServerAdmin DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.example.com Errorlog "log/abc.com-access_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtuaHost *:80> DocumentRoot "/data/wwwroot/www.111.com" ServerName www.111.com </VirtualHost>
  • /usr/local/apache2.4/bin/apachectl -t
  • /usr/local/apache2.4/bin/apachectl graceful

网站能够直接访问,其实是DocumentRoot "/usr/local/apache2.4/htdocs"指定了一个文件

[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf\DocumentRoot    211 # below.    212 #    213     214 #    215 # DocumentRoot: The directory out of which you will serve your    216 # documents. By default, all requests are taken from this directory, but    217 # symbolic links and aliases may be used to point to other locations.    218 #    219 DocumentRoot "/usr/local/apache2.4/htdocs"    220 
221 # 222 # Possible values for the Options directive are "None", "All", 223 # or any combination of: 224 # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 225 # 226 # Note that "MultiViews" must be named *explicitly* --- "Options All"

而域名为

[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf/ServerName    188 #    189 # ServerName gives the name and port that the server uses to identify itself.    190 # This can often be determined automatically, but we recommend you specify    191 # it explicitly to prevent problems during startup.    192 #    193 # If your host doesn't have a registered DNS name, enter its IP address here.    194 #    195 ServerName www.example.com:80    196     197 #    198 # Deny access to the entirety of your server's filesystem. You must    199 # explicitly permit access to web content directories in other    200 # 
blocks below. 201 # 202
203 AllowOverride none

Windows下的hosts

C:\Users\Administrator>cd C:\Windows\System32\drivers\etcC:\Windows\System32\drivers\etc>start hosts# Copyright (c) 1993-2009 Microsoft Corp.## This is a sample HOSTS file used by Microsoft TCP/IP for Windows.## This file contains the mappings of IP addresses to host names. Each# entry should be kept on an individual line. The IP address should# be placed in the first column followed by the corresponding host name.# The IP address and the host name should be separated by at least one# space.## Additionally, comments (such as these) may be inserted on individual# lines or following the machine name denoted by a '#' symbol.## For example:##      102.54.94.97     rhino.acme.com          # source server#       38.25.63.10     x.acme.com              # x client host# localhost name resolution is handled within DNS itself.#	127.0.0.1       localhost#	::1             localhost127.0.0.1       localhost

在hosts中添加一行192.168.9.134 www.abc.com www.123.com

C:\Users\Administrator>ping www.abc.com正在 Ping www.abc.com [192.168.9.134] 具有 32 字节的数据:来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64192.168.9.134 的 Ping 统计信息:    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),往返行程的估计时间(以毫秒为单位):    最短 = 0ms,最长 = 0ms,平均 = 0msC:\Users\Administrator>ping www.123.com正在 Ping www.abc.com [192.168.9.134] 具有 32 字节的数据:来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64192.168.9.134 的 Ping 统计信息:    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),往返行程的估计时间(以毫秒为单位):    最短 = 0ms,最长 = 0ms,平均 = 0ms

浏览器访问www.abc.com或www.123.com出现It work!则表示成功

apache默认虚拟主机,当访问ServerName和DocumentRoot 默认虚拟主机

[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf/extra    470 #Include conf/extra/httpd-languages.conf    471     472 # User home directories    473 #Include conf/extra/httpd-userdir.conf    474     475 # Real-time info on requests and configuration    476 #Include conf/extra/httpd-info.conf    477     478 # Virtual hosts    479 #Include conf/extra/httpd-vhosts.conf    480     481 # Local access to the Apache HTTP Server Manual    482 #Include conf/extra/httpd-manual.conf    483     484 # Distributed authoring and versioning (WebDAV)    485 #Include conf/extra/httpd-dav.conf

将479行的注释去掉为Include conf/extra/httpd-vhosts.conf

打开二级配置文件,虚拟主机配置文件

[root@localhost ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf     23 
24 ServerAdmin webmaster@dummy-host.example.com 25 DocumentRoot "/usr/local/apache2.4/docs/dummy-host.example.com" 26 ServerName dummy-host.example.com 27 ServerAlias www.dummy-host.example.com 28 ErrorLog "logs/dummy-host.example.com-error_log" 29 CustomLog "logs/dummy-host.example.com-access_log" common 30
31 32
33 ServerAdmin webmaster@dummy-host2.example.com 34 DocumentRoot "/usr/local/apache2.4/docs/dummy-host2.example.com" 35 ServerName dummy-host2.example.com 36 ErrorLog "logs/dummy-host2.example.com-error_log" 37 CustomLog "logs/dummy-host2.example.com-access_log" common 38

一个主机代表一个网站 ServerAdmin有无都可以 DocumentRoot代表网站根目录 ServerName代表域名 ServerAlias代表别名 ErrorLog错误日志 CustomLog访问日志

修改为以下,若修改后之前的www.example.com就会访问不了

23 
24 DocumentRoot "/data/wwwroot/abc.com" 25 ServerName abc.com 26 ServerAlias www.abc.com www.123.com 27 ErrorLog "logs/abc.com-error_log" 28 CustomLog "logs/abc.com-access_log" common 29
30 31
32 DocumentRoot "/data/wwwroot/111.com" 33 ServerName 111.com 34 ServerAlias www.example.com 35 ErrorLog "logs/111.com-error_log" 36 CustomLog "logs/111.com-access_log" common 37

但可以添加一个别名为www.example.com,但他此时对应的目录为/data/wwwroot/111.com

在站点目录下创建

[root@localhost ~]# mkdir /data/wwwroot/[root@localhost ~]# mkdir /data/wwwroot/abc.com[root@localhost ~]# mkdir /data/wwwroot/111.com[root@localhost ~]# vi /data/wwwroot/abc.com/index.php	
[root@localhost ~]# vi /data/wwwroot/111.com/index.php
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# ping www.abc.comPING abc.com (199.181.132.250) 56(84) bytes of data.^C64 bytes from 199.181.132.250: icmp_seq=1 ttl=128 time=248 ms--- abc.com ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 248.780/248.780/248.780/0.000 ms

若没有绑定hosts,则会访问到外网,此时可以使用curl -x

[root@localhost ~]# curl -x 192.168.9.134:80 abc.comhello!abc.com[root@localhost ~]# curl -x 192.168.9.134:80 www.abc.comhello!abc.com[root@localhost ~]# curl -x 192.168.9.134:80 www.abcde.comhello!abc.com

无论任何域名都指向abc.com,而abc.com是虚拟主机配置的文件中的虚拟主机,而且还是默认虚拟主机

[root@localhost ~]# curl -x 192.168.9.134:80 www.example.comhello!111.comcurl -x 192.168.9.134:80 www.111.comhello!abc.com[root@localhost ~]# curl -x 192.168.9.134:80 111.comhello!111.com

www.example.com指向111.com,而www.111.com没有指定别名www.111.com,则会指向www.abc.com

虚拟主机配置文件生效,则可以定义多个虚拟主机

[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf    471     472 # User home directories    473 #Include conf/extra/httpd-userdir.conf    474     475 # Real-time info on requests and configuration    476 #Include conf/extra/httpd-info.conf    477     478 # Virtual hosts    479 Include conf/extra/httpd-vhosts.conf    480     481 # Local access to the Apache HTTP Server Manual    482 #Include conf/extra/httpd-manual.conf    483     484 # Distributed authoring and versioning (WebDAV)    485 #Include conf/extra/httpd-dav.conf    486

而最为特殊的是默认虚拟主机,任何域名解析到这个主机上则会自动解析虚拟主机


修改与 171220

转载于:https://my.oschina.net/hellopasswd/blog/1592848

你可能感兴趣的文章
Linux常用命令1
查看>>
JS脚本强制kill掉MongoDB慢查询
查看>>
Bash on windows从14.0升级到ubuntu16.04
查看>>
分布式文件系统之管理DFS复制与基于访问的枚举
查看>>
iOS各种证书
查看>>
VC++编程之第二课笔记——C++的继承封装多态
查看>>
homework week03
查看>>
【Java例题】7.4 文件题1-学生成绩排序
查看>>
L3-004. 肿瘤诊断
查看>>
linux环境安装oracle11G准备环境(自学笔记1)
查看>>
js传输中文参数
查看>>
Windows sever 2008
查看>>
【C#】程序集中资源文件的使用
查看>>
docker基本使用
查看>>
sqlserver 发送邮件脚本
查看>>
cf 460 E. Congruence Equation 数学题
查看>>
css 区块与盒子模型
查看>>
AIR 初步 Javascript学习之cookie操作
查看>>
CentOS7 yum 安装git
查看>>
集合框架2
查看>>