10.4 個(gè)人用戶主頁功能

2023-05-12 10:25 更新

如果想在系統(tǒng)中為每位用戶建立一個(gè)獨(dú)立的網(wǎng)站,通常的方法是基于虛擬網(wǎng)站主機(jī)功能來部署多個(gè)網(wǎng)站。但這個(gè)工作會讓管理員苦不堪言(尤其是用戶數(shù)量很龐大時(shí)),而且在用戶自行管理網(wǎng)站時(shí),還會碰到各種權(quán)限限制,需要為此做很多額外的工作。其實(shí),httpd服務(wù)程序提供的個(gè)人用戶主頁功能完全可以以勝任這個(gè)工作。該功能可以讓系統(tǒng)內(nèi)所有的用戶在自己的家目錄中管理個(gè)人的網(wǎng)站,而且訪問起來也非常容易。

第1步:在httpd服務(wù)程序中,默認(rèn)沒有開啟個(gè)人用戶主頁功能。為此,我們需要編輯下面的配置文件,然后在第17行的UserDir disabled參數(shù)前面加上井號(#),表示讓httpd服務(wù)程序開啟個(gè)人用戶主頁功能;同時(shí)再把第24行的UserDir public_html參數(shù)前面的井號(#)去掉(UserDir參數(shù)表示網(wǎng)站數(shù)據(jù)在用戶家目錄中的保存目錄名稱,即public_html目錄)。最后,在修改完畢后記得保存。

    [root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf 
     1 #
     2 # UserDir: The name of the directory that is appended onto a user's home
     3 # directory if a ~user request is received.
     4 #
     5 # The path to the end user account 'public_html' directory must be
     6 # accessible to the webserver userid. This usually means that ~userid
     7 # must have permissions of 711, ~userid/public_html must have permissions
     8 # of 755, and documents contained therein must be world-readable.
     9 # Otherwise, the client will only receive a "403 Forbidden" message.
     10 #
     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>
     26 
     27 #
     28 # Control access to UserDir directories. The following is an example
     29 # for a site where these directories are restricted to read-only.
     30 #
     31 <Directory "/home/*/public_html">
     32 AllowOverride FileInfo AuthConfig Limit Indexes
     33 Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
     34 Require method GET POST OPTIONS
     35 </Directory>

第2步:在用戶家目錄中建立用于保存網(wǎng)站數(shù)據(jù)的目錄及首頁面文件。另外,還需要把家目錄的權(quán)限修改為755,保證其他人也有權(quán)限讀取里面的內(nèi)容。

    [root@linuxprobe home]# su - linuxprobe
    Last login: Fri May 22 13:17:37 CST 2017 on :0
    [linuxprobe@linuxprobe ~]$ mkdir public_html
    [linuxprobe@linuxprobe ~]$ echo "This is linuxprobe's website" > public_html/index.html
    [linuxprobe@linuxprobe ~]$ chmod -Rf 755 /home/linuxprobe

第3步:重新啟動httpd服務(wù)程序,在瀏覽器的地址欄中輸入網(wǎng)址,其格式為“網(wǎng)址/~用戶名”(其中的波浪號是必需的,而且網(wǎng)址、波浪號、用戶名之間沒有空格),從理論上來講就可以看到用戶的個(gè)人網(wǎng)站了。不出所料的是,系統(tǒng)顯示報(bào)錯頁面,如圖10-9所示。這一定還是SELinux惹的禍。

圖10-9 禁止訪問用戶的個(gè)人網(wǎng)站

第4步:思考這次報(bào)錯的原因是什么。httpd服務(wù)程序在提供個(gè)人用戶主頁功能時(shí),該用戶的網(wǎng)站數(shù)據(jù)目錄本身就應(yīng)該是存放到與這位用戶對應(yīng)的家目錄中的,所以應(yīng)該不需要修改家目錄的SELinux安全上下文。但是,前文還講到了SELinux域的概念。SELinux域確保服務(wù)程序不能執(zhí)行違規(guī)的操作,只能本本分分地為用戶提供服務(wù)。httpd服務(wù)中突然開啟的這項(xiàng)個(gè)人用戶主頁功能到底有沒有被SELinux域默認(rèn)允許呢?

接下來使用getsebool命令查詢并過濾出所有與HTTP協(xié)議相關(guān)的安全策略。其中,off為禁止?fàn)顟B(tài),on為允許狀態(tài)。

    [root@linuxprobe ~]# 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_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域安全策略規(guī)則,實(shí)在沒有必要逐個(gè)理解它們,我們只要能通過名字大致猜測出相關(guān)的策略用途就足夠了。比如,想要開啟httpd服務(wù)的個(gè)人用戶主頁功能,那么用到的SELinux域安全策略應(yīng)該是httpd_enable_homedirs吧?大致確定后就可以用setsebool命令來修改SELinux策略中各條規(guī)則的布爾值了。大家一定要記得在setsebool命令后面加上-P參數(shù),讓修改后的SELinux策略規(guī)則永久生效且立即生效。隨后刷新網(wǎng)頁,其效果如圖10-10所示。

    [root@linuxprobe ~]# setsebool -P httpd_enable_homedirs=on
    [root@linuxprobe ~]# firefox

圖10-10 正??吹絺€(gè)人用戶主頁面中的內(nèi)容

有時(shí),網(wǎng)站的擁有者并不希望直接將網(wǎng)頁內(nèi)容顯示出來,只想讓通過身份驗(yàn)證的用戶訪客看到里面的內(nèi)容,這時(shí)就可以在網(wǎng)站中添加口令功能了。

第1步:先使用htpasswd命令生成密碼數(shù)據(jù)庫。-c參數(shù)表示第一次生成;后面再分別添加密碼數(shù)據(jù)庫的存放文件,以及驗(yàn)證要用到的用戶名稱(該用戶不必是系統(tǒng)中已有的本地賬戶)。

    [root@linuxprobe ~]# htpasswd -c /etc/httpd/passwd linuxprobe
    New password:此處輸入用于網(wǎng)頁驗(yàn)證的密碼
    Re-type new password:再輸入一遍進(jìn)行確認(rèn)
    Adding password for user linuxprobe

第2步:編輯個(gè)人用戶主頁功能的配置文件。把第31~35行的參數(shù)信息修改成下列內(nèi)容,其中井號(#)開頭的內(nèi)容為劉遄老師添加的注釋信息,可將其忽略。隨后保存并退出配置文件,重啟httpd服務(wù)程序即可生效。

    [root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf
    27 #
    28 # Control access to UserDir directories. The following is an example
    29 # for a site where these directories are restricted to read-only.
    30 #
    31 <Directory "/home/*/public_html">
    32 AllowOverride all
    #剛剛生成出來的密碼驗(yàn)證文件保存路徑
    33 authuserfile "/etc/httpd/passwd"
    #當(dāng)用戶嘗試訪問個(gè)人用戶網(wǎng)站時(shí)的提示信息
    34 authname "My privately website"
    35 authtype basic
    #用戶進(jìn)行賬戶密碼登錄時(shí)需要驗(yàn)證的用戶名稱
    36 require user linuxprobe
    37 </Directory>
    [root@linuxprobe ~]# systemctl restart httpd

此后,當(dāng)用戶再想訪問某個(gè)用戶的個(gè)人網(wǎng)站時(shí),就必須要輸入賬戶和密碼才能正常訪問了。另外,驗(yàn)證時(shí)使用的賬戶和密碼是用htpasswd命令生成的專門用于網(wǎng)站登錄的口令密碼,而不是系統(tǒng)中的用戶密碼,請不要搞錯了。登錄界面如圖10-11所示。

圖10-11 網(wǎng)站提示需要輸入賬戶和密碼才能訪問

出現(xiàn)問題?大膽提問!

因讀者們硬件不同或操作錯誤都可能導(dǎo)致實(shí)驗(yàn)配置出錯,請耐心再仔細(xì)看看操作步驟吧,不要?dú)怵H~

Linux技術(shù)交流請加A群:560843(滿),B群:340829(推薦),C群:463590(推薦),點(diǎn)此查看全國群。

*本群特色:通過口令驗(yàn)證確保每一個(gè)群員都是《Linux就該這么學(xué)》的讀者,答疑更有針對性,不定期免費(fèi)領(lǐng)取定制禮品。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號