# Source: https://docs.upsun.com/languages/php/fpm.md

# PHP-FPM sizing


p:last-child]:mb-0 [&>h3]:mt-0 rounded-lg" >

### Note
You can now use composable image to install runtimes and tools in your application container. To find out more, see the [Composable image](https://docs.upsun.com/create-apps/app-reference/composable-image.md) topic.

PHP-FPM helps improve your app's performance
by maintaining pools of workers that can process PHP requests.
This is particularly useful when your app needs to handle a high number of simultaneous requests.

By default, Upsun automatically sets a maximum number of PHP-FPM workers for your app.
This number is calculated based on three parameters:

- The container memory: the amount of memory you can allot for PHP processing
  depending on [your defined application resources](https://docs.upsun.com/manage-resources.md).
- The request memory: the amount of memory an average PHP request is expected to require.
- The reserved memory: the amount of memory you need to reserve for tasks that aren't related to requests.
The number is calculated as follows: ![The sum of container memory minus reserved memory divided by request memory](https://docs.upsun.com/images/php/PHP-FPM-Workers-Calculation.png "0.2")

Note that when the resulting number is a decimal,
it's rounded up to set the maximum number of workers.
Also, the minimum number of PHP-FPM workers is 2.

To adjust the maximum number of PHP-FPM workers depending on your app's needs, follow the instructions on this page.

## Before you begin

You need:

- An up-and-running web app in PHP, complete with [PHP-FPM](https://www.php.net/manual/en/install.fpm.php)
- The [Upsun CLI](https://docs.upsun.com../../administration/cli.md)

Note that the memory settings mentioned on this page are different from the [`memory_limit` PHP setting](https://docs.upsun.com/languages/php.md).
The `memory_limit` setting is the maximum amount of memory a single PHP process can use
before it's automatically terminated.

## 1. Estimate the optimal request memory for your app

To determine what the optimal request memory is for your app,
you can refer to your PHP access logs.
Run a command similar to:

```bash
upsun log --lines 5000 php.access | awk '{print $6}' | sort -n | uniq -c
```

This command takes into account the last 5,000 requests that reached PHP-FPM.
You can adjust this number depending on the amount of traffic on your site.

In the output, you can see how many requests used how much memory, in KB.
For example:

```bash
    2654 2048
    431  4096
    584  8192
    889  10240
    374  12288
     68  46384
```

The output shows that:
- The majority of requests peaked at 2,048 KB of memory.
- Most other requests used up to around 10 MB of memory.
- A few requests used up to around 12 MB of memory.
- Only 68 requests peaked at around 46 MB of memory.

In this situation, you might want to be cautious
and [set your request memory](#2-adjust-the-maximum-number-of-php-fpm-workers) to 12 MB.
Setting a lower request memory presents a risk of allowing more concurrent requests.
This can result in memory swapping and latencies.

## 2. Adjust the maximum number of PHP-FPM workers

By default, the request memory is set to 45 MB
and the reserved memory is set to 70 MB.
These values allow most programs to run,
but you can amend them to fit your needs.

To do so, adjust your [app configuration](https://docs.upsun.com../../create-apps.md).
Under `runtime` in the [`sizing_hints` setting](https://docs.upsun.com/create-apps/app-reference/single-runtime-image.md#sizing-hints),
set the values for `reserved_memory` and `request_memory`.

For example,
if you estimate your [optimal request memory](#1-estimate-the-optimal-request-memory-for-your-app) to be 110 MB
and your reserved memory to be 80 MB,
you can use:

```yaml  {location=".upsun/config.yaml"}
applications:
  myapp:
    type: 'php:8.5'
    runtime:
      sizing_hints:
        request_memory: 110
        reserved_memory: 80
```
Note that the minimum value for the `request_memory` key is 10 MB
and the minimum value for the `reserved_memory` key is 70 MB.
If you set lower values,
they're automatically overridden with those minimums.

To check the maximum number of PHP-FPM workers available to your app,
run the following command, where `children` refers to PHP-FPM workers:

```bash
upsun ssh "grep -e '^pm.max_children' /etc/php/*/fpm/php-fpm.conf"
```

You get output similar to the following:

```bash
pm.max_children = 5
```