OpenJDK
Installing Keycloak with OpenJDK for production env with PostgreSQL in Ubuntu/Debian
Make sure your machine or container platform can provide sufficient memory and CPU for your desired usage of Keycloak. See Concepts for sizing CPU and memory resources for more on how to get started with production sizing.
Install openjdk 21 using
sudo apt install openjdk-21-jdk
Lets create a directory for Keycloak.
sudo mkdir /app sudo chown user /app cd /app mkdir keycloak cd keycloak
Download and extract Keycloak’s latest version from Keycloak here. You can use following command to download and extract it.
wget https://github.com/keycloak/keycloak/releases/download/25.0.2/keycloak-25.0.2.tar.gz tar -xvzf keycloak-25.0.2.tar.gz
After extracting this file, you should have a directory that is named keycloak-[your_version_here]. Let’s rename it to Keycloak.
mv keycloak-25.0.2 keycloak rm keycloak-25.0.2.tar.gz
Now our setup is ready, lets move to creating configuration.
Running Keycloak
Option 1 To get Keycloak running with SSL- Lets create dir and certificates
mkdir certs cd certs openssl genrsa -out keycloak.key 2048 openssl req -new -x509 -sha256 -key keycloak.key -out keycloak.crt -days 365
- Now go back to keycloak dir
cd ..
- Start server command (You can test it but skip it for now)
bin/kc.sh start --https-certificate-file=/app/keycloak/certs/keycloak.crt --https-certificate-key-file=/app/keycloak/certs/keycloak.key --hostname https://yourdomain.com
Option 2 To get Keycloak running without SSL
To run Keycloak without SSL (If you want to break your SSL on gateway or CDN and internal VNET or VPC routing can be without SSL)- Start server (You can test it but skip it for now)
bin/kc.sh start --hostname-strict false --proxy-headers forwarded --http-enabled true
- Lets create dir and certificates
Above will start server but we have not configured databases. To run keycloak with desired configuration we will create
run_keycloak.sh
script using following commandtouch run_keycloak.sh chmod +x run_keycloak.sh
Add following code in it
export KC_DB=postgres export KEYCLOAK_ADMIN=admin export KEYCLOAK_ADMIN_PASSWORD=admin export KC_DB_URL="jdbc:postgresql://127.0.0.1:5432/keycloak" export KC_DB_USERNAME=postgres export KC_DB_PASSWORD=password #bin/kc.sh start --hostname-strict false --proxy-headers forwarded --http-enabled true #--https-certificate-file=/app/keycloak/certs/keycloak.crt --https-certificate-key-file=/app/keycloak/certs/keycloak.key https://yourdomain.com/
Now you might have noticed we have two lines commented in the file above. According to your requirement uncomment run script.
- If SSL is required then uncomment last line change domain.
- If SSL is not required then uncomment second last line.
Run the script using following command
./run_keycloak.sh
To create service so that keycloak will start as soon as server reboots. To create service use following command Lets create service called keycloak using following command
sudo nano /etc/systemd/system/keycloak.service
paste following code in it
[Unit] Description = keycloak Webserver Daemon After = network.target [Service] PIDFile = /app/keycloak/keycloak-webserver.PIDFile WorkingDirectory = /app/keycloak ExecStart = /app/keycloak/run_keycloak.sh ExecStop = /bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target
once copied run following command to enable and start service
systemctl daemon-reload sudo systemctl enable keycloak.service sudo systemctl start keycloak.service