这章很简单,就是在局域网里面模拟一个服务器环境。
首先打开C:\Windows\System32\drivers\etc\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 localhost
127.0.0.1 localhost
可以看到里面定义了locahost为127.0.0.1,它的意思就是给127.0.0.1起了一个别名localhost.
我们在下面使用同样的方法模拟一个域名 ,格式 为 “本机ip” “模拟域名”
我的本机ip为192.168.103.128,我想在本机模拟一个域名 “wodeyuming.com”就添加如下
192.168.103.128 wodeyuming.com
配置完毕点保存,在本机ping wodeyuming.com正常是可以ping通的。
然后到xampp的安装目录下面的一个文件中把我们的虚拟域名添加进去,这样apache就可以用这个域名建立一个最基本的服务器了。
我的xampp安装了在e盘,需要更改的文件为E:\xampp\apache\conf\extra\httpd-vhosts.conf,
打开并且添加
<VirtualHost 192.168.103.128:80>
ServerName wodeyuming.com
DocumentRoot "E:/xampp/htdocs/dummy-host2.example.com"
</VirtualHost>
VirtualHost 为本机ip加端口号80,ServerName就是自己刚才模拟的域名,DocumentRoot 是服务器的文件目录。保存配置重启apache服务,就可以在本机下面访问
www.wodeyuming.com了,显示的内容就是DocumentRoot下面的工程文件。
我们学习的时候通常没有条件去申请一个真正的域名,用这种方法可以模拟比较真实的环境,只是服务器只能部署在局域网段内。