Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享

时间:2023-12-14 09:50:20

          Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享

                                                作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.实现用户家目录的http共享前提

在配置家目录共享的2个前提是:
  基于模块mod_userdir.so实现
  SELinux: http_enable_homedirs 其中userdir模块默认是动态在家的,而selinux我们一般在内外情况下是禁用的:
  [root@node101.yinzhengjie.org.cn ~]# httpd -M | grep userdir
  userdir_module (shared)
  [root@node101.yinzhengjie.org.cn ~]#
  [root@node101.yinzhengjie.org.cn ~]# getenforce
  Disabled
  [root@node101.yinzhengjie.org.cn ~]#
  [root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf.d/userdir.conf      #该配置文件时官方单独抽出来配置用户家目录共享的哟~
  <IfModule mod_userdir.c>
    UserDir disabled            #虽然userdir模块被加载了,但这里却被官方显示禁用了,我们可以添加一个"#"号注释它,这样默认就是启用该功能的。
  </IfModule>
  <Directory "/home/*/public_html">
    AllowOverride FileInfo AuthConfig Limit Indexes
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
  </Directory>
  [root@node101.yinzhengjie.org.cn ~]#

二.配置用户家目录以http方式共享实战

1>.准备数据

[root@node101.yinzhengjie.org.cn ~]# su - yinzhengjie
Last login: Sat Nov :: CST from 172.30.1.101 on pts/
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ ll
total
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ mkdir public_html
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ echo "尹正杰到此一游" > public_html/index.html    #会自动创建一个权限为644的文件。
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ ll
total
drwxrwxr-x yinzhengjie yinzhengjie Dec : public_html
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ cat public_html/index.html
尹正杰到此一游
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ setfacl -m u:apache:x /home/yinzhengjie      #默认其它用户是无权限访问"yinzhengjie"用户的家目录的,因此我们需要显式的添加一条ACL规则来允许apache对家目录有执行权限即可(有了执行权限用户就可以切换到该目录)。
[yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ getfacl /home/yinzhengjie
getfacl: Removing leading '/' from absolute path names
# file: home/yinzhengjie
# owner: yinzhengjie
# group: yinzhengjie
user::rwx
user:apache:--x
group::---
mask::--x
other::--- [yinzhengjie@node101.yinzhengjie.org.cn ~]$
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ exit
logout
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.修改httpd的配置文件

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf.d/userdir.conf      #查看默认的配置文件
<IfModule mod_userdir.c>
UserDir disabled
</IfModule>
<Directory "/home/*/public_html">
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
</Directory>
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# vim /etc/httpd/conf.d/userdir.conf               #默认的配置文件需要修改
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf.d/userdir.conf
<IfModule mod_userdir.c>
UserDir public_html          #如果没有显式指定"UserDir disabled"切userdir模块已经被动态加载的话默认是启用该功能的,我们这里指定只允许访问用户家目录的"public_html"目录。
</IfModule>
<Directory "/home/yinzhengjie/public_html">
Require all granted          #我们只对"/home/yinzhengjie/public_html"目录进行共享
</Directory>
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# httpd -t
Syntax OK
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# systemctl reload httpd
[root@node101.yinzhengjie.org.cn ~]#

3>. 访问测试

  在客户端浏览器只需要输入"http://node101.yinzhengjie.org.cn/~yinzhengjie/"就可以访问到"/home/yinzhengjie/public_html/"目录下的数据啦,如下图所示。

Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享

三.添加验证的方式才能访问httpd配置的家目录

1>.根据上面演示的案例修改httpd的配置文件

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf.d/userdir.conf
<IfModule mod_userdir.c>
UserDir public_html
</IfModule>
<Directory "/home/yinzhengjie/public_html">
AuthType Basic
AuthName "Welecon to Login"
AuthUserFile "/etc/httpd//conf.d/httpdpasswd"
Require user jason  
</Directory>
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /etc/httpd//conf.d/httpdpasswd
jason:$apr1$fnoHrDaP$Q0ZGtsOj9D4W3xHzIKm9E/
jay:{SHA}o78nbN18sxTgXokaJRMEYOxV5b8=
jerry:$apr1$NC0Az6Ga$2ers2ZQZttbW.DPNrie.n0
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# systemctl reload httpd
[root@node101.yinzhengjie.org.cn ~]#

2>.使用jason用户登录测试成功

Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享

  输入正确的用户名和密码就可以看到具体的内容,而且服务器会有相应的日志记录,如下图所示。

Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享

3>.使用jerry用户或者jay用户尽管输入正确的密码均会登录失败

Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享