ウェブサイト検索

RHEL/CentOS で Apache Userdir モジュールを有効にする方法


ユーザー ディレクトリ または Userdir は Apache モジュールであり、http://example.com/ を使用して Apache Web サーバー経由でユーザー固有のディレクトリを取得できるようにします。 ~user/ 構文。

たとえば、mod_userdir モジュールが有効になっている場合、システム上のユーザー アカウントは、Apache Web サーバーを介して世界中のホーム ディレクトリ内のコンテンツにアクセスできるようになります。

この記事では、RHELCentOS、および Apache userdirs (mod_userdir) を有効にする方法を説明します。 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 Userdirs を有効にする

次に、構成ファイル /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]

ステップ 3: ユーザー ディレクトリの作成

ここで、ユーザー/ユーザーのホーム ディレクトリに public_html ディレクトリを作成する必要があります。たとえば、ここでは tecmint のユーザー ホーム ディレクトリに public_html ディレクトリを作成しています。

mkdir /home/tecmint/public_html

次に、ユーザーの home ディレクトリと public_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/tecmint/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/tecmint/public_html/test.php ファイルを作成します。

<?php
  phpinfo();
?>

それだけです!この記事では、Userdir モジュールを有効にして、ユーザーがホーム ディレクトリのコンテンツを共有できるようにする方法を説明しました。この記事に関してご質問がある場合は、以下のコメント欄でお気軽にお問い合わせください。