2

I have a PHP script like this running on PHP-fpm 7.3:

<?php
$file = './French-Stories-Martine.zip';
if (headers_sent()) {
    echo 'HTTP header already sent';
} else {
    if (!is_file($file)) {
        header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
        echo 'File not found';
    } else if (!is_readable($file)) {
        header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
        echo 'File not readable';
    } else {
        header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: Binary");
        header("Content-Length: ".filesize($file));
        header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
        readfile($file);
        exit;
    }
}
?>

I've tried downloading this zip file statically (meaning without a PHP script - just using a plain old Apache + OpenLiteSpeed webserver), and the download speed is great. Nothing wrong with that.

The problem is when I'm trying to download the said zip file, with this PHP script. The download speed is 1/10th.

Now, I've concluded that the issue is with the PHP and not with the WebServer or the Server itself. I can't figure out what the problem could be with my PHP?

I'm using the following configuration for PHP-fpm:

pm.max_children = 80
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30

This is the PHP log file when I'm trying to download from my script:

[08-Nov-2021 00:40:52] WARNING: [pool www] child 3267, script '/www/wwwroot/example.com/my-php-script-dl.php' (request: "GET /my-php-script-dl.php") executing too slow (31.192873 sec), logging
[08-Nov-2021 00:40:52] NOTICE: child 3267 stopped for tracing
[08-Nov-2021 00:40:52] NOTICE: about to trace 3267
[08-Nov-2021 00:40:52] NOTICE: finished trace of 3267
[08-Nov-2021 00:42:30] NOTICE: Reloading in progress ...
[08-Nov-2021 00:42:30] NOTICE: reloading: execvp("/www/server/php/73/sbin/php-fpm", {"/www/server/php/73/sbin/php-fpm", "--daemonize", "--fpm-config", "/www/server/php/73/etc/php-fpm.conf", "--pid", "/www/server/php/73/var/run/php-fpm.pid"})
[08-Nov-2021 00:42:30] NOTICE: using inherited socket fd=8, "/tmp/php-cgi-73.sock"
[08-Nov-2021 00:42:30] NOTICE: using inherited socket fd=8, "/tmp/php-cgi-73.sock"
[08-Nov-2021 00:42:30] NOTICE: fpm is running, pid 5317
[08-Nov-2021 00:42:30] NOTICE: ready to handle connections
[08-Nov-2021 00:42:44] NOTICE: Reloading in progress ...
[08-Nov-2021 00:42:44] NOTICE: reloading: execvp("/www/server/php/73/sbin/php-fpm", {"/www/server/php/73/sbin/php-fpm", "--daemonize", "--fpm-config", "/www/server/php/73/etc/php-fpm.conf", "--pid", "/www/server/php/73/var/run/php-fpm.pid"})
[08-Nov-2021 00:42:44] NOTICE: using inherited socket fd=8, "/tmp/php-cgi-73.sock"
[08-Nov-2021 00:42:44] NOTICE: using inherited socket fd=8, "/tmp/php-cgi-73.sock"
[08-Nov-2021 00:42:44] ERROR: Another FPM instance seems to already listen on /tmp/php-cgi-73.sock
[08-Nov-2021 00:42:44] NOTICE: fpm is running, pid 5539
[08-Nov-2021 00:42:44] ERROR: FPM initialization failed
[08-Nov-2021 00:42:44] NOTICE: ready to handle connections
[08-Nov-2021 00:45:18] NOTICE: Reloading in progress ...
[08-Nov-2021 00:45:18] NOTICE: reloading: execvp("/www/server/php/73/sbin/php-fpm", {"/www/server/php/73/sbin/php-fpm", "--daemonize", "--fpm-config", "/www/server/php/73/etc/php-fpm.conf", "--pid", "/www/server/php/73/var/run/php-fpm.pid"})
[08-Nov-2021 00:45:18] ERROR: Another FPM instance seems to already listen on /tmp/php-cgi-73.sock
[08-Nov-2021 00:45:18] ERROR: FPM initialization failed
[08-Nov-2021 00:45:18] NOTICE: using inherited socket fd=8, "/tmp/php-cgi-73.sock"
[08-Nov-2021 00:45:18] NOTICE: using inherited socket fd=8, "/tmp/php-cgi-73.sock"
[08-Nov-2021 00:45:18] NOTICE: fpm is running, pid 6200
[08-Nov-2021 00:45:18] NOTICE: ready to handle connections
[08-Nov-2021 00:45:44] NOTICE: Reloading in progress ...
[08-Nov-2021 00:45:44] NOTICE: reloading: execvp("/www/server/php/73/sbin/php-fpm", {"/www/server/php/73/sbin/php-fpm", "--daemonize", "--fpm-config", "/www/server/php/73/etc/php-fpm.conf", "--pid", "/www/server/php/73/var/run/php-fpm.pid"})
[08-Nov-2021 00:45:44] NOTICE: using inherited socket fd=8, "/tmp/php-cgi-73.sock"
[08-Nov-2021 00:45:44] NOTICE: using inherited socket fd=8, "/tmp/php-cgi-73.sock"
[08-Nov-2021 00:45:44] ERROR: Another FPM instance seems to already listen on /tmp/php-cgi-73.sock
[08-Nov-2021 00:45:44] ERROR: FPM initialization failed
[08-Nov-2021 00:45:44] NOTICE: fpm is running, pid 6251
[08-Nov-2021 00:45:44] NOTICE: ready to handle connections
[08-Nov-2021 00:47:46] NOTICE: Finishing ...
[08-Nov-2021 00:47:46] NOTICE: exiting, bye-bye!
[08-Nov-2021 00:47:47] NOTICE: fpm is running, pid 6518
[08-Nov-2021 00:47:47] NOTICE: ready to handle connections
Patira
  • 65
  • 2
  • 9
  • The settings you shared are not relevant because the entirety of the transfer happens on a single request which occupies just a single thread. The log also indicates that you have the slowlog enabled for the thread which will freeze thread execution to dump the stack trace once the request has taken too long to respond which will add a bit of time as well. Finally check if you're gzipping your response with PHP – apokryfos Nov 07 '21 at 21:54
  • I know the PHP-fpm configs aren't relevant to this matter, but still, I wanted to share them, so there's no confusion in that regard. The thing is that according to the log, the script is taking too long to execute. Why is that? I've disabled gzip on apache and on PHP (via htaccess). Still have the same issue! – Patira Nov 07 '21 at 22:06
  • This is the line I'm using for disabling gzip: `RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]` – Patira Nov 07 '21 at 22:08
  • 1
    My question was to check if gzip is enabled because you might want to enable it if it's not – apokryfos Nov 07 '21 at 22:19
  • Well, by default it was enabled, so I disabled it. When I didn't see the difference, I changed it back to being enabled. Again, there's no difference. – Patira Nov 07 '21 at 22:32
  • Look at https://www.php.net/manual/en/function.readfile.php, search for a comment by TimB regarding output buffering. Wild guess. – Nic3500 Nov 08 '21 at 03:07
  • You can change the practice to download the file, first copy it from your file directory to public directory where we can access it in the url and then just hit the url using the php script on new tab. it will download more faster than your current code – PHP Geek Nov 08 '21 at 04:26
  • @Nic3500 I used `ob_end_flush()` immediately before the call to `Readfile()` and it didn't change anything. I don't think the problem is with the code. As I said before, I think the problem is with the PHP, but I can't figure out exactly what it is. – Patira Nov 08 '21 at 06:02
  • @PHPGeek The thing is that it's a little bit more complicated than that. I need to use PHP in order to download the file dynamically. – Patira Nov 08 '21 at 06:08

0 Answers0