Symptoms
When testing a site's TTFB, it is high only when accessing the site via PHP content. When accessing the domain via a simple text file, the TTFB is low.
Description
In almost all cases of TTFB being slow, it is due to the Web Application (your site code) and the time that it takes to process. The only way to speed up TTFB is to improve the website code itself or use additional caching mechanisms like Redis, Memcache, or otherwise (if the website code supports it).
Here are some troubleshooting steps and explanations:
1. Run a manual test direct to the server.
2. Make sure that Cloudflare is not in use to use a direct request.
3. Use a simple text file.
Replace "APACHE_PORT" with Apache's actual port number. The default port is port 80 unless there are already caching mechanisms or other web servers such as LightSpeed or NGNIX.
Replace "SERVER_IP" with the server's IP where the site is hosted.
Please note, these tests were conducted from a US-based workstation, and the test server was located in France.
4. Test with the text file with the below command through the server's webserver:
cPAgent@cPanel.net [~] # time curl -o /dev/null -silent --resolve domain.tld:tld:APACHE_PORT:SERVER_IP https://domain.tld:APACHE_PORT/cptestfile.txt
real 0m0.523s
user 0m0.070s
sys 0m0.071s
In the above test, the result was a very low TTFB.
5. Test again using the site's PHP content.
cPAgent@cPanel.net [~] # time curl -o /dev/null -silent --resolve domain.tld:APACHE_PORT:SERVER_IP https://domain.tld:APACHE_PORT
real 0m2.251s
user 0m0.068s
sys 0m0.076s
In the example above, the website with the PHP content takes nearly two full seconds longer.
Even when accessed the site through CloudFlare, there are similar processing delays when the site has to process your website's code.
[ Test file ]
cPAgent@cPanel.net [~] # time curl -o /dev/null -silent https://domain.tld/cptestfile.txt
real 0m0.375s
user 0m0.062s
sys 0m0.055s
[ PHP content ]
cPAgent@cPanel.net [~] # time curl -o /dev/null -silent https://domain.tld/
real 0m1.622s
user 0m0.067s
sys 0m0.087s
The above output reinforces the server itself and CloudFlare, is quick to respond. It is only when the website though the PHP code that the delays occur.
In both these examples, TTFB is seen once PHP processing is complete, so the TTFB is reported poorly for the site despite that TTFB for the server itself is very quick.
Workaround
The only way to "speed up" PHP processing would be to improve the code itself or provide it with a faster processor. Even then, best practices for web applications are to use heavily caching mechanisms. Whether or not you can take advantage of those caching systems is dependent on your web application and if its designers have coded support for it. Only the developer of the site will know more information.