使用WAMP搭建PHP本地开发环境

2019-05-01 15:02:33于海丽

3、配置虚拟主机

不喜欢以localhost/项目文件名/xxx.php/…这种方式进行访问?那可以配置虚拟主机,配置后可以通过类似www.test.com(可自定义)这种形式访问。

首先找到httpd-vhosts.conf文件并打开

安装目录binapacheapache2.4.23confextrahttpd-vhosts.conf

在文件末尾添加

<VirtualHost *:80>
  #设置主机名(可自己设置)
  ServerName www.test.com
  #设置主机别名,即用该别名也可以访问(前提是域名解析正确)
  ServerAlias test.com
  #设置该站点根目录
  DocumentRoot "D:wamp64wwwtest"
  #设置文件夹访问控制,其路径要和上一行的DocumentRoot一样,
  <Directory "D:wamp64wwwtest">
    #用于显示设定“可显示文件列表”(当无可显示网页的时候)
    Options Indexes
    #启用文件夹访问控制的文件.htaccess设置
    AllowOverride All
    #请求控制
    Require all granted
    #默认打开的页面设置
    DirectoryIndex index.php index.html
  </Directory>
</VirtualHost>

然后,找到hosts文件,win10的hosts文件路径为:

C:WindowsSystem32driversetc
#每个系统都不一样,可以去问问百度


在文件末尾处添加127.0.0.1 www.test.com(注意中间的空格不要丢),保存。
*如果保存提示另存为的话,可以先另存为,然后修改另存为的文件名为hosts,覆盖掉原来的hosts文件

...
# 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 www.test.com


修改hosts的目的是为了在浏览器访问时,系统不会把域名(www.test.com)提交到DNS服务器,而是直接根据hosts文件找到IP地址(此时是本地),提交解析。这样我们本地的服务器就可以解析这个域名了。

4、局域网远程访问

如果需要在局域网中能通过链接访问站点(例如开发web app时使用手机测试),那么就需要开启服务器远程访问权限了。

打开apache配置文件httpd.conf

安装目录binapacheapache2.4.23confhttpd.conf

修改AllowOverride和Require配置为如下

DocumentRoot "${INSTALL_DIR}/www"
<Directory "${INSTALL_DIR}/www/">
  ...
  AllowOverride all
  Require all granted
  ...
</Directory>

同时需要修改httpd-vhosts.conf文件,修改相同的配置

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot D:/wamp64/www
  <Directory "D:/wamp64/www/">
    ...
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>
								 
			 
相关文章 大家在看