SAMLやOIDCの認証プラットフォームをOSSで提供するKeycloak。Quarkus版になったことで起スクリプト周りも変更しないと動作しなかったので、メモを兼ねて掲載します。
0.稼動ユーザを作成する
usreadd keycloak
1.Javaの用意
ダウンロードして展開する
cd /usr/local/src
wget https://github.com/adoptium/temurin24-binaries/releases/download/jdk-24.0.2%2B12/OpenJDK24U-jdk_x64_linux_hotspot_24.0.2_12.tar.gz
tar xvzf OpenJDK24U-jdk_x64_linux_hotspot_24.0.2_12.tar.gz
mv jdk-24.0.2+12 /usr/local
パスを設定する
su keycloak
cat >> ~/.bashrc << EOF
JAVA_HOME=/usr/local/jdk-24.0.2+12
PATH=$JAVA_HOME/bin:$PATH
EOF
2.KeyCloakを展開する
cd /usr/local/src
wget https://github.com/keycloak/keycloak/releases/download/26.3.2/keycloak-26.3.2.tar.gz
tar xvzf keycloak-26.3.2.tar.gz
mv keycloak-26.3.2 /usr/local/
mkdir /usr/local/keycloak-26.3.2/logs
chown -R keycloak:keycloak /usr/local/keycloak-26.3.2
3.PostgreSQLを展開する(任意)
DBに入れて、Keycloakアップグレード時に移行をスムーズにしましょう。
こちらを参照してインストールします。
PostgreSQL 17.xをインストール(ソースからビルド)for RockyLinux 9.x & Ubuntu 22.x
4.設定ファイルを設定する
/usr/local/keycloak/keycloak-26.3.2/conf/keycloak.confを以下のように編集
/usr/local/keycloak/keycloak-26.3.2/conf/keycloak.conf
mv /usr/local/keycloak/keycloak-26.3.2/conf/keycloak.conf /usr/local/keycloak/keycloak-26.3.2/conf/keycloak.conf.def
cat > /usr/local/keycloak/keycloak-26.3.2/conf/keycloak.conf << EOF
# Basic settings for running in production. Change accordingly before deploying the server.
# Database
# The database vendor.
db=postgres
# The username of the database user.
db-username=keycloak
# The password of the database user.
db-password=keycloak-Password123
# The full database JDBC URL. If not provided, a default URL is set based on the selected database vendor.
db-url=jdbc:postgresql://localhost/keycloak
# Observability
# If the server should expose healthcheck endpoints.
#health-enabled=true
# If the server should expose metrics endpoints.
#metrics-enabled=true
# HTTP
# The file path to a server certificate or certificate chain in PEM format.
#https-certificate-file=${kc.home.dir}/conf/server.crt.pem
# The file path to a private key in PEM format.
#https-certificate-key-file=${kc.home.dir}/conf/server.key.pem
# The proxy address forwarding mode if the server is behind a reverse proxy.
#proxy=reencrypt
# Do not attach route to cookies and rely on the session affinity capabilities from reverse proxy
#spi-sticky-session-encoder-infinispan-should-attach-route=false
# Hostname for the Keycloak server.
#hostname=myhostname
#
log=console,file
log-file=logs/server.log
#log.console.output=file
#log.file=/usr/local/keycloak-26.3.2/logs/keycloak.log
EOF
PostgreSQLのユーザやデータベースの作成もお忘れなく。
5.ビルドするを行う
cd /usr/local/keycloak-26.3.2/bin/kc.sh build
6.起動スクリプトを配置する
cat > /usr/lib/systemd/system/keycloak.service << EOF
[Unit]
Description=Keycloak
After=network.target
[Service]
Type=idle
Environment="JAVA_HOME=/usr/local/jdk-24.0.2+12"
User=keycloak
Group=keycloak
ExecStart=/usr/local/keycloak-26.3.2/bin/kc.sh start --optimized --http-enabled=true --http-port=8080 --hostname=sso.example.com
TimeoutStartSec=600
TimeoutStopSec=600
KillMode=peocess
[Install]
WantedBy=multi-user.target
EOF
7.起動して管理者アカウントを作成する
systemctl start keycloak
稼動しているLinuxOSで、
「http://localhost:8080」にアクセスして管理ユーザを作成する