Skip to content

Amazon Linux 2023 + Apache + mod_auth_mellon + Entra で SSO を設定する

mod_auth_mellon というモジュールを利用すると Apache でも SAML 認証による SSO を実施出来るようになります。 mod_auth_mellon を利用すると「Web サーバ上のコンテンツやアプリケーションに手を加えずに SSO 化出来る」為、便利です。 今回は Amazon Linux 2023 上で mod_auth_mellon を利用して Entra と SAML 認証し、SSO 出来るようにする手順をメモしておきます。

検証環境

対象 バージョン
Amazon Linux 2023.7.202505121
Apache 2.4.62
mod_auth_mellon 0.19.0
mod_ssl 2.4.62

Apache のインストール

dnf で以下をインストールします。

パッケージ名 説明
httpd Apache 本体
mod_auth_mellon Apache を SSO 対応させるモジュール
mod_ssl Apache を SSL/TLS 対応させるモジュール

具体的には以下を実行します。

dnf info httpd mod_auth_mellon mod_ssl

今回は以下のバージョンがインストールされました。

# dnf info httpd mod_auth_mellon mod_ssl
Last metadata expiration check: 0:10:54 ago on Wed May 14 22:49:08 2025.
Available Packages
Name         : httpd
Version      : 2.4.62
Release      : 1.amzn2023
Architecture : aarch64
Size         : 48 k
Source       : httpd-2.4.62-1.amzn2023.src.rpm
Repository   : amazonlinux
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : Apache-2.0 AND (BSD-3-Clause AND metamail AND HPND-sell-variant AND Spencer-94)
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
             : web server.

Name         : mod_auth_mellon
Version      : 0.19.0
Release      : 2.amzn2023
Architecture : aarch64
Size         : 1.3 M
Source       : mod_auth_mellon-0.19.0-2.amzn2023.src.rpm
Repository   : amazonlinux
Summary      : A SAML 2.0 authentication module for the Apache Httpd Server
URL          : https://github.com/latchset/mod_auth_mellon
License      : GPL-2.0-or-later
Description  : The mod_auth_mellon module is an authentication service that implements the
             : SAML 2.0 federation protocol. It grants access based on the attributes
             : received in assertions generated by a IdP server.

Name         : mod_ssl
Epoch        : 1
Version      : 2.4.62
Release      : 1.amzn2023
Architecture : aarch64
Size         : 107 k
Source       : httpd-2.4.62-1.amzn2023.src.rpm
Repository   : amazonlinux
Summary      : SSL/TLS module for the Apache HTTP Server
URL          : https://httpd.apache.org/
License      : Apache-2.0 AND (BSD-3-Clause AND metamail AND HPND-sell-variant AND Spencer-94)
Description  : The mod_ssl module provides strong cryptography for the Apache HTTP
             : server via the Secure Sockets Layer (SSL) and Transport Layer
             : Security (TLS) protocols.

Apache の SSL/TLS サーバ証明書対応

Apache を SSL/TLS サーバ証明書に対応させます。 今回は ARM 版 AL2023 で lego で Route53 認証をし、Let's Encrypt のワイルドカード証明証取得を自動化する の内容に従って Let's Encrypt でサーバ証明書を取得しました。

Apache の設定ファイルである /etc/httpd/conf.d/ssl.conf を以下のように設定します。 SSLCertificateFileSSLCertificateKeyFile でサーバ証明書と秘密鍵を指定します。

/etc/httpd/conf.d/ssl.conf
Listen 443 https
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache         shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout  300
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
<VirtualHost _default_:443>
  ErrorLog logs/ssl_error_log
  TransferLog logs/ssl_access_log
  LogLevel warn
  SSLEngine on
  SSLHonorCipherOrder on
  SSLCipherSuite PROFILE=SYSTEM
  SSLProxyCipherSuite PROFILE=SYSTEM
  SSLCertificateFile /etc/letsencrypt/certificates/www.example.com.crt
  SSLCertificateKeyFile /etc/letsencrypt/certificates/www.example.com.key
  <FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
  </FilesMatch>
  <Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
  </Directory>
    BrowserMatch "MSIE [2-5]" \
      nokeepalive ssl-unclean-shutdown \
      downgrade-1.0 force-response-1.0
    CustomLog logs/ssl_request_log \
      "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>

まだ mod_auth_mellon の設定をしていませんが、一旦 Apache を起動して「サーバ証明書が有効である」ことを確認しておきます。 mod_auth_mellon 設定後は再度、Apache を再起動することになります。

systemctl start httpd.service && \
systemctl enable httpd.service

SSO 設定の流れ

SSO 設定の流れは以下の通りです。

  1. Apache 側でメタデータを設定する
  2. Entra に Enterprise Application を作成する
  3. Enterprise Application の情報を Apache に設定する

1. Apache 側でメタデータを設定する

Apache の mod_auth_mellon にはメタデータを生成するスクリプトが付随しています。 ですが、mod_auth_mellon 付属のスクリプトはバージョンが古い為、GitHub から最新のスクリプトを取得してきます。

curl https://raw.githubusercontent.com/latchset/mod_auth_mellon/main/mellon_create_metadata.sh -o /usr/local/bin/mellon_create_metadata.sh
chmod 755 /usr/local/bin/mellon_create_metadata.sh

メタデータを保存するディレクトリを作成し、メタデータを作成します。

mkdir /etc/httpd/metadata
cd /etc/httpd/meatdata
/usr/local/bin/mellon_create_metadata.sh \
  https://www.example.com/ \
  https://www.example.com/saml

実際にメタデータを作成した際の実行例は以下の通りです。

# /usr/local/bin/mellon_create_metadata.sh \
  https://www.example.com/ \
  https://www.example.com/saml
Output files:
Private key:               https_www.example.com_.key
Certificate:               https_www.example.com_.cert
Metadata:                  https_www.example.com_.xml

Host:                      www.example.com

Endpoints:
SingleLogoutService:       https://www.example.com/saml/logout
AssertionConsumerService:  https://www.example.com/saml/postResponse

作成したメタデータは /etc/httpd/metadata ディレクトリに https_www.example.com_.xml のようなファイル名で保存されます。 このファイルは後の手順で Entra へアップロードする為、手元にダウンロードしておきます。

2. Entra に Enterprise Application を作成する

Entra 上に Enterprise Application を作成していきます。

Step.1

Entra へログインしたら Enterprise Application の一覧へ移動し、独自のアプリケーションの作成 をクリックします。

image

Step.2

アプリケーション名を入力したら 作成 をクリックします。

image

Step.3

シングルサインオン をクリックします。

image

Step.4

SAML をクリックします。

image

Step.5

メタデータファイルをアップロードする をクリックします。

image

Step.6

Apache で作成したメタデータファイルである https_www.example.com_.xml を指定し、追加 をクリックします。

image

Step.7

Enterprise Application にメタデータの内容が反映されます。

image

Step.8

後の手順で Apache へ設定する際に必要となる為、フェデレーションメタデータ XML の ダウンロード をクリックし、XML ファイルをダウンロードしておきます。

image

3. Enterprise Application の情報を Apache に設定する

Entra からダウンロードしたフェデレーションメタデータ XML は /etc/httpd/metadata/www.example.com.xml へアップロードしておきます。 その後、Apache の設定ファイルである /etc/httpd/conf.d/ssl.conf へ以下の内容を追記します。

項目 指定する値 設定例
MellonEndpointPath Apache でメタデータ作成した際に指定した値 /saml/
MellonSPMetadataFile Apache でメタデータ作成した際の Metadata /etc/httpd/metadata/https_www.example.com_.xml
MellonSPPrivateKeyFile Apache でメタデータ作成した際の Private key /etc/httpd/metadata/https_www.example.com_.key
MellonSPCertFile Apache でメタデータ作成した際の Certificate /etc/httpd/metadata/https_www.example.com_.cert
MellonIdPMetadataFile Entra からダウンロードしたフェデレーションメタデータ XML /etc/httpd/metadata/www.example.com.xml

実際の設定例は以下の通りです。 25 〜 33 行目で mod_auth_mellon 関連の設定を行います。

/etc/httpd/conf.d/ssl.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Listen 443 https
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache         shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout  300
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
<VirtualHost _default_:443>
  ServerName www.example.com
  DocumentRoot "/var/www/html"

  ErrorLog logs/ssl_error_log
  TransferLog logs/ssl_access_log
  LogLevel warn
  SSLEngine on
  SSLHonorCipherOrder on
  SSLCipherSuite PROFILE=SYSTEM
  SSLProxyCipherSuite PROFILE=SYSTEM
  SSLCertificateFile /etc/letsencrypt/certificates/www.example.com.crt
  SSLCertificateKeyFile /etc/letsencrypt/certificates/www.example.com.key
  <Directory "/var/www/html">
    Options FollowSymLinks
    AllowOverride None

    AuthType Mellon
    MellonEnable auth
    Require valid-user

    MellonEndpointPath /saml/
    MellonSPMetadataFile /etc/httpd/metadata/https_www.example.com_.xml
    MellonSPPrivateKeyFile /etc/httpd/metadata/https_www.example.com_.key
    MellonSPCertFile /etc/httpd/metadata/https_www.example.com_.cert
    MellonIdPMetadataFile /etc/httpd/metadata/www.example.com.xml
  </Directory>
  <FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
  </FilesMatch>
  <Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
  </Directory>
    BrowserMatch "MSIE [2-5]" \
      nokeepalive ssl-unclean-shutdown \
      downgrade-1.0 force-response-1.0
    CustomLog logs/ssl_request_log \
      "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>

但し、これだけでは SSO が上手く動作しないようです。 この問題を回避する為には /etc/httpd/conf.d/auth_mellon.conf を以下のように修正します。

MellonCacheSize 100
MellonLockFile "/run/mod_auth_mellon/lock"
MellonDiagnosticsFile /var/log/httpd/mellon_diagnostics.log
MellonDiagnosticsEnable On
SetEnv MELLON_DISABLE_SAMESITE 1

設定が完了したら Apache を再起動し、設定を反映します。

systemctl restart httpd.service

Web ブラウザでサイトにアクセスし、「証明書の警告が表示されず、Entra へリダイレクトされて SSO 動作する」ことが出来れば設定は完了です。

参考

デフォルトの /etc/httpd/conf.d/auth_mellon.conf

MellonCacheSize 100
MellonLockFile "/run/mod_auth_mellon/lock"