# Deploy certificates

> Use Certificate Center certificates for CDN domains, bound bucket domains, and your own servers, and keep them updated automatically with the API.

After a certificate is deployed in the Certificate Center (green dot), you still need to select it once where it's used.

| Where it's used                   | How to use it         | After an auto certificate renews     |
| --------------------------------- | --------------------- | ------------------------------------ |
| CDN domains                       | Select in the console | Updated automatically                |
| Bound bucket domains              | Select in the console | Updated automatically                |
| Your own servers, other platforms | Fetch through the API | Fetched on a schedule by your script |

In **Associated projects (关联项目)** in the list, you can see which CDN domains and bucket domains use a certificate.

## Use for a CDN domain

1. Go to **CDN › Static acceleration management (静态加速管理)** ([open](https://console.bitiful.com/cdn/list)) and click **Manage (管理)** on the domain.
2. Switch to **HTTPS configuration (HTTPS 配置)** and click **Edit (编辑)** on the **HTTPS configuration** card.
3. Select **On (开启)**, choose the certificate in the **Certificate (证书)** drop-down, and click **Save (保存)**.
4. Wait for the status to go from **Deploying (部署中)** back to **Running (运行中)**, which usually takes 30 seconds.

If the certificate doesn't cover the domain or has expired, saving fails and the reason is shown. For the other options, see [HTTPS settings](https://docs.bitiful.com/en/cdn/handbook/https.md).

## Use for a bound bucket domain

Creating new bucket domains is paused. Connect new domains to [CDN](https://docs.bitiful.com/en/cdn/intro.md). Bucket domains that were bound earlier can still have a certificate set:

1. Go to **Object storage › Bucket list › a specific bucket › Website settings (对象存储 › 桶列表 › 具体的桶 › 网站设置)** and click **Manage domain (管理域名)** under **Bound domains (绑定域名)**.
2. On the **HTTPS configuration** card, click **Edit**, select **On**, and choose the certificate in the **Certificate** drop-down.
3. Click **Save**.

Notes:

- Once on, HTTP/2, OCSP, and TLS 1.0 to 1.3 are enabled by default, with no separate switches here
- Confirm yourself that the certificate covers this domain
- After you [replace](https://docs.bitiful.com/en/certs-center/custom-cert.md) your own certificate, select it again here and save once
- A certificate that is deploying or in an abnormal state can't be selected. You see "该证书正在部署中，请稍后" (this certificate is being deployed, try again later) or "证书状态异常" (abnormal certificate status)

## Use for your own servers

Fetch the certificate and private key through the [Certificate API](https://docs.bitiful.com/en/developer/api/certs.md), write them to the server, and reload the service. With a scheduled job, after an auto certificate renews, the certificate on your server is updated automatically too.

### Prepare an API token

Create one in [Console › API Token](https://console.bitiful.com/apiToken). See [API tokens](https://docs.bitiful.com/en/console/api-token.md). A token can read the private keys of all certificates in your account, so keep it only on the server and restrict the file permissions.

### Save the script

You need `curl`, `jq`, and `openssl` installed. Change the certificate name (copy it from the certificate list), the save path, and the reload command.

```bash title="/usr/local/bin/sync-cert.sh"
#!/usr/bin/env bash
set -euo pipefail
umask 077

CERT_NAME="my-cert"                      # The certificate name in the Certificate Center
TOKEN="$(cat /etc/bitiful/api-token)"    # Readable only by root, permissions 600
DIR="/etc/nginx/certs/cdn.example.com"

mkdir -p "$DIR"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

# URL-encode the certificate name if it has special characters
NAME="$(jq -rn --arg n "$CERT_NAME" '$n|@uri')"

# Pass the request header through stdin so the token doesn't appear in the process list
printf 'Authorization: %s\n' "$TOKEN" |
  curl -fsS --retry 3 --max-time 30 -H @- \
    "https://api.bitiful.com/cert/data/${NAME}/content" -o "$TMP/cert.json"

jq -er .crt "$TMP/cert.json" > "$TMP/fullchain.pem"
jq -er .key "$TMP/cert.json" > "$TMP/private.key"

# Don't replace if the certificate doesn't match the private key, or has expired
openssl x509 -in "$TMP/fullchain.pem" -noout -checkend 0 > /dev/null
[ "$(openssl x509 -in "$TMP/fullchain.pem" -noout -pubkey)" = \
  "$(openssl pkey -in "$TMP/private.key" -pubout)" ]

# Exit if nothing changed
if cmp -s "$TMP/fullchain.pem" "$DIR/fullchain.pem" && cmp -s "$TMP/private.key" "$DIR/private.key"; then
  exit 0
fi

install -m 644 "$TMP/fullchain.pem" "$DIR/fullchain.pem"
install -m 600 "$TMP/private.key" "$DIR/private.key"

nginx -t && systemctl reload nginx
```

### Configure Nginx

```text
ssl_certificate     /etc/nginx/certs/cdn.example.com/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/cdn.example.com/private.key;
```

### Add a scheduled job

Running it once a day is enough. Auto certificates renew when less than 15 days of validity remain.

```bash
chmod 700 /usr/local/bin/sync-cert.sh
echo '30 4 * * * root /usr/local/bin/sync-cert.sh' > /etc/cron.d/sync-cert
```

## Verify

```bash
echo | openssl s_client -connect cdn.example.com:443 -servername cdn.example.com 2>/dev/null \
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
```

Check that the validity period is the new certificate's, and that `subjectAltName` includes the domain being accessed.

## FAQ

<details>
<summary>Can one certificate be used for multiple domains?</summary>

Yes. As long as the certificate covers those domains, you can use it on multiple CDN domains, bucket domains, and servers.

</details>

<details>
<summary>After an auto certificate renewed, the CDN domain still shows the old validity period</summary>

The new certificate is pushed automatically, and it takes a little time. Use the command above to confirm the certificate actually in use. If the CDN domain uses a different certificate, the renewal doesn't affect it.

</details>

<details>
<summary>The API returns 403</summary>

A 403 is returned when the certificate name doesn't exist, the certificate is still being requested, or the request failed. Confirm the name and status in the list.

</details>
