网站搜索

如何在 RHEL/CentOS 上启用 Apache Userdir 模块


用户目录Userdir是一个Apache模块,它允许使用http://example.com/通过Apache Web服务器检索特定于用户的目录~user/ 语法。

例如,当启用 mod_userdir 模块时,系统上的用户帐户将能够通过 Apache Web 服务器访问其主目录中的内容。

在本文中,我们将向您展示如何在 RHELCentOS 和使用 Apache Web 服务器的 Fedora 服务器。

本教程假设您已经在 Linux 发行版上安装了 Apache Web 服务器。如果还没有,您可以使用以下过程安装它......

第 1 步:安装 Apache HTTP 服务器

要安装 Apache Web 服务器,请在 Linux 发行版上使用以下命令。

yum install httpd           [On CentOS/RHEL]
dnf install httpd           [On Fedora]

第 2 步:启用 Apache 用户目录

现在您需要在配置文件 /etc/httpd/conf.d/userdir.conf 中配置 Apache Web 服务器以使用此模块,该文件已经配置了最佳选项。

vi /etc/httpd/conf.d/userdir.conf

然后验证内容,如下所示。

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 tecmint

    #
    # 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>

要允许少数用户访问 UserDir 目录,但不允许其他用户访问,请在配置文件中使用以下设置。

UserDir disabled
UserDir enabled testuser1 testuser2 testuser3

要允许所有用户访问 UserDir 目录,但对少数用户禁用此功能,请在配置文件中使用以下设置。

UserDir enabled
UserDir disabled testuser4 testuser5 testuser6

根据您的要求进行配置设置后,您需要重新启动 Apache Web 服务器以应用最近的更改。

systemctl restart httpd.service  [On SystemD]
service httpd restart            [On SysVInit]

第三步:创建用户目录

现在您需要在用户/用户主目录中创建一个 public_html 目录。例如,我在这里在 howtoing 的用户主目录下创建一个 public_html 目录。

mkdir /home/tecmint/public_html

接下来,对用户 homepublic_html 目录应用正确的权限。

chmod 711 /home/tecmint
chown tecmint:tecmint /home/tecmint/public_html
chmod 755 /home/tecmint/public_html

另外,为 Apache homedirs (httpd_enable_homedirs) 设置正确的 SELinux 上下文。

setsebool -P httpd_enable_homedirs true
chcon -R -t httpd_sys_content_t /home/tecmint/public_html

第 4 步:测试启用的 Apache Userdir

最后,通过将浏览器指向服务器主机名或 IP 地址,后跟用户名来验证 Userdir

http://example.com/~tecmint
OR
http://192.168.0.105/~tecmint

如果需要,您还可以通过创建以下文件来测试 HTML 页面和 PHP 信息。

创建包含以下内容的 /home/howtoing/public_html/test.html 文件。

<html>
  <head>
    <title>TecMint is Best Site for Linux</title>
  </head>
  <body>
    <h1>TecMint is Best Site for Linux</h1>
  </body>
</html>

创建包含以下内容的 /home/howtoing/public_html/test.php 文件。

<?php
  phpinfo();
?>

就这样!在本文中,我们解释了如何启用 Userdir 模块以允许用户共享其主目录中的内容。如果您对本文有疑问,请随时在下面的评论部分提问。