Apache服务
用户访问控制,认证方式有basic和digest两种
首先创建用户认证文件,为用户认证做准备
[root@centos7-client html]# htpasswd -c -m /etc/httpd/conf.d/.htpassword wangy
New password:
Re-type new password:
Adding password for user wangy
//有以下四种加密手段,其中MD5是最常规的 BCRYPT是强加密
-m Force MD5 encryption of the password (default).
-2 Force SHA-256 crypt() hash of the password (secure).
-5 Force SHA-512 crypt() hash of the password (secure).
-B Force bcrypt aencryption of the password (very secure).
[root@centos7-client html]# htpasswd -b -m /etc/httpd/conf.d/.htpassword lisi lisi
Adding password for user lisi
// -b参数就不用去输入用户密码 可以直接放在命令行当中
接下来我们要修改配置文件,让目录添加认证参数
[root@centos7-client html]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/webdata/www/html"
<Directory "/webdata/www/html">
AuthType Basic
AuthName "Restricted Resource"
AuthBasicProvider file
AuthUserFile /etc/httpd/conf.d/.htpassword
Require user wangy
</Directory>
[root@centos7-client html]# mkdir -p /webdata/www/html
[root@centos7-client html]# echo hello,world > /webdata/www/html/index.html
[root@centos7-client html]# systemctl restart httpd
最后我们进行测试,因为我们目录放行的是wangy这个用户,所以哪怕lisi这个用户已经创建了加密文件,但是依旧无法登录
浏览器不同,提示框也不同,输入用户名和密码进行登录,我们还可以发现 lisi是没法登录网站的
Options指令
后面跟1个或者多个以空白字符分隔的选项列表,在选项前面可以用 + ,- 表示增加或者删除指定选项
常见选项(默认是全部禁用的)
Indexes : 指明的URL 路径下不存在与定义的主页面资源相符的资源文件,返回索引列表给用户
FollowSymLinks :允许访问符号链接文件所指向的源文件
None: 全部禁用
ALL:全部允许
修改配置文件:
[root@centos7-client html]# vim /etc/httpd/conf/httpd.conf
<Directory "/webdata/www/html">
Options +Indexes //加上这个参数
AuthType Basic
AuthName "Restricted Resource"
AuthBasicProvider file
AuthUserFile /etc/httpd/conf.d/.htpassword
Require user wangy
</Directory>
接下来我们创建一个目录,找不到主页 但是因为加上了+Indexes 所以找不到资源index.html主页,所以直接索引列表给用户,所以加上这个参数是不安全的。
[root@centos7-client html]# mkdir source
[root@centos7-client html]# cd source/
[root@centos7-client source]# touch file1 file2
[root@centos7-client source]# ll
total 0
-rw-r--r--. 1 root root 0 Feb 20 21:20 file1
-rw-r--r--. 1 root root 0 Feb 20 21:20 file2
首先我们创建软连接,把/etc下的配置文件 链接到/webdata/www/html/source目录当中
[root@centos7-client source]# ln -s /etc/hosts hosts
[root@centos7-client source]# ll
total 0
-rw-r--r--. 1 root root 0 Feb 20 21:20 file1
-rw-r--r--. 1 root root 0 Feb 20 21:20 file2
lrwxrwxrwx. 1 root root 10 Feb 20 21:37 hosts -> /etc/hosts
此时去访问浏览器,居然发现,链接文件也可以显示,直接可以通过web来访问,这个是不安全的
所以我们要在配置文件当中,把这个功能禁用FollowSymLinks
[root@centos7-client source]# vim /etc/httpd/conf/httpd.conf
<Directory "/webdata/www/html">
Options +Indexes
Options -FollowSymLinks
AuthType Basic
AuthName "Restricted Resource"
AuthBasicProvider file
AuthUserFile /etc/httpd/conf.d/.htpassword
Require user wangy
</Directory>
[root@centos7-client source]# systemctl restart httpd
重启服务 我们继续访问会发现,此时链接文件不见了。
虚拟主机:
- 基于IP地址的虚拟主机
- 基于端口的虚拟主机
首先我们在/webdata目录下 创建四个站点分别为site1 site2 site3 site4
我们分别给四个站点 设置index.html 设置内容为hello,this is siteX
基于ip的虚拟主机,以虚拟web主机使用不同的ip地址,通过一台httpd服务器,对外提供web功能,
所以我们的虚拟web服务器,需要有大量的网络接口,在显示中不太好实现,所以一般都是基于端口做虚拟主机。
基于端口的虚拟主机,增加一个配置文件
[root@centos7-client webdata]# vim /etc/httpd/conf.d/site.conf
Listen 8080
Listen 9090
<Directory "/webdata/">
Require all granted
</Directory>
<VirtualHost *:8080>
DocumentRoot "/webdata/site1/"
</virtualHost>
<VirtualHost *:9090>
DocumentRoot "/webdata/site2/"
</virtualHost>
[root@centos7-client webdata]# systemctl restart httpd
[root@centos7-client webdata]# curl 192.168.40.178:8080
<h1> hello,this is site1 </h1>
[root@centos7-client webdata]# curl 192.168.40.178:9090
<h1> hello this is Site2 </h1>