前言:为了方便,仍是先让你们知道怎么去创建虚拟主机吧(网站配置),本文基于windows,linux配置是同样的,只是文件所在位置不一样,找到作相应修改便可。都是基础教程大神绕道 php
首先咱们打开httpd.conf找到Include conf/extra/httpd-vhosts.conf这一行去掉前面的#这样apache就会载入extra/httpd-vhosts.conf这个文件,咱们的虚拟主机内容就创建在这个文件里面 html
而后打开extra/httpd-vhosts.conf(位置在apache安装目录下conf/extra/httpd-vhosts.conf)里面会有这么一段代码 linux
1 | <VirtualHost *:80> |
2 | ServerAdmin webmaster@dummy-host2.hleclerc-PC.ingenidev |
3 | DocumentRoot "C:/Dev/Projets/WampServer2-64b/install_files_wampserver2/bin/apache/Apache2.2.17/docs/dummy-host2.hleclerc-PC.ingenidev" |
4 | ServerName dummy-host2.hleclerc-PC.ingenidev |
5 | ErrorLog "logs/dummy-host2.hleclerc-PC.ingenidev-error.log" |
6 | CustomLog "logs/dummy-host2.hleclerc-PC.ingenidev-access.log" common |
7 | </VirtualHost> |
这是一段事例代码其中<VirtualHost *:80>表示虚拟主机配置的开始,星号表示匹配全部ip也能够指定IP,好比只监听127.0.0.1(本地)后面的80表示监听80端口 web
ServerAadmin 表示服务器管理员,写个email好了 apache
DocumentRoot 表示这个网站的跟目录位置好比d:/www/test 编程
ServerName 表示网站域名好比www.test.com windows
在这一行下面还能够写一行ServerAlias 表示的是域名的其余形式,在有二级域名的状况下将会用到,能够写成*.test.com 匹配全部,固然若是你想写成blog.test.com qq.test.com也是没有问题的 浏览器
ErrorLog 表示的是错误日志存放位置为了区分通常会用域名做为名字好比ErrorLog “logs/test.com-error.log” 安全
CustomLog 表示的是访问日志存放的位置,当浏览量十分大的时候这个最好关掉在前面加个#由于这个日志文件会占用很大的磁盘空间固然大小其实也是能够控制的好比CustomLog “logs/test.com-access.log 1M”这样日志文件就不会超过1M了 服务器
理论上这样就行了能够用了但在实际使用过程当中咱们一般会加上
1 | <Directory "d:/www/test"> |
2 | Options FollowSymLinks Includes |
3 | AllowOverride None |
4 | Order allow,deny |
5 | Allow from all |
6 | </Directory> |
这段代码中AllowOverride 设置为 None 时, .htaccess 文件将被彻底忽略。当此指令设置为 All 时,全部具备 “.htaccess” 做用域的指令都容许出如今 .htaccess 文件中。其中Options 别写Indexes禁止目录索引,安全起见。
1 | Order allow,deny |
2 | Allow from all |
这两句呢是控制访问顺序,这里是优先考虑allow,
也能够写成Order deny,allow这样就优先考虑deny,好比若是你要禁止ip为127.0.0.1的用户能够这样写
1 | <Directory "d:/www/test"> |
2 | Options FollowSymLinks Includes |
3 | AllowOverride None |
4 | Order deny,allow |
5 | Allow from all |
6 | Deny from 127.0.0.1 |
7 | </Directory> |
为了让apache自动查找index.php运行咱们打开httpd.conf找到
<IfModule dir_module> DirectoryIndex index.html </IfModule>
修改为
<IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
stop是中止,restart是重启,咱们点restart好了一个虚拟主机配置好了(每次修改配置都要重启)
接下来打开hosts(通常位置C:\WINDOWS\system32\drivers\etc)在最后加上127.0.0.1 www.test.com而后重启浏览器
在d:/www/test下创建index.php 写入<?php echo “hello vhost”?>
在地址栏输入www.test.com看到hello vhost了么
好了vhost的内容就这么多了,详细的apache设置之后会一步一步介绍
猪哥每日一贴教你们编程技术,很是适合新人学习,欢迎转载,务必带上原创出处!
原文:apache建立虚拟主机