Install Keycloak in ubuntu behind nginx reverse proxy using postgresql database

Below we show how to install Keycloak in ubuntu behind Nginx proxy and also use PostgreSQL as its database.



//download keycloadk and unzip
wget https://github.com/keycloak/keycloak/releases/download/19.0.1/keycloak-legacy-19.0.1.zip
//create admin user
cd keycloak-legacy-19.0.1/
./bin/add-user-keycloak.sh -r master -u admin -p mypassword
//start keycloak
./bin/standalone.sh
//enable proxy for keycloak by adding proxy-address-forwarding="true"
vim standalone/configuration/standalone.xml
 <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true" proxy-address-forwarding="true"/>
////nginx reverse proxy , Keycloak runs on port 8080
vim /etc/nginx/nginx.conf
      location / {
                    proxy_pass http://localhost:8080/;
                    proxy_set_header X-Forwarded-For $host;
                    proxy_set_header X-Forwarded-Proto $scheme;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection 'upgrade';
                    proxy_set_header Host $host;
              }
/////change database from h2 to PostgreSQL
  cd /home/ubuntu/keycloak-legacy-19.0.1/modules/system/layers/keycloak/org
  mkdir postgresql
  cd postgresql
  mkdir main
  wget https://jdbc.postgresql.org/download/postgresql-42.2.26.jre6.jar
///create module.xml and copy contents below and save the file
 cd /home/ubuntu/keycloak-legacy-19.0.1/modules/system/layers/keycloak/org/postgresql/main
 vim module.xml
 <?xml version="1.0" encoding="UTF-8"?>
  <module xmlns="urn:jboss:module:1.3" name="org.postgresql">
      <resources>
          <resource-root path="postgresql-42.2.26.jar"/>
      </resources>
      <dependencies>
          <module name="javax.api"/>
          <module name="javax.transaction.api"/>
      </dependencies>
  </module>
///change database configuration and add postgersql
cd /home/ubuntu/keycloak-legacy-19.0.1//standalone/configuration
//add driver
vim standalone.xml
      <drivers>
          <driver name="postgresql" module="org.postgresql">
              <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
          </driver>
          <driver name="h2" module="com.h2database.h2">
              <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
          </driver>
       </drivers>
/////add datasources
<datasources>
                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                    <driver>h2</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>
                <datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
                        <connection-url>jdbc:postgresql://localhost/keycloakdb</connection-url>
                  <driver>postgresql</driver>
                  <pool>
                      <max-pool-size>20</max-pool-size>
                  </pool>
                  <security>
                      <user-name>Your PostgreSQL username</user-name>
                      <password>Your PostgreSQL password</password>
                  </security>
                </datasource>
                <drivers>
                    <driver name="postgresql" module="org.postgresql">
                         <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
                    </driver>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                    </driver>
                </drivers>
            </datasources>

Leave a Reply

Your email address will not be published. Required fields are marked *