phpdesktop icon indicating copy to clipboard operation
phpdesktop copied to clipboard

Official FastCGI Support for Improved Performance

Open Cherwin077 opened this issue 8 months ago • 10 comments

I am using PHP Desktop version [latest] on Windows to package a Laravel 11 / FilamentPHP application.

While PHP Desktop works, I've observed significant performance bottlenecks, especially with AJAX requests (like those from Livewire used by Filament). Request times are consistently high (e.g., over 1 second), with tools like Laravel Debugbar showing large portions of time spent in the "Booting" and "Application" phases of the framework lifecycle.

Image

Based on the documentation and experimentation, the root cause appears to be PHP Desktop's default reliance on the standard CGI interface (configured via cgi_interpreter in settings.json).

Interestingly, I discovered a significant performance improvement (request times reduced by more than half) when I manually launch the included php/php-cgi.exe

When php-cgi.exe runs persistently in the background (effectively in FastCGI mode), the PHP Desktop application seems to communicate with this existing process instead of spawning new ones. This drastically reduces the framework bootstrapping overhead and leverages PHP Opcache effectively.

Image

before:

Image

after run php/php-cgi.exe:

Image

How can I make phpdeskstop use Fast CGI mode by default? What options do I have?

Cherwin077 avatar Apr 26 '25 00:04 Cherwin077

Mongoose web server that we currently use doesn't support FastCGI protocol. It is also an old version with MIT license, because newer versions of Mongoose use GPL. We would have to replace web server. Maybe lighttpd? (https://en.wikipedia.org/wiki/Lighttpd) However it says that its support on Windows is experimental. Other options are apache and nginx? I think apache is too heavy. We need something lightweight.

cztomczak avatar Apr 26 '25 14:04 cztomczak

Thanks for the quick and detailed explanation regarding the Mongoose limitations and the licensing challenges with newer versions. I understand the difficulty in replacing the web server component, especially finding a lightweight one with solid Windows support.

In the meantime,my plan is to create a simple launcher script (like a .bat file ) located in the root directory of the PHP Desktop application. This script would perform two main actions:

  1. First, it would start the included php/php-cgi.exe in the background, manually instructing it to run in FastCGI mode and listen on a specific port (e.g., start /B php/php-cgi.exe -b 127.0.0.1:9000).
  2. Second, it would immediately launch the main PHP Desktop application executable .

appreciate your insight on whether this external launcher approach might introduce any known side effects within the PHP Desktop environment itself. Thanks again for your time and the great work on PHP Desktop!

Cherwin077 avatar Apr 26 '25 15:04 Cherwin077

Where can I read more about the -b flag for php-cgi.exe?

We have an option to run php built-in web server with the php_server.cpp source file. Can be enabled by modifying C++ sources to include php_server.cpp instead of mongoose web_server.cpp. However this PHP web server is single threaded (at least it was a few years ago when I tested).

cztomczak avatar Apr 26 '25 15:04 cztomczak

I received the script from an AI.

According to the AI, the -b flag instructs PHP-CGI to act as a FastCGI server, listening for connections on a specific address and port.

Although I asked about the origin of this option (-b), the AI concluded that it does not appear to be explicitly documented, basing its explanation on its 'accumulated knowledge' and the tool's observed behavior in the real world.

I conducted a test using the -b flag and a specific port. I was able to verify that it does indeed work and uses that port.

Cherwin077 avatar Apr 26 '25 19:04 Cherwin077

Does it support multi threading? Is it stable running for many hours/days? Let us know how your testing goes.

From what you're saying it's a web server and we could add it as an alternative option for mongoose web server.

cztomczak avatar Apr 26 '25 19:04 cztomczak

Nginx is one alternative. Good article: can nginx run on windows, mac, linus?

What the -b flag does in PHP-CGI

When you run php-cgi without -b, it behaves like traditional CGI: it reads one request from STDIN, writes the response to STDOUT, then exits.
By adding the -b (or --bind) option, you tell PHP-CGI to open a listening socket and stay alive as a FastCGI server, accepting multiple requests over TCP or a UNIX-domain socket.

Typical php-cgi -b usage examples

– Bind to a TCP port (common with standalone web servers):

php-cgi -b 127.0.0.1:9000

– Bind to a UNIX socket (often slightly faster/safer on the same host):

php-cgi -b /tmp/php-fcgi.sock

You can combine -b with other flags like -d foo=bar (to set INI values) or -n (to ignore the default php.ini).

Integrating with Mongoose

  1. Start your PHP FastCGI backend before (or alongside) Mongoose. For example:

    /usr/bin/php-cgi -b 127.0.0.1:9000 -d extension_dir=/usr/lib/php/extensions
    
  2. Configure Mongoose to forward .php requests to that FastCGI listener. In your mongoose.conf (or via command-line flags), point the FastCGI socket/interpreter at the same address:

    fastcgi {
      socket      127.0.0.1:9000
      interpreter /usr/bin/php-cgi
    }
    

With this setup, Mongoose acts purely as the front-end web server, and your persistent PHP-CGI process (started with -b) handles all the PHP scripts via the FastCGI protocol.

oleteacher avatar Apr 26 '25 20:04 oleteacher

Hello,

To add up, there is a build of lighttpd web server called wlighttpd that could be worth a try.

https://itefix.net/wlighttpd

Juan

On Sat, Apr 26, 2025 at 10:31 PM Susan @.***> wrote:

oleteacher left a comment (cztomczak/phpdesktop#378) https://github.com/cztomczak/phpdesktop/issues/378#issuecomment-2832599710

Nginx is one alternative. Good article: can nginx run on windows, mac, linus? https://kinsta.com/knowledgebase/install-nginx/ What the -b flag does in PHP-CGI

When you run php-cgi without -b, it behaves like traditional CGI: it reads one request from STDIN, writes the response to STDOUT, then exits. By adding the -b (or --bind) option, you tell PHP-CGI to open a listening socket and stay alive as a FastCGI server, accepting multiple requests over TCP or a UNIX-domain socket. Typical php-cgi -b usage examples

– Bind to a TCP port (common with standalone web servers):

php-cgi -b 127.0.0.1:9000

– Bind to a UNIX socket (often slightly faster/safer on the same host):

php-cgi -b /tmp/php-fcgi.sock

You can combine -b with other flags like -d foo=bar (to set INI values) or -n (to ignore the default php.ini). Integrating with Mongoose

Start your PHP FastCGI backend before (or alongside) Mongoose. For example:

/usr/bin/php-cgi -b 127.0.0.1:9000 -d extension_dir=/usr/lib/php/extensions

Configure Mongoose to forward .php requests to that FastCGI listener. In your mongoose.conf (or via command-line flags), point the FastCGI socket/interpreter at the same address:

fastcgi { socket 127.0.0.1:9000 interpreter /usr/bin/php-cgi }

With this setup, Mongoose acts purely as the front-end web server, and your persistent PHP-CGI process (started with -b) handles all the PHP scripts via the FastCGI protocol.

— Reply to this email directly, view it on GitHub https://github.com/cztomczak/phpdesktop/issues/378#issuecomment-2832599710, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAI7NGLMNX2SSET4Z3BUOBL23PULZAVCNFSM6AAAAAB34Y4RRWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDQMZSGU4TSNZRGA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

juangacovas avatar Apr 27 '25 04:04 juangacovas

lighttpd developer here. The only reason that lighttpd is still marked experimental on Windows (upstream) is that I don't provide a pre-built installer of lighttpd for Windows.

lighttpd builds natively on Windows, so I recommend using a native build of lighttpd rather than the third-party build linked above, which is based on the Cygwin build of lighttpd. If you're going to use the Cygwin build of lighttpd, I recommend using the latest lighttpd from the Cygwin package manager, rather than an outdated lighttpd from a third-party.

lighttpd native build on Windows is used in production use of https://github.com/birdofpreyru/react-native-static-server/ which runs on Linux, Android, MacOS, iOS, and Windows. If you're packaging a PHP interpreter with your app, you may choose to package your own native build of lighttpd, too.

If you're looking for hints how to build lighttpd on Windows, lighttpd regularly builds on Windows as part of https://github.com/lighttpd/lighttpd1.4/blob/master/.github/workflows/ci.yml

gstrauss avatar May 09 '25 16:05 gstrauss

Where can I read more about the -b flag for php-cgi.exe?

Couldn't find much, hopefully this helps.

PS C:\phpdesktop-chrome-130.1-php-8.3\php> .\php-cgi.exe --help
Usage: php-cgi [-q] [-h] [-s] [-v] [-i] [-f <file>]
       php-cgi <file> [args...]
  -a               Run interactively
  -b <address:port>|<port> Bind Path for external FASTCGI Server mode
  -C               Do not chdir to the script's directory
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse <file>.  Implies `-q'
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -q               Quiet-mode.  Suppress HTTP Header output.
  -s               Display colour syntax highlighted source.
  -v               Version number
  -w               Display source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.
  -T <count>       Measure execution time of script repeated <count> times.

https://www.manpagez.com/man/1/php/

https://github.com/php/php-src/blob/master/sapi/cgi/cgi_main.c

toazd avatar May 31 '25 16:05 toazd

@toazd All I could find is right up above... https://github.com/cztomczak/phpdesktop/issues/378#issuecomment-2832599710

oleteacher avatar May 31 '25 23:05 oleteacher