If you're experiencing PHP build failing you need to check the following requirements in order to fix the issue

1st case :

you've low on RAM we recommend adding at least 1.5-2 Gb of available ram to compile PHP 7.xx and 8.xx as the compiler needs more memory for this version of PHP

you can check if you've sufficient RAM by this command :

free -h

example output :

root@server:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       328Mi        90Mi        56Mi       1.4Gi       1.5Gi
Swap:          2.0Gi        38Mi       1.9Gi

here you can see Mem which indicates the total RAM and you need to check the `available` memory which should be more than 1.5Gb listed

Also for low memory servers, we always recommend adding at least 2Gb of Swap memory to prevent OOM (Out of memory) incidents.

2nd Case :

You're using an outdated OS that is already ON EOL period

Like Centos 6 and Centos 8 which are already in the EOL period and many repositories are disabled in this OS and you may experience non-availability of packages and dependencies which are required to build the PHP.

We always recommend upgrading you to Following OS :

Centos 7
Centos 8 stream
AlmaLinux 8
Rocky Linux 8
Oracle Linux 7 and 8

3rd Case :

Internet issue/Resolver issue

We've seen some cases where the OS resolver is not correctly set and many packages are not downloading due to the resolver failing.
Ensure you have the correct resolver added in /etc/resolv.conf

you can add this lines at the end of the file in /etc/resolv.conf :

nameserver 1.1.1.1
nameserver 8.8.8.8

* Your server provider might also have some custom resolvers

Now lock the file :

chattr +i /etc/resolv.conf

The next step is to reboot the server :

reboot

Make sure you check the logs as it can be also any other reason