Skip to main content

1. Connect to Your EC2 Server

Use SSH to connect to your EC2 instance:
You should see a prompt like:

2. Generate SSL Key and CSR

Create the private key and certificate signing request (CSR):
Check the generated files:

3. Obtain SSL Certificates

  • Go to your SSL provider (e.g., GoDaddy, Namecheap, or any hosting site that sells SSL certificates).
  • Copy the contents of linqra.csr and paste it into the provider’s CSR box.
  • Submit and wait for approval.
  • Download the resulting ZIP file (Server type: Other), e.g. linqra.com.zip.
You will receive:
  • f8d48b14580110e.crt
  • f8d48b14580110e.pem (identical to the .crt)
  • gd_bundle-g2.crt

4. Upload Certificates to the Server

Use FileZilla, IntelliJ, or another SFTP tool to upload the two .crt files to /tmp/ on your EC2 server. Then move them to the target directory:
Note: The .pem and .crt files from your provider are often identical.

5. Generate the Certificate Chain

Switch to root and concatenate the certificate and bundle to create the chained certificate:
Check the resulting files:

You now have all the necessary SSL files (linqra.key, linqrachained2025.crt) to configure your web server for HTTPS on linqra.com.

6. Set Up AWS EC2 Folders and Prepare for Deployment

Create the necessary directories for your application and logs:

7. Manage Nginx

You may need these commands to manage the Nginx service:

8. Configure Nginx for SSL and React Frontend

Edit the default Nginx site configuration:
Example configuration:

9. Enable the Nginx Site

Symlink the sites-available file to the sites-enabled folder:

10. Set Permissions for Deployment

Navigate to your deployment directory and set the correct permissions:

Note:
  • Always edit the sites-available file for Nginx configuration.
  • Adjust the paths and domain names as needed for your environment.

11. Configure GitHub Actions Repository Secrets

To enable automated deployment and secure access, add the following secrets to your GitHub repository:

1. SSH Key for EC2

Generate or use your existing SSH private key.
Example (do not use this key, just for illustration):
Add this as a new repository secret named:

2. EC2 Host and Deployment Info

Add the following repository secrets:

3. AWS Credentials


4. Web Application Secrets

Copy these from your web environment files and add as repository secrets:
How to add a secret:
  1. Go to your GitHub repository.
  2. Click on Settings > Secrets and variables > Actions.
  3. Click New repository secret.
  4. Enter the name and value, then click Add secret.

Tip:
Never commit your private keys or secrets to the repository. Always use GitHub Actions secrets for sensitive data.
GitHub Actions Repository Secrets Example
With the above GitHub secrets configured, your ci.yml workflow will automatically deploy all necessary files to your EC2 server whenever new code is merged into the master branch. This ensures a secure and automated deployment process. We’ll cover the Dockerization steps for your application in the next section.

12. Final GitHub Actions CI/CD Pipeline (ci.yml)

Below is the complete ci.yml workflow file used for automated deployment.
Once code is merged into the master branch, this pipeline will automatically build and deploy your application to the EC2 server—no additional manual steps are required.
You should see a prompt like:

2. Initialize the Replica Set

At the mongosh prompt, run:
You should see output similar to:

3. Check Replica Set Status

You should see a status object with PRIMARY and SECONDARY members.
Note:

14. First Initial Data

After initializing the MongoDB replica set, you need to create the initial user and data for your application.

1. Exit the MongoDB Shell

If you are still inside the mongosh shell, type:

2. Run the Initial Setup API Call

Use curl to create the first user and initialize the database:

3. Verify Data in MongoDB

Connect to MongoDB and list the databases to confirm the Linqra database was created:
You should see output similar to:
You should now see the Linqra database listed.

15. Keycloak Configuration for EC2

Access Keycloak Admin Console

Access the Keycloak Admin Console at:

1. Create New Realm

  1. Log in to the Keycloak Admin Console
  2. Click on the dropdown in the top-left corner (default shows “master”)
  3. Click “Create Realm”
  4. Configure the realm:
    • Realm name: Linqra
    • Display name: Linqra
    • Frontend URL: https://linqra.com/keycloak
  5. Ensure “Enabled” is switched ON
  6. Click “Create”

2. Create New Client

General Settings

  1. Navigate to “Clients” in the left sidebar
  2. Click “Create client”
  3. Configure the general settings:
    • Client ID: linqra-gateway-client
    • Name: Linqra Gateway Client
    • Description: Linqra Gateway Client
    • Always display in UI: ON
    • Click “Next”

Access Settings

Configure the client’s access settings:
  • Root URL: https://linqra.com
  • Home URL: https://linqra.com
  • Valid redirect URIs: https://linqra.com/callback
  • Valid post logout redirect URIs: https://linqra.com
  • Web origins: https://linqra.com
  • Admin URL: https://linqra.com

Capability Config

Set up the authentication capabilities:
  • Client authentication: ON
  • Authorization: OFF
  • Authentication flow:
    • Standard flow: Enabled
    • Service accounts roles: Enabled
    • All other flows: Disabled

3. Create Client Role

  1. Navigate to the “Roles” tab in your client settings
  2. Click “Create role”
  3. Configure the role:
    • Role name: gateway_admin
    • Description: Gateway admin role for the client id: linqra-gateway-client
  4. Click “Save”

4. Create Realm Role

  1. Navigate to “Realm roles” in the left sidebar
  2. Click “Create role”
  3. Configure the role:
    • Role name: gateway_admin_realm
    • Description: Role to identify the belongings of the Linqra realm
  4. Click “Save”

5. Assign Service Account Roles

  1. Navigate to your client’s “Service accounts roles” tab
  2. You’ll see the service account username: service-account-linqra-gateway-client
  3. Assign both roles:
    • Client role: gateway_admin
    • Realm role: gateway_admin_realm

6. Create Client Scopes

a. Gateway Read Scope

  1. Navigate to “Client scopes” in the left sidebar
  2. Click “Create client scope”
  3. Configure the scope:
    • Name: gateway.read
    • Description: To read the gateway end points
    • Type: Default
    • Display on consent screen: ON
    • Include in token scope: ON
  4. Click “Save”

b. Team Scope

  1. Create another client scope:
    • Name: team.scope
    • Description: Restricts access to specific teams
    • Type: Default
    • Display on consent screen: ON
    • Include in token scope: ON
  2. Add a hardcoded claim mapper:
    • Name: teams
    • Category: Token mapper
    • Mapper Type: Hardcoded claim
    • Claim name: teams
    • Claim value: ["67d0aeb17172416c411d419e"]
    • Claim JSON type: JSON
    • Add to ID token: ON
    • Add to access token: ON
    • Add to userinfo: ON

7. Assign Roles to Client Scope

  1. Navigate to the gateway.read client scope
  2. Go to the “Scope” tab
  3. Assign both roles:
    • Client role: gateway_admin
    • Realm role: gateway_admin_realm

8. Add Client Scopes to Client

  1. Navigate to the linqra-gateway-client settings
  2. Go to the “Client scopes” tab
  3. Add both scopes as “Default”:
    • gateway.read
    • team.scope
The configuration above is specific to the EC2 deployment environment. Make sure all URLs are using https://linqra.com instead of localhost URLs.
After configuring Keycloak, you may need to restart the Keycloak service to ensure all changes take effect:

16. Useful Docker Commands and Maintenance

Check Running Containers

Example output:

Check Disk Usage


Stop All Containers and Remove Docker Images

Check Docker storage usage:
Remove all Docker data (network, volume, image):
Check disk usage again:
Check all containers (including stopped):

Restart or Build Specific Services

Restart only the Keycloak service:
Restart only the Eureka server:
You should see: https://linqra.com/eureka/ Restart only the API Gateway service:
Build and start all services:

Test and Debug

Refresh routes:
Get OpenID certs from Keycloak (on EC2):
Get OpenID certs from the gateway container:

Let me know if you want to add more commands or explanations!