Estimated read time: 2 minutes   | 0 comments

Multiple Concurrent PHP Processes with Nginx on Windows

Multiple Concurrent PHP Processes with Nginx on Windows

This article will describe an easy way to serve multiple PHP processes on a Windows system using a Nginx web server. Most default setups, when developing with PHP on a Windows system, will only serve a single PHP process, which can be annoying if you ever have a script go into an endless loop, or otherwise hang. On Linux/UNIX production servers, you most certainly will serve many concurrent PHP processes, so it usually helps to have your development environment setup in a similar way.

Linux/UNIX can use PHP FastCGI Process Manager (FPM), which is an alternative PHP FastCGI implementation with some additional features. Unfortunately, PHP-FPM is not available on Windows, but we can still use FastCGI to get similar results.

Requirements

  1. Windows operating system
  2. Nginx HTTP web server https://nginx.org
  3. PHP https://www.php.net
  4. RunHiddenConsole.exe (optional to hide the console window)

How to do it

There are a couple ways to do it, but I find this method to be the best because it does not require any additional software. All you need to do is edit the nginx config and set some environment variables before starting the PHP process.

Nginx can interface with PHP on Windows via a FastCGI daemon, which ships with PHP: php-cgi.exe.

Setup the Nginx Config File

We are going to use the FastCGI setup in our nginx config. This will pass our PHP scripts (URLs with files ending in .php), to our FastCGI PHP process listening on 127.0.0.1 port 9000.

# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
    include     fastcgi.conf;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_read_timeout 60;
    allow       127.0.0.1;
    allow       ::1;
    deny        all;
}

Starting the FastCGI PHP Daemon Process

I find it easiest to make a Windows .bat script to set the necessary environment variables and start the PHP process. I like using a tiny utility, RunHiddenConsole.exe to hide the console window but this is optional.

Make sure php-cgi.exe is on you PATH, or provide the full path to it in the script below.

start-php.bat

@echo off
echo Setting environment variables for 10 concurrent PHP fast-cgi processes!
set PHP_FCGI_MAX_REQUESTS=0
set PHP_FCGI_CHILDREN=10
echo Starting PHP FastCGI...
set prg=php-cgi.exe
QPROCESS "%prg%">NUL
if "%errorlevel%" GTR "0" (
  echo "Process (%prg%) starting..."

  C:/path/to/RunHiddenConsole.exe %prg% -b 127.0.0.1:9000
) else (
  echo "Process (%prg%) already started..."
)

The most important part here is the PHP_FCGI_CHILDREN environment variable. This is what gives us multiple PHP processes! The script above starts 10 PHP processes but feel free to change it to whatever you like.

This 10 processes will always be running; they aren't dynamically created and destroyed as needed depending on load.

stop-php.bat

Here is a Windows batch script to easily stop PHP.

@echo off
echo Stopping PHP FastCGI...
set prg=php-cgi.exe
QPROCESS "%prg%">NUL
if "%errorlevel%" EQU "0" (
  echo "Process (%prg%) stopping..."

  taskkill /f /IM %prg%
) else (
    echo "Process (%prg%) is not currently running."
)

Run

Now we just have to start our nginx server, and run our PHP FastCGI start up script.

nginx.exe
./start-php.bat

And that's it! Now we have 10 PHP processes ready to serve requests concurrently! 🎉

To stop PHP

./stop-php.bat

Thanks for reading! 😃 If you have any questions or comments please post in the comment section below!

Leave a Comment 0 comments

Preview rendered markdown comment...