Skip to content

How to Set Up Keycloak on a Linux Server – Single Sign-On and User Management for Your Apps

Set up Keycloak on a Linux server: use the One-Click App with HTTPS or install it manually with Docker Compose, then create an admin account, realm, and OpenID Connect connection.

29 November 202612 min read0 Views0 helpful

Introduction

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.

When Keycloak Makes Sense on a VPS

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.

Keycloak with or without PostgreSQL?

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 PostgresKeycloak
DatabaseSeparate PostgreSQL containerBuilt-in file-based database dev-file (H2)
Intended forProduction use with users and appsTrying it out and testing
Data stored inPostgreSQL volumeVolume /opt/keycloak/data in the Keycloak container
RAM requirementsSlightly 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 Easy Way: Keycloak as a One-Click App

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.

Requirements

  • 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.

Install

  1. Open your VPS in the ZapX dashboard and go to the One-Click Apps tab.
  2. Search for Keycloak in the app catalog. You’ll see two cards. Select Keycloak with Postgres and click Install app.
  3. 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 sso or login. Keycloak needs a domain with HTTPS for sign-in to work with other apps.
  4. You don’t need to enter anything under App configuration. TIMEZONE sets the time zone, and POSTGRESQL_DATABASE sets the database name. Leave KC_HTTP_ENABLED, KC_HEALTH_ENABLED, and KC_PROXY_HEADERS at their default values.
  5. 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.

First Login: Find Your Login Details

Keycloak is installed with an admin account whose username and password are generated automatically:

  1. Click the gear icon on the app card and go to App environment.
  2. Under Automatically generated values, you’ll find SERVICE_USER_ADMIN (username) and SERVICE_PASSWORD_ADMIN (password). Click the eye icon to reveal a value, or the icon next to it to copy it.
  3. 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.
  4. Sign in using the username and password from the App environment.

Create a Permanent Admin Account

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:

  1. You’re in the master realm. Click Users on the left, then Add user.
  2. Enter a name under Username, optionally add your email address, and click Create.
  3. Go to the Credentials tab and click Set password. Set a long password, switch Temporary to Off, and click Save.
  4. Go to the Role mapping tab, click Assign role, select Realm roles, check admin, and confirm by clicking Assign.
  5. Sign out at the top right and sign back in with the new account. Then delete the starter account under Users.
Warning: Changing the password in the app environment no longer has any effect

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.

Hostname and HTTPS

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”.

Updates and Backups

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.

Warning: Back up before updating

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.

Manual Installation

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.

Requirements

  • 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 --version and docker 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.

HardwareMinimumRecommended
CPU1 vCPU core2 vCPU cores
RAM2 GB4 GB
Storage10 GB20 GB

Prepare the Firewall

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:

Code
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
Warning: Docker bypasses ufw

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.

Install with Docker Compose

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:

Code
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:

Code
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. The start-dev mode used in many examples is only intended for testing.
  • KC_DB and KC_DB_URL: Keycloak stores everything in PostgreSQL instead of the dev-file file-based database.
  • KC_HOSTNAME: the public address, including https://. Keycloak uses it to generate all links and token URLs.
  • KC_HTTP_ENABLED and KC_PROXY_HEADERS: Keycloak communicates with the reverse proxy over HTTP internally and reads the X-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:

Code
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 Visit

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.

HTTPS Access Through a Reverse Proxy

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:

Code
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.

Tip: Don’t expose administration publicly

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.

Back Up Your Data

The most important data is in the PostgreSQL database: all realms, users, clients, and settings are stored there. Back it up with pg_dump:

Code
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.

Update Keycloak

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:

Code
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.

Connect Your First Application

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.

Create Your Own Realm

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:

  1. Click Manage realms on the left, then Create realm.
  2. Enter a name under Realm name, for example apps, and click Create.
  3. 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.

Create a Client for Grafana

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):

  1. Click Clients on the left, then Create client.
  2. Leave Client type set to OpenID Connect, enter grafana as the Client ID, and click Next.
  3. Set Client authentication to On, leave Standard flow enabled, and click Next.
  4. Enter https://grafana.example.com/login/generic_oauth under Valid redirect URIs and https://grafana.example.com under Web origins. Click Save.
  5. 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:

Code
      - 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.

Require Two-Factor Authentication

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.

Limitations

  • 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.

Conclusion

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”.

Free tools

Related tools for servers and developers

View all

FAQ

Frequently asked questions

The One-Click App is the quickest option: Keycloak runs over HTTPS on your domain within minutes, hostname and proxy settings are configured, and the admin account and database password are generated automatically. You can install updates from the dashboard. Manual installation with Docker Compose is useful if you don’t use One-Click Apps on the server, want to run Keycloak behind your own reverse proxy, or want to control the version yourself.
For the One-Click App, click the gear icon on the app card and open App environment. Under Automatically generated values, you’ll find SERVICE_USER_ADMIN (username) and SERVICE_PASSWORD_ADMIN (password). Use them to sign in to the Administration Console at /admin/. Then create your own admin account, because the generated account is only a temporary starter account.
The admin account created on the first startup is deliberately intended only as a starter account. In the master realm, create your own account under Users, set a password with Temporary set to Off, assign it the admin realm role under Role mapping, and then delete the starter account. The notice will then disappear.

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 people found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP-Hosting support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.