博客中提到的各个文件路径说明:(我的apache安装在”D:\webtools\Apache2.4”).我的apache版本为Apache/2.4.17 (Win64),最新版下载地址:http://httpd.apache.org/
文件 | 路径 |
---|---|
httpd.exe | D:\webtools\Apache2.4\bin |
httpd.conf | D:\webtools\Apache2.4\conf |
httpd-vhosts.conf | D:\webtools\Apache2.4\conf |
hosts | C:\Windows\System32\drivers\etc\hosts |
如果配置后重启apache失败可以通过在命令行运行:httpd -t来检查语法错误
1. 端口监听
apache的端口监听设置,是指设定apache这个软件针对当前服务器的哪些端口提供服务。通常浏览器提供的都是web请求,但有些请求不在apache的服务范围之内(端口不符)
1.1 设置监听多端口
直接在httpd.conf中添加监听的端口即可。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2. 主机配置
一个主机,最核心的就是两件事:
- 主机(站点)的名字:ServerName “主机名”
- 主机(站点)的实际文件夹位置:DocumentRoot “站点的实际完整路径”
apache的作用其实就是一个”转换”的角色:将当前电脑中的某个文件夹,对外以某个域名(站点)的方式展现出来。换句话说:站点的本质就是一个文件夹。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
当一个请求通过域名解析进入到当前apache并端口配置成功后,apache就会开始”提供服务”。
- 在站点设置中找到ServerName项,看是否匹配请求中的主机名
- 如果找到,则在对应的目录(DocumentRoot配置项)中找相应的文件
- 如果找到,则返回该文件(或调用php语言模块执行后返回)
- 如果第2步没有找到对应请求中的主机名,那么将第一个主机当作准备提供服务的主机
因此:只要ip解析和端口能够进入当前服务器并匹配apache端口设定,则apache一定会提供服务,即使主机名不匹配
当修改主机根目录后访问可能会造成Forbidden,You don't have permission to access / on this server
.这是由于文件夹的访问是有权限的,初始的站点所对应的文件夹,安装的时候已经设置好权限了。现在改了,需要进行设置。
3. 配置文件夹访问权限
权限中部分设置见2中的代码内容。以下内容apache可以设定“默认网页”以提供给用户
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
此时,对于没有指定要访问明确的网页请求,会按顺序从前往后找这些文件,找到后就在返回给用户浏览器中显示出来。但是如果没有找到,此时,Options中的Indexes发挥作用:显示该文件夹中所有文件和文件夹
我们可以将DirectoryIndex设置项单独放在站点文件夹区域设置中,则只对该单独的站点或文件夹起作用
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
4. 主机别名设置
在应用中,通常用一下两种方式来访问同一个站点:
http://www.chris.com
http://chris.com
此时,就相当于”2个站点(主机名)”但访问的是同一个内容
这需要使用主机别名来实现,在多站点设置中会使用到
ServerAlias 别名1 别名2 别名3
当然,如果在本机上访问,记得配置对应的hosts文件,否则仍然无效
5. 目录别名设置
目录别名也叫虚拟目录,对于实际存在的目录,可以直接按正常的文件夹访问层级关系来访问,但是对于不存在的目录,我们可以通过配置项,做到对外看起来存在一样:
比如:http://www.study.com/soft/ 站点中不存在该目录,正常访问会出现NOT FOUND
,但是可以通过设置让该地址访问
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
设置之后会出现Forbidden
,解决方法和2中一样。
6. 文件夹访问控制的文件控制方式
通常,我们在config配置文件中,使用Directory配置项,目的是用来控制文件夹的访问权限。
但我们也可以使用一个独立的文件来控制某文件夹的访问权限。
该文件名必须是: .htaccess
注意:
- 只有后缀和点号(无文件名部分)
- 该文件必须放在要被控制访问权限的文件夹中(不同的文件夹可以放不同的该文件)
- 其“上级文件夹”(通常是Directory设定中的文件夹)必须使用如下代码允许.htaccess发挥作用:
AllowOverride All
- .htaccess文件中出现代码,几乎可以跟Directory设定中出现的代码一样。
- 如果.htaccess文件有效,则其设置会覆盖其上级设置。
- 此.htaccess文件修改后可以立即发挥作用,无需重启apache
7. 多站点设置
7.1 在http.conf中打开多站点配置模块
- 1
- 2
- 1
- 2
将其前面的分号去掉改为:
- 1
- 2
- 1
- 2
7.2 在httpd-vhosts.conf文件中修改默认站点根目录
这是对于apache2.4来说,如果你原先httpd.conf中的根目录修改了,那么这里也要改,因为打开多站点功能后该设置会覆盖httpd.conf文件中的部分设置。否则可能会出现Forbidden。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
7.3 配置站点
你可以按照它给的例子
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
以下是我的设置:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
切记:配置好后一定要修改hosts文件,包括设置的主机名和别名都要添加上。否则缺少这一步还是没用。