Long Time to First Byte value for an empty php file

10,072

Solution 1

Seems clear to be a PHP problem, since Apache has no problem serving static files. Have you tried installing APD from PECL?

Using a PHP profiler like APD will show you whether the bottleneck is in PHP and, if so, where it is. For example, is the slowness in the framework that you're using? Or perhaps just a rogue extension?

Paraphrasing from the official manual:

With APD, you just add an instruction at the entry point:

<?php
apd_set_pprof_trace();
?>

APD will dump profiling information to *apd.dumpdir/pprof_pid.ext*.

Then, pprofp will consume your dump files and tell you what methods are chewing up the response time:

bash-2.05b$ pprofp -R /tmp/pprof.22141.0

Trace for /home/dan/testapd.php
Total Elapsed Time = 0.00
Total System Time  = 0.00
Total User Time    = 0.00


Real         User        System             secs/    cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
--------------------------------------------------------------------------------------
100.0 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0000   0.0009            0 main
56.9 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0005   0.0005            0 apd_set_pprof_trace
28.0 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 preg_replace
14.3 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 str_replace

If none of the delay you see shows up in the profile, it would suggest it's a systemwide PHP configuration problem (perhaps a rogue or misconfigured extension). But I would guess it's something in the framework.

Solution 2

You write that at the time when this problem occurs, you have several clients doing AJAX requests every 3 seconds. This makes it likely that all the PHP workers available at your server are blocked by these AJAX requests. Your web server receives your request for /foobar.php and then has to wait until a PHP worker is free to process your request.

So among the possible solutions for your problem are (without you being more specific what the AJAX is needed for, I need to keep this general):

  • have the AJAX requests go to a static file
  • ensure that PHP really closes the connection when the (AJAX) request is finished, for example with a header("Connection: close"); (though this might not be enough, check the comments on the PHP manual about connection handling)
  • reduce the number of AJAX requests made (to a number worked out in cooperation with your provider)

In general you will need your provider's help with solving this. You did not write which method your provider uses for serving PHP, for example with fpm there is a setting called process.max that can limit your web servers ability to process so many PHP files in parallel.

Solution 3

Without cooperation of you provider you have no chance to find out what is wrong.

I would suggest it is something like disk io problems of session.

Another intresting point is that your provider use: http://www.litespeedtech.com/php-litespeed-sapi.html Never heard about that bevore.

If your provider would to what mjk advised you, you should simply change your provider. It seams to be that they dont have theyr ther under control.

Solution 4

If the same empty PHP file loads instantly when moved to another site, it can't be due to any frameworks or included files because non have been loaded at that point.

It can either be a configuration issue on the php or Apache side or it may be caused by your rewrite rules. I would recommend trying the following:

1.) If per-site PHP settings are allowed, ask the hosting company to rename your specific php.ini to some name else, copy over php.ini from another site and restart Apache, see if that helps. I had a similar problem on Windows and it was due to file access problems on php.ini so this may help.

2.) Temporarily rename you .htaccess and access the php file again. If the load time decreases you'll have an errornous rewrite condition or other directive. Could you also post the contents of your .htaccess?

Share:
10,072

Related videos on Youtube

trante
Author by

trante

Updated on June 10, 2022

Comments

  • trante
    trante about 2 years

    I had such question some months ago. Now to isolate the problem I tried a new approach. I put an empty file to my server.

    File name is "foobar.php". Its content is as follows:

    <?php
    echo "hello world";
    

    But when I try to enter to example.com/foobar.php, I get

    DNS 203 mseconds
    Connect 3.33 seconds
    Send 0 miliseconds
    Time to First Byte 17.35 seconds
    Receive 1 miliseconds Total load time 20.88 seconds

    Then I put another file called "foobar.txt". Its content is as follows:

    hello world<br/>
    

    Loading time of foobar.txt is approximeately 0.2 seconds.

    This website is inside a shared hosting so I can't get a root Linux access. I'm trying to find out what makes my site slow.

    • When I get these results I have 60 visitors in my site. And they send AJAX requests when they are active. When they are active they send an AJAX request nearly every 3 seconds.
    • Generally my website has 5-20 requests per second.
    • My hosting provider says that there occurs no CPU overload, it is very low generally.
    • I asked hosting company for Apache limits. I get this values for the whole shared server:

    MaxClients 300
    MaxRequestsPerChild 4000
    ThreadsPerChild 25

    • example.com/mybigpage.php and example.com/foobar.php pages are opened nearly at the same time.
    • If page has txt, jpeg or other extensions they are opened instantly. If extension is php it opens very slow.
    • CakePHP stores session files inside "/httpdocs/app/tmp/sessions" folder. Session files are deleted after two hours of creation. Now there is 3653 files inside that folder. Oldest file is created 2,5 hours ago.
    • In my configuration, PHP handler is Apache module mod_php

    New Edit: I talked with my hosting company. And told them that "foobar.php" is opened nearly in 20 seconds. Although that file has no code at all. They told me that they put "foobar.php" to other websites that we use same server. I also tried "othersite.com/foobar.php". It opened instantly. But "mysite.com/foobar.php" opened nearly in 15 seconds. What would make this behaviour? We use same PHP configuration with other sites, but they open instantly.. Can it be because of my .htaccess rules? Or other thing?

    New Edit2: My provider told me that there exists no "apd.so" file inside the server. So it seems like I can't use APD.

    What should I look for to find the bottleneck?
    What would limit my site?


    Additional data: from the phpinfo, I get this:

    './configure' '--prefix=/usr/local/lsws/lsphp5' '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux-gnu' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib64' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.dd' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-libexpat-dir=/usr/lib64' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-wddx' '--with-kerberos' '--enable-ucd-snmp-hack' '--with-unixODBC=shared,/usr' '--enable-shmop' '--enable-calendar' '--with-libxml-dir=/usr' '--with-mysql' '--with-mysqli' '--with-gd' '--enable-dom' '--disable-dba' '--without-unixODBC' '--enable-xmlreader' '--enable-xmlwriter' '--with-mcrypt' '--enable-mbstring' '--with-litespeed' '--enable-soap' '--with-xsl' '--with-pdo-mysql' '--with-pdo-sqlite' '--enable-sqlite-utf8' '--with-pspell' '--with-sqlite=shared' '--with-xmlrpc' '--with-mhash' '--enable-pdo' '--with-imap' '--with-imap-ssl' '--without-suhosin' '--with-tidy' '--enable-zip' '--enable-inline-optimization' '--enable-gd-native-ttf' '--enable-bcmath'

    • Michel Feldheim
      Michel Feldheim over 11 years
      You might want to look at the xdebug profiler, gives you a nice visual overview of which part takes time
    • AD7six
      AD7six over 11 years
      put print_r(get_included_files()); in your "empty" php file - if you are not infact loading an empty file - it will tell you which files you've loaded first. if you don't have app/webroot/ as your domain's doc root - you have to consider whatever mod_rewrite rules you've added.
  • GreenRover
    GreenRover over 11 years
    You would need root access to the server to do what user MJK (post above me) told you. But a shard hoster will noz grant that to you. But you can ask then to do that. But if they dont linke to debug your problem and only give you the easy answer that the cpu only idle. I would advice you to change the provider.
  • GreenRover
    GreenRover over 11 years
    That yould be way. But because you pay them for service. I would only place you hello_world.php on the server and tell you provider that it is not acceptable that you have a load time of more them 0.01 seconds
  • trante
    trante over 11 years
    I had some gzip and redirect lines. I nearly emptied my htaccess, tried again. But it didn't help.. I pasted my htaccess files to here
  • SaschaM78
    SaschaM78 over 11 years
    Just for testing purposes, what happens if you temporarily remove your htaccess completely?
  • trante
    trante over 11 years
    After I delete .htaccess file I started to get "500 Internal Server Error" so I restored file.
  • SaschaM78
    SaschaM78 over 11 years
    Removing an htaccess file can't be the reason of an Internal Server Error. Assuming your web root would be something like "/usr/htdocs/", putting a foobar.php into the web root and browsing to www.mysite.com/foobar.php should always work without having any htaccess file in the directory. So what happens if you remove the .htaccess, put a phpinfo.php into your web root with <?php phpinfo(); ?> as content and open it in your browser?
  • trante
    trante over 11 years
    I can't put any file to my "/usr/htdocs" because CakePHP dictates me to put my root files to "/usr/htdocs/app/webroot". So from my root I redirect user to "webroot" folder to access root files by .htaccess files. Removing the .htaccess inside my root folder will crash all my site. So nobody would visit my site in that situation, so load will be very low and I wouldn't see the difference in time.
  • SaschaM78
    SaschaM78 over 11 years
    That's why I wrote assuming because I didn't know the path to your web root ;-) To find out what is really causing the problems, may it be php.ini, Apache configuration or .htaccess you temporarily have to rename the .htaccess after you put the phpinfo.php into your webroot directory, then open www.mysite.com/phpinfo.php and if it opens instantly it's going to be htaccess rules.
  • AD7six
    AD7six over 11 years
    "I can't put any file to my "/usr/htdocs" because CakePHP dictates [...]" - that's nonsense, and indicates you have a development-style install what you should be doing is putting your webroot files in /user/htdocs/ directly more information in the book. this also means that example.com/foobar.php is not pointing directly at your php file - you're relying on mod_rewrite to rewrite the request to example.com/app/webroot/foobar.php
  • trante
    trante over 11 years
    Yes I'm relying on mod_rewrte to redirect user request to "webroot" folder. But because of this is a shared hosting, can I change DocumentRoot value of Apache?
  • GreenRover
    GreenRover over 11 years
    1.) You provider need to compile this. It seams you provider has no clue what he does. 2.) Your provider need to debug this problem. what i would try: Run you script on cli, to test time. Run a per or other cgi script to test performancen. When per also has this problem it is a problem with apache / Webserver. If not it could be possible to trace the problem with APD. 3.) I still be shure that it is a problem with disk io. Also i woul try to disable session auto start and apache logging for testing. If one of this improves performence, then has you provider to mutch hostings on a single host
  • SaschaM78
    SaschaM78 over 11 years
    So have you even tried to see if the slowdown is caused by htaccess rules? I can understand that you want to use rewrite rules but what's the use of having fancy URLs if all your pages load terribly slow? People tend to not visit pages if pages take 20 seconds to load. About your question about DocumentRoot changes: some providers offer to change the base directory for a given website using CPanel or whatever management interface they offer. Otherwise try following this guide: CakePHP on Shared Hosts
  • trante
    trante over 11 years
    @SaschaM78 @AD7six I changed my DocumentRoot to /usr/htdocs/app/webroot. Now I don't use .htaccess of /usr/htdocs. I only use htaccess of /usr/htdocs/app/webroot.
  • SaschaM78
    SaschaM78 over 11 years
    Good you where able to change the docroot but did you also test script access times with a temporarily removed htaccess?