Windows 10 Nginx + PHP
Tags: php, programming, nginx, devops
Published: 2021-03-24 00:51:16

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:<port> and use fastcgi_pass 127.0.0.1:<port>; 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](https://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip)

## First
- Download [nginx](http://nginx.org/en/download.html), choose stable version.
- Download [PHP](https://windows.php.net/download#php-8.0), 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
    ```sh
        root c:/www;
    ```
- Save nginx.conf
- Open yout php folder, find php-ini-development
- Edit your php-ini-development, like down below.
    ```sh
        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
    ```sh
        # 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxcxqhrg0f2q4s81lndm.jpg)
    * Edit "path" on "Sytem ariables"
        ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewvzvvqzx7dcyygxqpqy.jpg)
    * Add new path "C:\php"
        ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqz9s45oajvo94finjzk.jpg)

## Create nginx start, stop & restart .bat
- Download [RunHiddenConsole](https://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip) 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
    ```sh
        @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
    ```sh
        @ECHO OFF
        taskkill /f /IM nginx.exe
        taskkill /f /IM php-cgi.exe
        EXIT /b
    ```
- Create file "nginx-restart.bat"
    ```sh
        @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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cg5w6pa0xdu6z0xzkye3.jpg)
- Create folder like "test-php" in "C:\www"
- Create file "index.php"
    ```php
     <?php phpinfo(); ?>
    ```
- Open your browser and go to "localhost/test-php"
- If every thing ok you can see
    ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzk5u21rmuormk4ymm3v.jpg)
- 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