Keycloak is open-source Identity and Access Management software. You manage users, groups, and roles centrally in Keycloak, while your applications leave sign-in to Keycloak. Once users sign in, they’re signed in to all connected apps. This principle is called Single Sign-on (SSO). Keycloak uses OpenID Connect, OAuth 2.0, and SAML 2.0 for this, includes two-factor authentication, and can import users from LDAP or Active Directory. In this guide, we’ll show you two ways to get started: the easy way, using the One-Click App, which makes Keycloak available over HTTPS in just a few clicks, and manual installation with Docker Compose. After that, you’ll create your own realm and connect your first application using OpenID Connect.
Keycloak is useful as soon as multiple applications need to use the same users. Keycloak is a good fit on a VPS if:
- You don’t want to manage accounts separately in each app, but instead create and disable users centrally and grant permissions using groups and roles.
- You develop your own software and don’t want to build sign-in yourself. Keycloak handles the login page, password rules, registration, and OpenID Connect tokens.
- Software or partners explicitly require Keycloak, which is the case for many business and open-source projects.
- You want to use users from LDAP or Active Directory for web applications without copying them.
- You want to enforce two-factor authentication consistently across all connected apps.
Keycloak is less suitable if you only run a single app with a small number of users. In that case, the app’s built-in sign-in is simpler. Keycloak is a Java application with its own concepts, such as realms, clients, and roles, and needs more RAM than most of the apps it protects. authentik is also a good choice for self-hosted apps at home, and you can find it in the app catalog too.
The app catalog offers Keycloak in two variants. Both start Keycloak in production mode with the same version; they only differ in the database:
| Keycloak with Postgres | Keycloak | |
|---|---|---|
| Database | Separate PostgreSQL container | Built-in file-based database dev-file (H2) |
| Intended for | Production use with users and apps | Trying it out and testing |
| Data stored in | PostgreSQL volume | Volume /opt/keycloak/data in the Keycloak container |
| RAM requirements | Slightly higher (additional database container) | Slightly lower |
According to the official documentation, the dev-file database is only intended for development and is not suitable for production. Therefore, choose Keycloak with Postgres as soon as real users or applications depend on Keycloak. There’s no one-click way to switch databases later; you would need to export your realms and import them into a new installation.
The quickest way to get Keycloak is through your VPS’s One-Click Apps. The app is directly accessible over HTTPS using a domain, and it sets the hostname and proxy settings for you and automatically creates an admin account and database password. You don’t need a reverse proxy, certificate, or firewall rules.
- A Linux VPS (Debian or Ubuntu) with One-Click Apps set up. See Set Up One-Click Apps for instructions.
- At least 2 GB of RAM; 4 GB of RAM and 2 vCPU cores are recommended. Immediately after startup, Keycloak and PostgreSQL together use just over 0.5 GB of RAM. Usage increases with more users and sessions; the official documentation estimates around 1.25 GB of RAM for Keycloak alone.
- Open your VPS in the ZapX dashboard and go to the One-Click Apps tab.
- Search for Keycloak in the app catalog. You’ll see two cards. Select Keycloak with Postgres and click Install app.
- Use a custom subdomain or zap.cloud is already selected, with a randomly generated name. Leave the option enabled or enter your own name, such as
ssoorlogin. Keycloak needs a domain with HTTPS for sign-in to work with other apps. - You don’t need to enter anything under App configuration.
TIMEZONEsets the time zone, andPOSTGRESQL_DATABASEsets the database name. LeaveKC_HTTP_ENABLED,KC_HEALTH_ENABLED, andKC_PROXY_HEADERSat their default values. - Click Queue installation.
Installation takes a few minutes. On the first startup, Keycloak creates its database. The address will show an error page while this is happening; reload it after a short while.
Keycloak is installed with an admin account whose username and password are generated automatically:
- Click the gear icon on the app card and go to App environment.
- Under Automatically generated values, you’ll find
SERVICE_USER_ADMIN(username) andSERVICE_PASSWORD_ADMIN(password). Click the eye icon to reveal a value, or the icon next to it to copy it. - Open your app using the Access link. Keycloak redirects you to the Administration Console at
/admin/and displays the “Sign in to your account” page. - Sign in using the username and password from the App environment.
After your first login, Keycloak displays a yellow notice at the top: “You are logged in as a temporary admin user”. The generated account is deliberately intended only as a starter account. Create your own admin account and then remove the starter account:
- You’re in the master realm. Click Users on the left, then Add user.
- Enter a name under Username, optionally add your email address, and click Create.
- Go to the Credentials tab and click Set password. Set a long password, switch Temporary to Off, and click Save.
- Go to the Role mapping tab, click Assign role, select Realm roles, check
admin, and confirm by clicking Assign. - Sign out at the top right and sign back in with the new account. Then delete the starter account under Users.
Keycloak reads SERVICE_USER_ADMIN and SERVICE_PASSWORD_ADMIN only on the very first startup, before the master realm exists. Changes to these values later won’t take effect. After that, change passwords in the Administration Console under Users.
The One-Click App automatically sets KC_HOSTNAME to your app’s domain. Keycloak uses it to build all links, redirects, and token URLs. KC_PROXY_HEADERS is set to xforwarded, so Keycloak can detect behind the proxy that the connection is using HTTPS. Don’t change these values. Otherwise, the addresses your apps use for sign-in will no longer be correct, and Keycloak may display an error such as “HTTPS required”.
Update Keycloak in the app settings (gear icon) under App update. Click Check for updates, then install a new version using Apply update. If it says the app is up to date, there isn’t a newer version in the app catalog yet.
When a new version starts, Keycloak automatically updates the database. After that, you can only go back to the old version by restoring a backup. So back up your VPS first, for example using Backups in the dashboard, and read the release notes.
All realms, users, clients, and settings are stored in the app’s PostgreSQL database. They’re preserved during updates.
The rest of this guide describes manual installation of Keycloak with Docker Compose on your VPS, for example if you don’t use One-Click Apps on the server or want to run Keycloak behind your own reverse proxy.
- A Linux VPS running Debian or Ubuntu, with SSH access. See First Access via SSH.
- Docker and Docker Compose (Compose v2). See Install Docker. Check the installation with
docker --versionanddocker compose version. - A domain or subdomain with an A record pointing to your VPS’s IP address (e.g.
keycloak.example.com).
The figures below are estimates for a small to medium-sized deployment with PostgreSQL. The official documentation estimates around 1.25 GB of RAM for Keycloak with caches for 10,000 sessions and recommends one vCPU core per 15 password-based logins per second.
| Hardware | Minimum | Recommended |
|---|---|---|
| CPU | 1 vCPU core | 2 vCPU cores |
| RAM | 2 GB | 4 GB |
| Storage | 10 GB | 20 GB |
In this guide, Keycloak listens on port 8080 (HTTP) and management port 9000 for health checks. You don’t need to open either port if you use a reverse proxy as described below. With ufw, allow only SSH and the ports for the reverse proxy:
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
Ports published by Docker using ports: are accessible from outside even if ufw blocks them. Docker adds its own rules ahead of ufw’s rules. Therefore, bind Keycloak only to 127.0.0.1 as described below. For more about the firewall, see Firewall Management on Linux.
Create a directory for Keycloak and generate passwords for the database and starter account. The commands write both directly to the .env file without displaying them:
mkdir -p /opt/keycloak
cd /opt/keycloak
echo "POSTGRES_PASSWORD=$(openssl rand -hex 24)" >> .env
echo "KC_ADMIN_PASSWORD=$(openssl rand -hex 16)" >> .env
chmod 600 .env
Then create the docker-compose.yml file, for example using nano docker-compose.yml, and add the following content. Replace keycloak.example.com with your domain:
services:
postgres:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_DB: keycloak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- ./postgres:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U keycloak -d keycloak"]
interval: 10s
retries: 10
keycloak:
image: quay.io/keycloak/keycloak:26.8
command: start
restart: unless-stopped
environment:
KC_DB: postgres
KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak
KC_DB_USERNAME: keycloak
KC_DB_PASSWORD: ${POSTGRES_PASSWORD}
KC_HOSTNAME: https://keycloak.example.com
KC_HTTP_ENABLED: "true"
KC_PROXY_HEADERS: xforwarded
KC_HEALTH_ENABLED: "true"
KC_BOOTSTRAP_ADMIN_USERNAME: admin
KC_BOOTSTRAP_ADMIN_PASSWORD: ${KC_ADMIN_PASSWORD}
ports:
- "127.0.0.1:8080:8080"
depends_on:
postgres:
condition: service_healthy
Here’s what the important lines mean:
command: start: starts Keycloak in production mode. Thestart-devmode used in many examples is only intended for testing.KC_DBandKC_DB_URL: Keycloak stores everything in PostgreSQL instead of thedev-filefile-based database.KC_HOSTNAME: the public address, includinghttps://. Keycloak uses it to generate all links and token URLs.KC_HTTP_ENABLEDandKC_PROXY_HEADERS: Keycloak communicates with the reverse proxy over HTTP internally and reads theX-Forwarded-*headers to determine that the browser is connected over HTTPS.KC_BOOTSTRAP_ADMIN_*: creates a temporary admin account in the master realm on the first startup.127.0.0.1:8080:8080: Keycloak is accessible only from the server itself, and from outside only through the reverse proxy.
Pull the images and start Keycloak:
docker compose pull
docker compose up -d
docker compose logs -f keycloak
Keycloak is ready when the log shows a line containing started in and Listening on: http://0.0.0.0:8080. Exit the log view with Strg+C.
First, set up the reverse proxy in the next section. Because KC_HOSTNAME points to your domain, Keycloak redirects every request there. Then open https://keycloak.example.com/admin/ and sign in as the admin user. To view the password, run grep KC_ADMIN_PASSWORD /opt/keycloak/.env on the server. Then create your own admin account as described above under “Create a Permanent Admin Account” and delete the starter account.
To access Keycloak from outside, put a reverse proxy with a valid certificate in front of it. For the basics, see Set Up a Reverse Proxy with nginx and Create an SSL Certificate (Let's Encrypt).
Keycloak needs the proxy to send the X-Forwarded-For, X-Forwarded-Proto, X-Forwarded-Host, and X-Forwarded-Port headers. According to the documentation, the proxy must set and overwrite these headers, not just append them. Here’s a server block for nginx:
server {
listen 80;
server_name keycloak.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port 443;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
}
}
Then get the certificate with certbot --nginx -d keycloak.example.com. Certbot adds HTTPS and HTTP redirection to the block. The larger buffers prevent the nginx error “upstream sent too big header”, which Keycloak’s extensive login headers can otherwise trigger.
The documentation recommends not exposing the /admin/ and /realms/master/ paths to the internet. In nginx, use allow and deny to permit access only from your own IP address or VPN. Your applications use their own realm anyway.
For more tips on securing your server, see Linux Security Tips.
The most important data is in the PostgreSQL database: all realms, users, clients, and settings are stored there. Back it up with pg_dump:
cd /opt/keycloak
docker compose exec -T postgres pg_dump -U keycloak -d keycloak > keycloak-db-$(date +%F).sql
Also back up the .env and docker-compose.yml files. Don’t store the backup only on the VPS itself.
Back up the database first, then read the new version’s release notes and upgrading guide. Then update the version in the image in docker-compose.yml, for example to quay.io/keycloak/keycloak:26.9, and restart Keycloak:
cd /opt/keycloak
docker compose pull
docker compose up -d
When Keycloak starts, it updates the database to the new version. With the 26.8 tag, docker compose pull also gets bug fixes within that version. You can see the installed version in the Administration Console, in the master realm under Server info.
After setup, connect your first application using OpenID Connect. The example with Grafana below walks you through the whole process. You can connect other apps using the same steps; only the settings on the app side differ.
A realm is a separate area with its own users, roles, and clients. The master realm is only for managing Keycloak. Create a separate realm for your applications:
- Click Manage realms on the left, then Create realm.
- Enter a name under Realm name, for example
apps, and click Create. - Under Users > Add user, create the users who will later sign in to your apps. Add an email address, as many apps require one. Set the password as described above in the Credentials tab.
Your realm’s sign-in page for users is at https://keycloak.example.com/realms/apps/account. Users can change their password and set up two-factor authentication there.
You’ll need a running Grafana installation with its own domain. See Install Grafana. In the examples, grafana.example.com is Grafana and keycloak.example.com is Keycloak.
In Keycloak (realm apps):
- Click Clients on the left, then Create client.
- Leave Client type set to OpenID Connect, enter
grafanaas the Client ID, and click Next. - Set Client authentication to On, leave Standard flow enabled, and click Next.
- Enter
https://grafana.example.com/login/generic_oauthunder Valid redirect URIs andhttps://grafana.example.comunder Web origins. Click Save. - In the Credentials tab, copy the Client Secret.
In Grafana: Grafana recognizes Keycloak as “Generic OAuth”. For a Grafana installation using Docker Compose, add the following values under environment in the docker-compose.yml file and restart Grafana with docker compose up -d:
- GF_AUTH_GENERIC_OAUTH_ENABLED=true
- GF_AUTH_GENERIC_OAUTH_NAME=Keycloak
- GF_AUTH_GENERIC_OAUTH_CLIENT_ID=grafana
- GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET=CLIENT_SECRET
- GF_AUTH_GENERIC_OAUTH_SCOPES=openid profile email
- GF_AUTH_GENERIC_OAUTH_AUTH_URL=https://keycloak.example.com/realms/apps/protocol/openid-connect/auth
- GF_AUTH_GENERIC_OAUTH_TOKEN_URL=https://keycloak.example.com/realms/apps/protocol/openid-connect/token
- GF_AUTH_GENERIC_OAUTH_API_URL=https://keycloak.example.com/realms/apps/protocol/openid-connect/userinfo
If you use Grafana as a One-Click App, enter the same information in Grafana under Administration > Authentication > Generic OAuth. Sign in with Keycloak will then appear on Grafana’s login page. Keycloak lists all the addresses for your realm at https://keycloak.example.com/realms/apps/.well-known/openid-configuration; many apps only need this one address.
In the apps realm, open a user under Users and select Configure OTP under Required user actions. The next time they sign in, Keycloak displays a QR code for the user to scan with an authenticator app. From then on, Keycloak asks for the code at every sign-in, including for all connected apps. Protect your own admin account in the master realm in the same way.
- Resources: Keycloak is a Java application and needs significantly more RAM than many of the apps it protects. Plan for at least 2 GB of RAM for Keycloak and the database.
- Complexity: Realms, clients, roles, and authentication flows have a learning curve. Don’t change the default flows until you understand what they do, or you may lock yourself out.
- The app must support it: Single Sign-on only works with apps that support OpenID Connect, OAuth 2.0, or SAML. Keycloak doesn’t include a built-in proxy for apps without their own sign-in.
- An outage affects all apps: If Keycloak is unavailable, you also won’t be able to sign in to connected apps using SSO. Keep a local admin account as a backup in important apps.
You’ve set up Keycloak on your VPS, created a permanent admin account, created your own realm, and connected Grafana using OpenID Connect. From now on, you can manage users, roles, and two-factor authentication for your apps in one place.
Prefer not to manage Docker yourself? Use the easy One-Click App approach described above under “The Easy Way: Keycloak as a One-Click App”.