> For the complete documentation index, see [llms.txt](https://docs.facephi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.facephi.com/docs.facephi-en/sdks/backend-sdk/backend1n/installation/installation_instructions.md).

# Service installation and deployment

## 1. Installation and deployment

The service is containerized and there is a Docker image in a Facephi repository.

```bash
docker login facephicorp.jfrog.io
user: username
pass: token
```

```bash
docker pull facephicorp.jfrog.io/docker-pro-fphi/facephi-backend1n-service:#VERSION#
```

Where `#VERSION#` is the specific Version number we want to download (for example, `4.2.0`).

## 2. Databases.

Before the service starts, the administrator must create and configure the relevant databases, as well as a specific user for the service. This **user/password must be specified in the connection string**. The database technology used by the service will be decided at startup, based on the prefix of this connection string.

## 2.1 MongoDB

The environment variable `DATABASE_CONNECT_STRING` must point to MongoDB.

```bash
# Accepted MongoDB prefixes.
# mongodb://
# mongodb+srv://
export DATABASE_CONNECT_STRING=mongodb://user:pass@localhost:32902/?directConnection=true&serverSelectionTimeoutMS=5000
```

The service itself will create a database called `db-backend`, if it does not already exist. It will host two collections:

* `media` to store images.
* `patterns` to store biometric patterns.

## 2.2 PostgreSQL

Likewise, the environment variable `DATABASE_CONNECT_STRING` must point to PostgreSQL.

```bash
# Accepted PostgreSQL prefixes.
# postgres://
# postgresql://
export DATABASE_CONNECT_STRING=postgresql://user:pass@localhost:32903/db_backend
```

The PostgreSQL case differs slightly from MongoDB. A database named `db_backend` must have been created beforehand by the system administrator. PostgreSQL makes it difficult to create databases from the client itself. An example of the prior commands the administrator should run would be:

```bash
sudo -u postgres psql -c "CREATE ROLE backend1n WITH LOGIN PASSWORD 'backend1n';"
sudo -u postgres psql -c "CREATE DATABASE db_backend OWNER backend1n;"
sudo -u postgres psql -d db_backend -c "GRANT ALL ON SCHEMA public TO backend1n;"
```

Inside the PostgreSQL DB, the service will create two tables, just as it did with MongoDB:

* `media` to store images.
* `patterns` to store biometric patterns.

## 3. docker-compose

One way to deploy the service is to create a file `docker-compose.yml` with the following content, changing the value of the variables as needed.

```yaml
version: '3.7'

services:
  backend1n-service:
    ports:
      - "6982:6982"
    volumes:
      - ~/backend1n/config:/service/config
    environment:
      DATABASE_CONNECT_STRING: mongodb://127.0.0.1:27017
    image: facephicorp.jfrog.io/docker-pro-fphi/facephi-backend1n-service:#VERSION#
    container_name: facephi-backend1n-service
```

First, you must create a user directory with read and write permissions, for example `~/backend1n/config`, and place these two files:

* The SelphID license file `license.lic` (required).
* The service configuration file `config.json` (optional).

> In that same directory, the service will write the log files.

In addition, you must provide the environment variable `DATABASE_CONNECT_STRING` with the database connection string (for example, `mongodb://127.0.0.1:27017`). For debugging purposes, you can disable database operations by providing the value `no-db`. Example: `DATABASE_CONNECT_STRING=no-db`

Run the following command, inside the folder where the file `docker-compose.yml`, to deploy the service:

```
docker compose up
```

## 4. Additional configuration

Following the steps in **section 2**, the service is ready to start. If you need to create additional volumes for the different resource types, you can follow these additional configurations.

### 4.1 License

You can mount an additional volume and define the environment variable `LICENSE_PATH`, in case you want to install the license outside the usual configuration directory.

```yaml
version: '3.7'

services:
  backend1n-service:
    ports:
      - "6982:6982"
    volumes:
      - ~/backend1n/config/config.json:/service/config/config.json
      - ~/backend1n/config/license.lic:/service/license/license.lic
    environment:
      LICENSE_PATH: /service/license
      DATABASE_CONNECT_STRING: mongodb://127.0.0.1:27017
    image: facephicorp.jfrog.io/docker-pro-fphi/facephi-backend1n-service:#VERSION#
    container_name: facephi-backend1n-service
```

> `LICENSE_PATH` is a folder with the file `license.lic` and read/write permissions. In this example, `~/backend1n/license/license.lic`.

### 4.2 Service configuration

You can specify another location for the file `config.json` with the service parameters:

```yaml
version: '3.7'

services:
  backend1n-service:
    ports:
      - "6982:6982"
    volumes:
      - ~/backend1n/config:/service/config
      - ~/backend1n/service:/service
    environment:
      CONFIG_FILE: /service/config.json
      DATABASE_CONNECT_STRING: mongodb://127.0.0.1:27017
    image: facephicorp.jfrog.io/docker-pro-fphi/facephi-backend1n-service:#VERSION#
    container_name: facephi-backend1n-service
```

> `CONFIG_FILE` is the path to the configuration file in the additional volume.

If no `config.json`, these default values will be used:

```json
{
    "port": 6969,                   # Service port number.
    "number_of_threads": 0,         # Number of I/O threads, 0 = number of CPU cores.
    "connection_timeout": 0,        # Connection lifetime without read or write.
    "keep_alive_request_number": 0, # Sets the maximum number of requests that can be served over a keep-alive connection.
                                    # Once the maximum number of requests is reached, the connection is closed.
                                    # The default value of 0 means unlimited.
    "client_max_body_size": 100,    # Maximum body size allowed in requests, in Mb.
                                    # The default value is 100 Mb.
    "logger_path" : "./logs",       # Sets the path where the log files are stored.
    "logger_level" : "info",        # Possible values are [trace|debug|info|warning|error|critical|off].
    "logger_rotation" : "daily",    # Possible values are [hourly|daily].
    "logger_max_files" : 0,         # The default value of 0 means unlimited.
    "auth_enabled": false,          # Enables JWT authentication for protected endpoints.
    "auth_jwt_secret": "",         # Shared secret used to validate JWT HS256 signatures.
    "auth_accept_authorization_header": true, # Accepts Authorization: Bearer <jwt>.
    "auth_accept_api_key_header": true,       # Accepts the API Key header with the JWT token.
    "auth_api_key_header_name": "x-api-key" # Name of the API Key header when enabled.
}
```

The same JWT parameters can be injected through environment variables using the prefix `FACEPHI_BACKEND1N_REST_`. For example:

```yaml
services:
  backend1n-service:
    environment:
      DATABASE_CONNECT_STRING: mongodb://127.0.0.1:27017
      FACEPHI_BACKEND1N_REST_AUTH_ENABLED: "true"
      FACEPHI_BACKEND1N_REST_AUTH_JWT_SECRET: shared-secret
      FACEPHI_BACKEND1N_REST_AUTH_ACCEPT_AUTHORIZATION_HEADER: "true"
      FACEPHI_BACKEND1N_REST_AUTH_ACCEPT_API_KEY_HEADER: "true"
      FACEPHI_BACKEND1N_REST_AUTH_API_KEY_HEADER_NAME: x-api-key
```

### 4.3 Debug and usage path

Lastly, you can specify a concrete volume/path with read and write permissions for the SDK debugging information and the SDK usage data, using the environment variables `DEBUG_PATH` and `USAGE_PATH`:

```yaml
version: '3.7'

services:
  backend1n-service:
    ports:
      - "6982:6982"
    volumes:
      - ~/backend1n/config:/service/config
      - ~/backend1n/debug:/service/debug
      - ~/backend1n/usage:/service/usage
    environment:
      DEBUG_PATH: /service/debug
      USAGE_PATH: /service/usage
      DATABASE_CONNECT_STRING: mongodb://127.0.0.1:27017
    image: facephicorp.jfrog.io/docker-pro-fphi/facephi-backend1n-service:#VERSION#
    container_name: facephi-backend1n-service
```

### 4.4 Network configuration

To be able to connect to our license servers, you must add the following rules to your firewall rules:

| IP           | Port | Type   |
| ------------ | ---- | ------ |
| 52.223.22.71 | 443  | TCP/IP |
| 35.71.188.31 | 443  | TCP/IP |
| 75.2.113.112 | 443  | TCP/IP |
| 99.83.149.57 | 443  | TCP/IP |

Next, add the following `DNS` to your whitelist:

```bash
https://api.cryptlex.com:443
https://api.eu.cryptlex.com:443
```
