httpd服务程序提供的个人用户主页功能,可以实现在系统中为每位用户建立一个独立的网站。

1.在httpd 服务程序中,默认没有开启个人用户主页功能。为此,我们需要编辑下面的配置文件,然后在第17 行的UserDir disabled 参数前面加上井号(#),表示让httpd 服务程序开启个人用户主页功能;

  同时再把第24 行的UserDir public_html 参数前面的井号(#)去掉(UserDir 参数表示网站数据在用户家目录中的保存目录名称,即public_html 目录)。最后,在修改完毕后记得保存。  

[root@localhost html]# vim /etc/httpd/conf.d/userdir.conf 
11 <IfModule mod_userdir.c>
 12     #
 13     # UserDir is disabled by default since it can confirm the presence
 14     # of a username on the system (depending on home directory
 15     # permissions).
 16     #
 17     #UserDir disabled
 18 
 19     #
 20     # To enable requests to /~user/ to serve the user's public_html
 21     # directory, remove the "UserDir disabled" line above, and uncomment
 22     # the following line instead:
 23     # 
 24 UserDir public_html
 25 </IfModule>

2.在用户家目录中建立用于保存网站数据的目录及首页面文件。另外,还需要把家目录的权限修改为755,保证其他人也有权限读取里面的内容。  

[root@localhost html]# su - centos
[centos@localhost ~]$ mkdir public_html
[centos@localhost ~]$ echo "This is centos1's website" > public_html/index.html
[centos@localhost public_html]$ chmod -Rf 755 /home/centos

3.重新启动httpd 服务程序,在浏览器的地址栏中输入网址,其格式为“网址/~用户名”(其中的波浪号是必需的,而且网址、波浪号、用户名之间没有空格),从理论上来讲就可以看到用户的个人网站了。

  不出所料的是,系统显示报错页面,如图 所示。这一定还是SELinux 惹的祸。  

[root@localhost html]# systemctl restart httpd.service 

  

4.httpd 服务程序在提供个人用户主页功能时,该用户的网站数据目录本身就应该是存放到与这位用户对应的家目录中的,所以应该不需要修改家目录的SELinux 安全上下文。

  但是,前文还讲到了Linux 域的概念。Linux 域确保服务程序不能执行违规的操作,只能本本分分地为用户提供服务。httpd 服务中突然开启的这项个人用户主页功能到底有没有被SELinux 域默认允许呢?

  getsebool:查询并过滤出所有与HTTP 协议相关的安全策略。其中,off为禁止状态,on 为允许状态。  

[centos@localhost ~]$ getsebool -a |grep http
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_ipa --> off
httpd_run_preupgrade --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off
named_tcp_bind_http_port --> off
prosody_bind_http_port --> off

  面对如此多的SELinux 域安全策略规则,实在没有必要逐个理解它们,我们只要能通过名字大致猜测出相关的策略用途就足够了。比如,想要开启httpd 服务的个人用户主页功能,那么用到的SELinux 域安全策略应该是httpd_enable_homedirs。

  大致确定后就可以用setsebool 命令来修改SELinux 策略中各条规则的布尔值了。

  在setsebool 命令后面加上-P 参数,可以让修改后的SELinux 策略规则永久生效且立即生效。  

[root@localhost html]# setsebool -P httpd_enable_homedirs=on
[root@localhost html]# firefox

 

5.在网站中添加口令,实现不直接将网页内容显示出来,只想让通过身份验证的用户访客看到里面的内容。  

  1)使用htpasswd 命令生成密码数据库。-c 参数表示第一次生成;后面再分别添加密码数据库的存放文件,以及验证要用到的用户名称(该用户不必是系统中已有的本地账户)。    

[root@localhost html]# htpasswd -c /etc/httpd/passwd centos
New password: 
Re-type new password: 
Adding password for user centos

  2)编辑个人用户主页功能的配置文件。把第31~35 行的参数信息修改成下列内容,其中井号(#)开头的内容为刘遄老师添加的注释信息,可将其忽略。随后保存并退出配置文件,重启httpd 服务程序即可生效。  

[root@localhost html]# vim /etc/httpd/conf.d/userdir.conf
 31     <Directory "/home/*/public_html">
 32     AllowOverride all
 33     authuserfile "/etc/httpd/paswd"
 34     authname "My privately website"
 35     authtype basic
 36     require user centos
 37     Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
 38     Require method GET POST OPTIONS
 39 </Directory>

  

 

posted on 2020-09-23 15:06  星痕1216  阅读(478)  评论(0编辑  收藏  举报