# Certificate chain in the wrong order: PEM bundle must start with the leaf, then intermediates

**Code:** `chain order` · **Category:** SSL / TLS · **Seen on:** SSL, Nginx, OpenSSL

Servers or clients choke because the certificates in the file are in the wrong order or the root was included first.

## Meaning

The bundle must be leaf → intermediate(s) (optionally no root). Some stacks (nginx, older OpenSSL) won't recover from wrong ordering. Rebuild the file: `cat server.crt intermediate.crt > fullchain.pem` and verify with `openssl crl2pkcs7 -nocrl -certfile fullchain.pem | openssl pkcs7 -print_certs -noout`.

## Common causes

- Intermediate placed before the leaf
- Root first in the bundle
- Wrong order from concatenating downloads
- Missing intermediate
- Duplicate certificates

## Quick fix

1. Order: leaf, intermediates, (root optional)
2. Rebuild with `cat`
3. Verify with `openssl verify -untrusted`

## Detailed fix by platform

### Shell

1. 

```bash
cat server.crt intermediate.crt > fullchain.pem
openssl verify -CAfile root.pem -untrusted intermediate.crt server.crt
```


## How to diagnose

1. **First cert** — Leaf?
2. **Order** — Correct?
3. **Missing** — Intermediate?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/ssl/certificate-chain-wrong-order-leaf-first)
