# # Deployment

## # Deployment in single host

### # Set up the server

Prerequisite: Make sure you have `Docker` and `docker-compose` installed and running.

1. Pull webPDF Server image from our Docker hub

       # pull the latest published version
       docker pull harbor-us.cpdf.io:4430/websdk-sr/master:latest

       # pull a specific version
       docker pull harbor-us.cpdf.io:4430/websdk-sr/master:7.4.0

2. We use MongoDB as the database. If you don't use an existing MongoDB service, you should create an `init-mongo.js` file somewhere on your file system to initialize the MongoDB. If you use an existing MongoDB, ignore this step and go to next step.

       db.createUser(
       {
           user: 'mongo',
           pwd: 'mongo',
           roles: [
           {
               role: 'readWrite',
               db: 'com-foxit-webpdf'
           }
           ]
       }
       )

3. In the same directory of `init-mongo.js`, create the docker-compose.yml. If you use an existing MongoDB, you should modify `S8_WEBPDF_MONGO_URI` value and other fields in the `mongo` section like hostname, ports, username, password and database name, so webViewer Server can connect to your MongoDB.

    version: '2'
    services:
      mongo:
        image: mongo:4.2.2
        restart: always
        hostname: mongo
        ports:
          - 27017:27017
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: root
          MONGO_NON_ROOT_USERNAME: mongo
          MONGO_NON_ROOT_PASSWORD: mongo
          MONGO_INITDB_DATABASE: com-foxit-webpdf
        volumes:
          - ./init-mongo.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      webpdf:
        image: harbor.cpdf.io:4430/websdk-sr/master:latest
        ports:
          - 8890:8080
        environment:
          S8_WEBPDF_MONGO_URI: mongodb://mongo:mongo@mongo:27017/com-foxit-webpdf
          S8_WEBPDF_GSDK_SN:Copy the whole text string after `SN=` in the SN license file you received and paste here.
          S8_WEBPDF_GSDK_KEY:Copy the whole text string after `Sign=` in the KEY license file you received and paste here.
        depends_on: # to ensure MongoDB will be started before webViewer Server .
          - "mongo"

4. Navigate to the directory where you placed the `docker-compose.yml` in your terminal and run:

    docker-compose up -d

Now your webViewer Server is running at this point: `http://<host_ip_address or localhost>:8890/webpdf/`. For instance, my host IP address is `0.0.0.0`, then the server base URL would be `http://0.0.0.0:8890/webpdf/`.

### # Set up the webViewer

In SDK package, navigate to complete_webViewer_sr, open the index.html, configure the `viewerOptions.server/root:` as follows:

    var pdfui = new PDFUI({
    viewerOptions: {
    sr: {
    server: {
    root: 'http://0.0.0.0:8890/webpdf/',
    //other options
    }
    },
    libPath: '../../../lib',

    },

Now you get your webViewer running with server rendering. For how to start webViewer, refer to Quickly Run Samples.

## # Deployment in distributed environment

Prerequisite: Make sure you have `Docker` and `docker-compose` installed and running.

In a distributed environment, each webViewer Server will connect to a same MongoDB, and requires a share storage for the purpose of data caching. The MongoDB is used to log PDF conversion tasks like which PDFs have bee parsed, the parsing results and so on. The MongoDB data storing duration depends on the `S8_WEBPDF_CACHE_AGE:` your set in `docker-composer.yml`. And the share storage is used to cache the PDF files, rendered page image and other PDF related data.

### # Set up multiple webViewer servers in a single-node environment

This section will walk you through how to set up multiple webViewer Servers in a single-node environment.

Repeated the steps 1 ~ 2 in Deployment in single host, and then create the docker-compose.yml. Here you will add environment variable `S8_WEBPDF_CACHE_DIRS` for data caching, and add the mount in `volumes` so that the cache can be accessed by multiple webViewer servers. Below is the configuration example:

    version: '2'
    services:
      mongo:
        image: mongo:4.2.2
        restart: always
        hostname: mongo
        ports:
          - 27017:27017
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: root
          MONGO_NON_ROOT_USERNAME: mongo
          MONGO_NON_ROOT_PASSWORD: mongo
          MONGO_INITDB_DATABASE: com-foxit-webpdf
        volumes:
          - ./init-mongo.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      webpdf1:
        image: harbor.cpdf.io:4430/websdk-sr/master:latest
        ports:
          - 8890:8080
        environment:
          S8_WEBPDF_MONGO_URI: mongodb://mongo:mongo@mongo:27017/com-foxit-webpdf
          S8_WEBPDF_CACHE_DIRS: /srdata
          S8_WEBPDF_GSDK_SN:Copy the whole text string after `SN=` in the SN license file you received and paste here
          S8_WEBPDF_GSDK_KEY:Copy the whole text string after `Sign=` in the KEY license file you received and paste here
        volumes:
          - ./srdata:/srdata
        depends_on:
          - "mongo"
      webpdf2:
        image: harbor.cpdf.io:4430/websdk-sr/master:latest
        ports:
          - 8890:8080
        environment:
          S8_WEBPDF_MONGO_URI: mongodb://mongo:mongo@mongo:27017/com-foxit-webpdf
          S8_WEBPDF_CACHE_DIRS: /srdata
          S8_WEBPDF_GSDK_SN:Copy the whole text string after `SN=`in the SN license file you received and paste here
          S8_WEBPDF_GSDK_KEY:Copy the whole text string after `Sign=`in the KEY license file you received and paste here
        volumes:
          - ./srdata:/srdata
        depends_on:
          - "mongo"

### # Set up multiple webViewer servers in a multiple-node environment

In a multiple-node environment, you should create a File Share for data caching so they can be accessible by different nodes. The File Share can be implemented by any utils like NFS, SMB, k8s, ceph etc.

In the following section, we assume you have 3 machines, they are allocated as follows:

Name | IP | Description
--- | --- | ---
database | 192.168.0.2 | Provide MongoDB service, and SMB File Share service
webpdf-1 | 192.168.0.3 | Provide webViewer Server service
webpdf-2 | 192.168.0.4 | Provide webViewer Server service

database:

1. File Share: It is an assumption that this host has SMB File Share installed, and a mount point directory `/share` has been created.

2. MongoDB:

If you don't use an existing MongoDB service, you should create an `init-mongo.js` file somewhere on your file system to initialize the MongoDB. If you use an existing MongoDB, ignore this step and go to next step.

    db.createUser(
      {
        user: 'mongo',
        pwd: 'mongo',
        roles: [
          {
            role: 'readWrite',
            db: 'com-foxit-webpdf'
          }
        ]
      }
    )

In the same directory, create the `docker-compose.yml`. If you use an existing MongoDB, you should modify `S8_WEBPDF_MONGO_URI` value and other fields in the `mongo` section like hostname, ports, username, password and database name,so webViewer Server can connect to your MongoDB.

    version: '2'
    services:
      mongo:
        image: mongo:4.2.2
        restart: always
        hostname: mongo
        ports:
          - 27017:27017
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: root
          MONGO_NON_ROOT_USERNAME: mongo
          MONGO_NON_ROOT_PASSWORD: mongo
          MONGO_INITDB_DATABASE: com-foxit-webpdf
        volumes:
          - ./init-mongo.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

webpdf-1 node:

1. On the host where webViewer Server is installed, mount the share in database node to webpdf-1:

    mount -t cifs -o username=smb,password=password //192.168.0.2/share /mnt/share

2. Add the share to the list of permanently associated mount points in the /etc/fstab file, to avoid losing data after a system restart.

    echo "//192.168.0.2/share /mnt/share cifs username=smb,password=password 0 0" >> /etc/fstab`

3. Repeated the steps 1 ~ 2 in Deployment in single host, and then create the `docker-compose.yml`. Here, you will add environment variable `S8_WEBPDF_CACHE_DIRS` for data caching, and add the mount in `volumes` so that the cache can be shared by multiple webViewer servers. Below is the configuration example:

    version: '2'
    services:
      webpdf1:
        image: harbor.cpdf.io:4430/websdk-sr/master:latest
        ports:
          - 8890:8080
        environment:
          S8_WEBPDF_MONGO_URI: mongodb://mongo:mongo@192.168.0.2:27017/com-foxit-webpdf
          S8_WEBPDF_CACHE_DIRS: /srdata
          S8_WEBPDF_GSDK_SN:Copy the whole text string after `SN=` in the SN license file you received and paste here
          S8_WEBPDF_GSDK_KEY:Copy the whole text string after `Sign=` in the KEY license file you received and paste here
        volumes:
          - /mnt/share:/srdata

Note: If you use an existing MongoDB, you should modify `S8_WEBPDF_MONGO_URI` value and other fields in the `mongo` section like hostname, ports, username, password and database name, so webViewer Server can connect to your MongoDB.

webpdf-2 node:

Repeated the procedures in webpdf-1 node.

### # Set up the webViewer

After done, open webViewer SDK package, configure the webViewer to access server at the point `http://192.168.0.3:8890/webpdf/` or `http://192.168.0.4:8890/webpdf/`. For details, refer to Set up the webViewer in the section of Deployment in single host.

Note: If you don't want the actual IP to be exposed directly, you can use nginx or other tools to do the reverse proxy, and configure https access.