User Directory或Userdir是一個(gè)Apache模塊,它允許使用http://example.com/~user/語(yǔ)法通過(guò)Apache Web服務(wù)器檢索特定于用戶(hù)的目錄。
例如,當(dāng)啟用mod_userdir模塊時(shí),系統(tǒng)上的用戶(hù)帳戶(hù)將能夠通過(guò)Apache Web服務(wù)器訪問(wèn)其主目錄中的內(nèi)容。
在本文中,我們將向您展示如何使用Apache Web服務(wù)器在RHEL,CentOS和Fedora服務(wù)器上啟用Apache userdirs(mod_userdir)。
本教程假設(shè)您已經(jīng)在Linux發(fā)行版上安裝了Apache Web服務(wù)器。 如果還沒(méi)有,可以使用以下步驟安裝它…
第1步:安裝Apache HTTP Server
要安裝Apache Web服務(wù)器,請(qǐng)?jiān)贚inux發(fā)行版上使用以下命令。
[linuxidc@localhost www.linuxidc.com]$ sudo yum install httpd [在 CentOS/RHEL 上]
[linuxidc@localhost www.linuxidc.com]$ sudo dnf install httpd [在 Fedora 上]
在CentOS 7上安裝Apache
第2步:?jiǎn)⒂肁pache Userdirs
現(xiàn)在,您需要配置Apache Web服務(wù)器以在配置文件/etc/apache2/mods-available/userdir.conf中使用此模塊,該文件已配置了最佳選項(xiàng)。
# vi /etc/httpd/conf.d/userdir.conf
然后驗(yàn)證內(nèi)容如下所示。
# directory if a ~user request is received.
#
# The path to the end user account ‘public_html’ directory must be
# accessible to the webserver userid. This usually means that ~userid
# must have permissions of 711, ~userid/public_html must have permissions
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a “403 Forbidden” message.
#
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
UserDir enabled linuxidc
#
# To enable requests to /~user/ to serve the user’s public_html
# directory, remove the “UserDir disabled” line above, and uncomment
# the following line instead:
#
UserDir public_html
</IfModule>
#
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
#
<Directory “/home/*/public_html”>
## Apache 2.4 users use following ##
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
## Apache 2.2 users use following ##
Options Indexes Includes FollowSymLinks
AllowOverride All
Allow from all
Order deny,allow
</Directory>
要允許少數(shù)用戶(hù)訪問(wèn)UserDir目錄,但不允許其他人訪問(wèn),請(qǐng)?jiān)谂渲梦募惺褂靡韵略O(shè)置。
UserDir disabled
UserDir enabled testuser1 testuser2 testuser3
要允許所有用戶(hù)訪問(wèn)UserDir目錄,但對(duì)少數(shù)用戶(hù)禁用此功能,請(qǐng)?jiān)谂渲梦募惺褂靡韵略O(shè)置。
UserDir enabled
UserDir disabled testuser4 testuser5 testuser6
根據(jù)需要進(jìn)行配置設(shè)置后,需要重新啟動(dòng)Apache Web服務(wù)器以應(yīng)用最近的更改。
# systemctl restart httpd.service [在 SystemD 上]
# service httpd restart [在 SysVInit 上]
第3步:創(chuàng)建用戶(hù)目錄
現(xiàn)在,您需要在user/users主目錄中創(chuàng)建一個(gè)public_html 目錄/目錄。 例如,這里我在linuxidc的用戶(hù)主目錄下創(chuàng)建一個(gè)public_html目錄。
# mkdir /home/linuxidc/public_html
接下來(lái),在用戶(hù)home和public_html目錄上應(yīng)用正確的權(quán)限。
# chmod 711 /home/linuxidc
# chown linuxidc:linuxidc /home/linuxidc/public_html
# chmod 755 /home/linuxidc/public_html
另外,為Apache homedir(httpd_enable_homedirs)設(shè)置正確的SELinux context。
# setsebool -P httpd_enable_homedirs true
# chcon -R -t httpd_sys_content_t /home/linuxidc/public_html
第4步:測(cè)試啟用Apache Userdir
最后,通過(guò)將瀏覽器指向服務(wù)器主機(jī)名或IP地址,然后是用戶(hù)名來(lái)驗(yàn)證Userdir。
http://www.linuxidc.com/~linuxidc/
或
http://IP地址/~linuxidc
如果需要,還可以通過(guò)創(chuàng)建以下文件來(lái)測(cè)試HTML頁(yè)面和PHP信息。
使用以下內(nèi)容創(chuàng)建/home/linuxidc/public_html/linuxidc.com.html文件。
<html>
<head>
<title>linuxidc.com is Best Site for Linux</title>
</head>
<body>
<h1>linuxidc.com is Best Site for Linux</h1>
</body>
</html>
如下圖:
使用以下內(nèi)容創(chuàng)建/home/linuxidc/public_html/linuxidc.com.php文件。
<?php
phpinfo();
?>
OK,在本文中,我們解釋了如何啟用Userdir模塊來(lái)允許用戶(hù)共享來(lái)自其主目錄的內(nèi)容。如果您對(duì)本文有任何疑問(wèn),請(qǐng)?jiān)谙旅娴脑u(píng)論部分提出。