Apache默认虚拟主机

来源:互联网 发布:github源码在微信上 编辑:程序博客网 时间:2024/06/17 16:13

一个服务器可以运行多个网站,每个网站都是一个虚拟主机

任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机

在dns还未生效时,通过修改hosts文件,设置ip与域名的映射解析,来配置域名。

在Windows系统中写hosts

Windows系统中hosts位置“C:\Windows\System32\drivers\etc\hosts”
说明:在此可以自定义匹配本地IP和域名,目的是添加临时访问地址(在DNS未生效的时候使用)。

用记事本打开为

# 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             localhost192.168.36.128 www.abc.com www.123.com        //定义一个IP和域名

在windows上的cmd中ping一下看看能否访问定义的IP

C:\Users\丁乐>ping www.123.com正在 Ping www.abc.com [192.168.36.128] 具有 32 字节的数据:    //可以访问定义的IP来自 192.168.36.128 的回复: 字节=32 时间<1ms TTL=64来自 192.168.36.128 的回复: 字节=32 时间<1ms TTL=64来自 192.168.36.128 的回复: 字节=32 时间<1ms TTL=64来自 192.168.36.128 的回复: 字节=32 时间<1ms TTL=64

在浏览器上访问

QQ截图20171219221417.png

说明:在hosts文件中添加“192.168.36.128 www.abc.com
www.123.com”即可将本地虚拟主机添加到Windows系统,访问该域名时会自动解析到本地虚拟主机“ServerName
www.example.com:80”。


Linux中的Apache虚拟主机管理

在物理机访问的域名“www.abc.com”并未在虚拟机Apache配置文件中定义,虚拟机中只定义了“ServerName
www.example.com:80”一个域名,该域名即为Apache的默认主机,此时通过任何一个绑定该虚拟机IP的域名进行访问都会跳转到该主机。

因为一台服务器可以跑多个域名,为了方便管理,需要对虚拟主机进行配置:

编辑Apache配置文件:
[root@dl-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf..........................# Virtual hostsInclude conf/extra/httpd-vhosts.conf    //把注释去掉,表示启用虚拟主机配置文件“httpd-vhosts.conf”..........................................

定义虚拟主机配置文件

[root@dl-001 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf。。。。。。。。。。。。。。。。。。<VirtualHost *:80>    //每个VirtualHost代表一个主机,一个主机代表一个网站    ServerAdmin webmaster@dummy-host.example.com    //定义管理员的邮箱    DocumentRoot "/data/wwwroot/abc.com"    //指定域名所指向的地址    ServerName abc.com            //定义网站名称    ServerAlias www.abc.com www.123.com    //设置别名    ErrorLog "logs/abc.com-error_log"    //指定错误日志    CustomLog "logs/abc.com-access_log" common</VirtualHost><VirtualHost *:80>    ServerAdmin webmaster@dummy-host2.example.com    DocumentRoot "/data/wwwroot/111.com"    ServerName 111.com    ServerAlias www.example.com    ErrorLog "logs/111.com-error_logo"    CustomLog "logs/111.com-access_log" common</VirtualHost>

说明:此时即定义了一个网站,定义网站的核心参数即为:DocumentRoot(网站根目录)、ServerName(域名)。

注意: 虚拟主机生效后,原Apache配置文件中的默认主机(www.example.com ~
192.168.8.131)就会失效(在此用www.example.com ~ 111.com进行验证)。


添加虚拟主机相应目录

[root@dl-001 ~]# mkdir /data/wwwroot/[root@dl-001 ~]# mkdir /data/wwwroot/abc.com[root@dl-001 ~]# mkdir /data/wwwroot/111.com

在目录创建PHP文件

[root@dl-001 ~]# vim /data/wwwroot/abc.com/index.php<?phpecho "abc.com";?>[root@dl-001 ~]# vim /data/wwwroot/111.com/index.php<?php echo "111.com" ?>

检查服务是否有错,并重新加载

[root@dl-001 ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK[root@dl-001 ~]# /usr/local/apache2.4/bin/apachectl graceful

虚拟主机测试

使用curl命令进行测试。

[root@dl-001 ~]# curl -x 192.168.36.128:80 abc.com     // 访问指定ip、端口、域名的内容,返回结果abc.com[root@dl-001 ~]# curl -x 192.168.36.128:80 111.com         111.com[root@dl-001 ~]# curl -x 192.168.36.128:80 www.example.com     111.com

说明:在没有指定域名的情况下,访问任意的域名都将访问到默认主机所对应的内容。

原创粉丝点击