# Deploying Care on DigitalOcean

In this guide, we'll walk you through deploying **Care** on **DigitalOcean**. We'll keep things simple so you can follow along to get your instance up and running. Expect the process to take about **an hour**, depending on your familiarity with the tools.

For an overview of all supported cloud providers and their infrastructure requirements, see the [Cloud providers overview](https://docs.ohc.network/deployment/providers.md).

***

## Prerequisites

* A DigitalOcean account ([Sign up here](https://www.digitalocean.com)).
* A fork of the Care backend and frontend repositories.
* A registered domain name (optional but recommended).
* Basic familiarity with DigitalOcean's App Platform and Spaces.

***

## What You'll Be Doing

* Set up a **PostgreSQL database** for secure data storage.
* Configure **DigitalOcean Spaces** for handling files and media.
* Deploy the **Care backend** with Redis and Celery for task management.
* Set up the **Care frontend** for user access.
* Secure the setup with custom domains, API keys, and SSL.
* Setup a CI/CD Pipeline for Continuous Deployment.

***

## Step 1: Create the Database

### 1.1 Navigate to Databases

* Log in to DigitalOcean and go to the **Databases** section.

### 1.2 Create a PostgreSQL Database

* Click **Create Database**.

* Choose the following:

  * **Region**: Select the nearest data center.
  * **Engine**: PostgreSQL (Version 16).
  * **Resources**: Select a plan based on your requirements.

  ![Navigate to Databases](/assets/images/do-db-1-2d39adc2df9e52395aed72c75b0e207c.png)

  * **Name**: Give your database a unique name.
  * **Tags**: Add tags for organization.

  ![Navigate to Databases](/assets/images/do-db-2-73f496b5851db0171f907325c32c91d6.png)

  ![Create Database](/assets/images/do-db-3-aaf313775a0476c486f230f0d7be72d2.png)

### 1.3 Save Connection Details

* Once the database is created, note the connection URL for later use.

  ![Connection String](/assets/images/do-db-4-1901c5b5cc69b2f31aaf5a595840b8d9.png)

***

## Step 2: Set Up Object Storage (Spaces)

### 2.1 Navigate to Spaces

* Go to the **Spaces** section in DigitalOcean.

### 2.2 Create a New Space

* Click **Create a Space**.

  ![Create Space](/assets/images/do-spaces-1-49547fccf34c9a6f99bb07d943bdfaaf.png)

* Choose:

  * **Region**: Select the same region as your database.
  * **Enable CDN**: For faster file delivery.
  * **Bucket Name**: Enter a unique name.
  * **Project**: Select the default project.
  * **Create**: Click to create the space.

  ![Create Space](/assets/images/do-spaces-3-a3d9db09d967e1a5d75dcdbb535f3535.png)

### 2.3 Generate API Keys

* Go to the **API** tab and click **Spaces Keys**.

* Create a new key pair and save the **Key** and **Secret**.

  ![Generate Keys](/assets/images/do-spaces-2-523ea44dbcba44ebfe5782d07fb49b33.png)

***

## Step 3: Deploy the Care Backend (BE)

### Step 3.1: Deploy Redis

#### 3.1.1 Set Up Redis

* Go to the App Platform and click **Create App**.

  \[Image: Select App Platform]

* Choose **Docker Hub** as the source and use:

  * **Repository**: `redis/redis-stack-server`.
  * **Tag**: `6.2.6-v10`.
  * **Port**: Expose `6379` as an internal port.

* Name the component `redis`.

  ![Select App Platform](/assets/images/redis-1-2d7e6d03aa2ac917361874e2afbbe883.png)

  ![Edit these fields](/assets/images/redis-2-de9f95063068144c6d20911206fad4e6.png)

#### 3.1.2 Name and Deploy

* Name the App `care-backend` and deploy it.

  ![Name and Deploy](/assets/images/redis-3-f868d608da7db27165cd55b6220aefde.png)

***

### Step 3.2: Deploy the Care Backend

#### 3.2.1 Fork and Clone the Repository

* Fork the [Care Backend Repository](https://github.com/ohcnetwork/care).
* Use the `production` branch for deployment.

#### 3.2.2 Set Up the App

* Click **Create App** and choose **From Source Code**.

* Select your forked repo and branch.

* Set the build and run commands:

  ![Create Resource from Source Code](/assets/images/care-django-1-1d8298180b6ecaee62aa95092e34485f.png)

  ![Select GitHub Repository](/assets/images/care-django-2-ea2166c77e0f939f09bd22612281dd5f.png)

  * **Build Command:**

    ```
    python install_plugins.py && python manage.py collectstatic --noinput && python manage.py compilemessages
    ```

  * **Run Command:**

    ```
    gunicorn config.wsgi:application --workers 2 --bind :9000
    ```

    ![Select GitHub Repository](/assets/images/care-django-3-e9e808f25da41a2689258f51c291e011.png)

#### 3.2.3 Environment Variables

Add the following as global environment variables:

```
DJANGO_SETTINGS_MODULE=config.settings.production

DATABASE_URL=<db-url> # Use the connection URL from Step 1.

REDIS_URL=redis://redis:6379

CORS_ALLOWED_ORIGINS=["https://care.example.com","http://localhost:4000","http://127.0.0.1:4000"]

CELERY_BROKER_URL=redis://redis:6379

REDIS_URL=redis://redis:6379

BUCKET_PROVIDER=DIGITAL_OCEAN

BUCKET_REGION=nyc3 # Use the region of your Spaces.

BUCKET_KEY= # Use the Spaces key.

BUCKET_SECRET= # Use the Spaces secret.

BUCKET_HAS_FINE_ACL=true

FILE_UPLOAD_BUCKET=example-space-ohc # Use the name of your Space.

FILE_UPLOAD_BUCKET_ENDPOINT=https://example-space-ohc.nyc3.digitaloceanspaces.com

FACILITY_S3_BUCKET=example-space-ohc # Use the name of your Space.

FACILITY_S3_BUCKET_ENDPOINT=https://example-space-ohc.nyc3.digitaloceanspaces.com

JWKS_BASE64= # Add your JWKS key. (Refer to the next section to create one.)

DISABLE_COLLECTSTATIC=1
```

##### Generate JWKS Key

The `JWKS_BASE64` value is generated here. Use the generator below to create a key, then paste the output into the `JWKS_BASE64` environment variable above.

Generating JWKS...

#### 3.2.4 Expose Ports

* Expose port `9000` for public HTTP access.

***

### Step 3.3: Add a Domain for the Backend

#### 3.3.1 Configure Domain

* Go to the app settings and add your domain (e.g., `care-api.example.com`).

* Update your DNS provider with the CNAME record.

  ![Add Domain](/assets/images/domain-995e5dd40000ef6e950185465ec5e17f.png)

***

### Step 3.4: Set Up Celery Worker and Beat

#### 3.4.1 Celery Worker

* Create a new component in the app.

* Use the following **Run Command**:

  ```
  celery --app=config.celery_app worker --max-tasks-per-child=2 --concurrency=2 --loglevel=DEBUG --logfile stdout
  ```

  ![Celery Worker](/assets/images/worker-1-52b0ce5105c6ac885c9b95a7ccc0c2ea.png)

  ![Celery Worker](/assets/images/worker-2-3419f76370013c972ef985b3fd975c54.png)

#### 3.4.2 Celery Beat

* Create another component with the following **Run Command**:

  ```
  python manage.py migrate
  ```

  ![Celery Job](/assets/images/job-1-cb4bbe7619cbbb8766f0ce9ec5b1f881.png)

  ![Celery Job](/assets/images/job-2-cabb9d8b3e24b36fac2d6f18fed31f94.png)

***

## Step 4: Deploy the Care Frontend (FE)

### 4.1 Fork the Frontend Repository

* Fork the [Care Frontend Repository](https://github.com/ohcnetwork/care_fe).
* Use the `production` branch.

### 4.2 Set Up the App

* Click **Create App** and configure:

  * **Type**: Static Site.

  * **Build Command:** `npm run build`.

  * **Environment Variable:**

    ```
    REACT_CARE_API_URL=https://care-api.example.com
    ```

  * **Output Directory:** Auto.

### 4.3 Add Domain

* Go to the app settings and add the frontend domain (e.g., `care.example.com`).

  ![Frontend Setup](/assets/images/care-fe-1-0ddab3bcdb1175a1ae62d8736602454f.png)

  ![Celery Job](/assets/images/care-fe-2-210c0930aaa704beb698b1a28cd8eccd.png)

  ![Celery Job](/assets/images/care-fe-3-fad994b16b62d5e0e93ab728f5fa3479.png)

***

## Step 5: Secure Database Access

### 5.1 Restrict Access

* Navigate to the Database settings.

* Add the Care backend app as a trusted source.

  ![Secure Database](/assets/images/secure-db-d4c76159cc71cc04bab2e5c3c6da18b4.png)

***

## Step 6: Configure CORS for Spaces

### 6.1 Go to Bucket Settings

* Open your bucket in Spaces.

* Add the following CORS configuration:

  ```
  [

    {

      "AllowedHeaders": ["*"],

      "AllowedMethods": ["GET", "POST", "PUT", "DELETE"],

      "AllowedOrigins": ["https://example.com"],

      "ExposeHeaders": []

    }

  ]
  ```

  ![CORS](/assets/images/cors-d967abbbf88c37053b8fb70e08976c25.png)

***

## Final Steps

* **Verify Deployment:**

  * Test the backend and frontend by accessing the respective domains.
  * Ensure all components (Redis, Celery Worker, Beat) are running.

* **Optimize Security:**

  * Use SSL certificates (e.g., Let's Encrypt) to enable HTTPS.

  * Configure firewall rules to restrict access.

    ![Care Infra](/assets/images/care-infra-do-4cba884bcc5a19c1f9c2372d929969eb.png)

***

## Conclusion

You've successfully deployed the Care application on DigitalOcean! With the database, object storage, backend, and frontend configured, your application is ready for use. 🚀

For troubleshooting, feel free to join our Community on Slack at [slack.ohc.network](https://slack.ohc.network). You may also refer to the official [DigitalOcean Documentation](https://www.digitalocean.com/docs) for any questions about DigitalOcean.
