Debian 13 trixie

Apache2 : Basic 認証 + LDAP2025/09/08

 

mod_ldap をインストールして、Basic 認証の際に、LDAP ディレクトリーのユーザーで認証できるよう設定します。
当例では以下のような環境の Active Directory を使用します。

ドメインサーバー : Windows Server 2025
ドメイン名 : srv.world
ホスト名 : fd3s.srv.world
NetBIOS 名 : FD3S01
レルム : SRV.WORLD

[1]

Basic 認証の際のパスワードは平文で送信されるため、事前に SSL/TLS の設定を実施しておきます

[2]

こちらを参考に、Active Directory へ LDAPS で接続できるように設定しておきます

[3]

Active Directory 側に、Apache2 からの接続用の AD ユーザーを作成しておきます。
当例では [ldapuser] で進めます。権限は通常の [Domain Users] のみで OK です。

[4] Basic 認証 + LDAP の設定です。
例として [/var/www/html/auth-ldap] ディレクトリーを認証対象として設定します。
# AD の証明書を取得

root@www:~#
echo | openssl s_client -connect fd3s.srv.world:636 2>&1 |awk '/BEGIN/,/END/' > /etc/ssl/certs/fd3s.srv.world.cer
root@www:~#
vi /etc/apache2/sites-available/auth-ldap.conf
# 新規作成
# 下例では ベース DN から [Hiroshima] OU 配下に検索範囲を限定
# よって 下例の場合 認証可能な AD ユーザーは [Hiroshima] OU 配下に登録されたユーザーのみ
# [AuthLDAPBindDN], [AuthLDAPBindPassword] は事前に作成した接続用 AD ユーザーを指定

LDAPTrustedMode SSL
LDAPTrustedGlobalCert CA_BASE64 /etc/ssl/certs/fd3s.srv.world.cer
<Directory "/var/www/html/auth-ldap">
    SSLRequireSSL
    AuthType Basic
    AuthName "LDAP Authentication"
    AuthBasicProvider ldap
    AuthLDAPURL "ldaps://fd3s.srv.world:636/ou=Hiroshima,dc=srv,dc=world?sAMAccountName?sub?(objectClass=*)"
    AuthLDAPBindDN ldapuser@srv.world
    AuthLDAPBindPassword Password
    Require valid-user
</Directory>

root@www:~#
chgrp www-data /etc/apache2/sites-available/auth-ldap.conf

root@www:~#
chmod 640 /etc/apache2/sites-available/auth-ldap.conf

root@www:~#
a2ensite auth-ldap

Enabling site auth-ldap.
To activate the new configuration, you need to run:
  systemctl reload apache2

root@www:~#
a2enmod authnz_ldap

Considering dependency ldap for authnz_ldap:
Enabling module ldap.
Enabling module authnz_ldap.
To activate the new configuration, you need to run:
  systemctl restart apache2

root@www:~#
systemctl restart apache2

# テストページ作成

root@www:~#
mkdir /var/www/html/auth-ldap

root@www:~#
vi /var/www/html/auth-ldap/index.html
<html>
<body>
<h1 style="width: 100%; font-size: 40px; text-align: center;">
Test Page for LDAP Authentication
</h1>
</body>
</html>
[5] 任意のクライアントコンピューターで Web ブラウザーを起動し、作成したテストページにアクセスします。すると設定通り認証を求められるので、AD のユーザーで認証します。
[6] 正常に認証が成功して、テストページが表示されれば OK です。
関連コンテンツ