Skip to content

API and App Server Setups

This document covers all the steps taken to configure and set up the four servers that are being used to host various APIs and web applications. This process largely follows the multi-app deployment document on the auth-template project on GitHub because the auth template was used for many of the applications being served there.

There are four servers that make up the infrastructure to support these applications:

  1. test-apis.shore.mbari.org: Hosting test APIs
  2. test-app.shore.mbari.org: Hosting test web applications
  3. apis.shore.mbari.org: Hosting production APIs
  4. app.shore.mbari.org: Hosting production web applications

Port Mappings

In order to manage multiple APIs and applications on these servers, I need to maintain a mapping of the ports so that I can create appropriate virtual hosts on each server.

Application API Port Web App Port
TrackingDB 8001 3001

Setting Up the Servers

I sent Peter an email to prepare him for this work:

Hey there,

You've probably seen the two VM requests for the monitoring stuff, but I wanted to give you a heads up on for other VMs I am going to be requesting. I thought it would be easier to detail out things in an email vs individual VM tickets (I will still submit tickets though).

I am trying to set up some infrastructure to create a templated way to build web applications (I have to build a few in the near future) in a consistent way that integrate our OAuth stuff, SCIM, metrics and monitoring. I put together a diagram of the system I am trying to build to test all this out:

https://docs.mbari.org/internal/mbari-ia/logical-view/

I want to set things up so that the application APIs are separate from the web applications so that other applications can use the APIs (like data processing and ETL scripts). I also want to set things up so that one server can host multiple APIs and other server can host multiple web applications. If you're curious, you can find the GitHub project template here:

https://github.com/mbari-org/oauth-local-template

And the multiple apis/apps per server notes here:

https://github.com/mbari-org/oauth-local-template/blob/main/docs/multi-app-deployment.md

So, to create a test and production environment for this, I will be asking for 4 new VMs:

apis.shore.mbari.org app.shore.mbari.org test-apis.shore.mbari.org test-app.shore.mbari.org

The APIs would be name spaced by adding a path parameter to the end (like apis.shore.mbari.org/app1) and the web applications would be divided out by virtual hosts (like app1.app.shore.mbari.org). So to support that kind of namespacing, each of the VMs should have a native Nginx server installed so I can add these divisions.

Thoughts? Happy to chat about it too.

Thanks,

Kevin

He was good with all this, so I submitted four tickets for these servers. He set up each one with the response:

The VM has been set up. NGINX was installed on the host and the ACME SSL certs are placed where they automatically apply to all virtual servers set up in the nginx process. Also, your "kgomes" user has full sudo permissions and is in the "docker" group.

Please poke around and let me know if anything is missing.

So here we go:

Test APIs (test-apis.shore.mbari.org)

  1. I ssh'd in using kgomes and my AD password. Got in no problem.
  2. Before going any further, I wanted to configure ssh keys so I can connect to the server and my account on the server can checkout code from GitHub
  3. On my mac, I ran cat ~/.ssh/mbari-shore.pub | ssh kgomes@test-apis.shore.mbari.org "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys" and edited the .ssh/config file to add the test-apis.shore.mbari.org host to the list of hosts using the mbari-shore key. Now I can ssh without a password.
  4. Next I copied the keys from my Mac to the server that I use to interact with GitHub by scp'ing them over using scp id_ed25519_githu* test-apis.shore.mbari.org:~/.ssh
  5. The last step was to create ~/.ssh/config and tell it to use this key for GitHub by entering:

    Host github.com
        HostName github.com
        IdentityFile ~/.ssh/id_ed25519_github
        IdentitiesOnly yes
    
  6. Next, I did a quick check and Nginx is already running so that's great.

  7. The first application I want to deploy is the Test Tracking Database Application. So, first, I created the /etc/nginx/conf.d/apis.conf file and added:

    # TrackingDB API
    server {
        listen 443 ssl;
        server_name trackingdb.test-apis.shore.mbari.org;
    
        ssl_certificate     /etc/nginx/ssl/test-apis.shore.mbari.org-chain.crt;
        ssl_certificate_key /etc/nginx/ssl/test-apis.shore.mbari.org.key;
    
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
    
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    
        location /metrics { return 404; }
    
        location / {
            proxy_pass http://localhost:8001;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    
  8. Then I restarted the Nginx server using sudo nginx -t && sudo systemctl reload nginx. That should setup the virtual host.

  9. Since this is the first application to install on this server, I created the apps directory using sudo mkdir /opt/apps and then gave myself permission by using sudo chown kgomes:dialout /opt/apps.
  10. Since this was my first checkout, I need to set up an SSH key for github.
  11. Next, I need to install the application. I cloned the repo using git clone git@github.com:mbari-org/mbari-tracking-app.git /opt/apps/trackingdb
  12. I changed into the /opt/apps/trackingdb directory and created a new docker-compose.api-server.yml file which contains the port overrides:

    # Overrides for running only the API on a shared server.
    # Usage: docker compose -f docker-compose.prod.yml -f docker-compose.api-server.yml up -d api db
    services:
    api:
        ports:
        - "8001:8000"    # ← this app's assigned API port
    
  13. I copied the