DEV Community

Cover image for Windows 10 Nginx + PHP
ilhamsabir
ilhamsabir

Posted on

32 2

Windows 10 Nginx + PHP

NGINX can interface with PHP on Windows via a FastCGI daemon, which ships with PHP: php-cgi.exe. You need to run php-cgi.exe -b 127.0.0.1: and use fastcgi_pass 127.0.0.1:; in the NGINX configuration file. After being launched, php-cgi.exe will keep listening for connections in a command prompt window. To hide that window, use the tiny utility RunHiddenConsole

First

  • Download nginx, choose stable version.
  • Download PHP, choose non stable version.

Second

  • Extract nginx to "C:\nginx"
  • Extract php to "C:\php"
  • Create folder "www" in your "C" system , "C:\www"

Third

  • Edit your nginx.conf at "C:\nginx\conf\nginx.conf"
  • Change root

        root c:/www;
    
  • Save nginx.conf

  • Open yout php folder, find php-ini-development

  • Edit your php-ini-development, like down below.

        extension_dir = "ext" 
        enable_dl = On 
        cgi.force_redirect = 1 
        fastcgi.impersonate = 1 
        cgi.rfc2616_headers = 1 
        extension=php_gd2.dll 
        extension=php_mbstring.dll 
        extension=php_exif.dll 
        extension=php_mysql.dll 
        extension=php_mysqli.dll 
        extension=php_pdo_mysql.dll 
        date.timezone = "Asia/Jakarta" 
    
  • Save it.

  • Open your nginx.conf again

  • Uncoment on php configuration and change to this

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9999
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9999;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    
  • Save nginx.conf again.

Fourth

  • Set Php path
  • Open your environment variable
    • Open "edit the system environment variables"
    • Click, "Environtment variables ..." Alt Text
    • Edit "path" on "Sytem ariables" Alt Text
    • Add new path "C:\php" Alt Text

Create nginx start, stop & restart .bat

  • Download RunHiddenConsole first, and move RunHiddenConsole.exe to "C:\".
  • Create file "nginx-start.bat", edit file with down below, make your php-cgi.exe port same as in nginx.conf

        @ECHO OFF
    
        ECHO Starting PHP FastCGI...
        C:\RunHiddenConsole.exe C:\php8\php-cgi.exe -b 127.0.0.1:9999
    
        ECHO Starting NGINX
        start nginx.exe
    
        popd
        EXIT /b
    
  • Create file "nginx-stop.bat", edit with this

        @ECHO OFF
        taskkill /f /IM nginx.exe
        taskkill /f /IM php-cgi.exe
        EXIT /b
    
  • Create file "nginx-restart.bat"

        @ECHO OFF
        call nginx-stop.bat
        call nginx-start.bat
        EXIT /b
    

Test

  • Doble click on "nginx-start.bat"
  • Open your browser if every thing work you can see like this Alt Text
  • Create folder like "test-php" in "C:\www"
  • Create file "index.php"

     <?php phpinfo(); ?>
    
  • Open your browser and go to "localhost/test-php"

  • If every thing ok you can see
    Alt Text

  • At the end , nginx php now working.

Notes:
Sometimes , nginx & php cannot working properly if your run with nginx-start.bat
so you need to run php manually
open your terminal, run your php "php-cgi -b 127.0.0.1:9999"
and now your nginx & php work properly

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (2)

Collapse
 
marre86 profile image
Artem Pokhiliuk • Edited

why is it said at the beginning of the article that we must download NON STABLE version of PHP ?

Collapse
 
ilhamsabir profile image
ilhamsabir

I tried to use stable version , but its not work in my system

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay