Skip to content

Instantly share code, notes, and snippets.

@odan
Last active October 6, 2024 13:21
Show Gist options
  • Save odan/b5f7de8dfbdbf76bef089776c868fea1 to your computer and use it in GitHub Desktop.
Save odan/b5f7de8dfbdbf76bef089776c868fea1 to your computer and use it in GitHub Desktop.
Nginx and PHP Setup on Windows

Nginx and PHP Setup on Windows

For local development you could also use Nginx with PHP as an replacement for XAMPP.

Install Nginx

server {
    listen 80;
    server_name localhost;
    index index.php;
    error_log c:/nginx/logs/localhost.error.log;
    access_log c:/nginx/logs/localhost.access.log;
    root c:/nginx/html;

    location / {
        try_files $uri /index.php$is_args$args;
	}

    location ~ \.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9123;
    }
}

  • Create a file c:/nginx/html/index.php and copy/paste this content:
<?php

phpinfo();
  • To start the webserver run:
    • cd c:\nginx
    • start nginx
  • Allow the windows firewall access
  • Run the tasklist command-line utility to see nginx processes: tasklist /fi "imagename eq nginx.exe"

nginx runs as a standard console application (not a service), and it can be managed using the following commands:

  • nginx -s stop fast shutdown
  • nginx -s quit graceful shutdown
  • nginx -s reload changing configuration, starting new worker processes with a new configuration, graceful shutdown of old worker processes
  • nginx -s reopen re-opening log files
  • taskkill /IM nginx.exe /F Close all nginx processes

Install PHP

  • Download PHP for Windows Thread Safe x64: php-8.0.2-Win32-vs16-x64.zip
  • Extract the ZIP file to: c:\nginx\php
  • Make sure that the file C:\nginx\php\php-cgi.exe exists.
  • Create a new file: c:\nginx\php\php.ini and copy/paste this content:
[PHP]
engine = On
short_open_tag = Off
implicit_flush = Off
zend.enable_gc = On
expose_php = Off
max_execution_time = 30
max_input_time = 60
memory_limit = 512M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
default_mimetype = "text/html"
default_charset = "UTF-8"
include_path = "."
extension_dir = "c:\nginx\php\ext"
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60

extension=curl
extension=fileinfo
extension=gd2
extension=gettext
extension=intl
extension=mbstring
extension=mysqli
extension=openssl
extension=pdo
extension=pdo_mysql
extension=sqlite3

[Session]
session.save_handler = files
session.save_path = "c:\nginx\temp"
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.cookie_samesite =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_bits_per_character = 5

To start PHP with FastCGI run:

  • cd c:\nginx\php
  • php-cgi.exe -b 127.0.0.1:9123
  • Open http://localhost
  • You should see the phpinfo page.

XDebug Setup

@caendesilva
Copy link

Thanks! This was really useful for a quickstart. I ended up combining it with the Batch script from Nginx wiki resource. https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/

Then, I also created a shortcut for the Batch file to start it minimized.

@abdinegoro
Copy link

Hello, thanks for the info. Can you please tell me what I should change if I want the PHP folder to be outside the NGINX folder (i.e in c:\php)

@odzyoba4
Copy link

odzyoba4 commented Jul 6, 2024

For anyone trying this currently, PHP 8.3 raised two issues when starting: a missing gd2 extension and a missing pdo extension. I fixed the gd2 extension issue by editing the php.ini file to change 'gd2' to 'gd', as said here:

In Windows, you'll include the GD DLL php_gd.dll as an extension in php.ini. Prior to PHP 8.0.0, the DLL was named php_gd2.dll.

I fixed the missing pdo extension by commenting out in the php.ini file the line "extension=pdo" for reasons stated here.

@shofwa123
Copy link

thank's

@alexnemynov
Copy link

Hello, thanks for the info. Can you please tell me what I should change if I want the PHP folder to be outside the NGINX folder (i.e in c:\php)

I haven't change anything and it works PHP 8.3.7 & Nginx 1.26.1 separate folders on C:/

@vyndalin
Copy link

vyndalin commented Oct 1, 2024

For anyone trying this currently, PHP 8.3 raised two issues when starting: a missing gd2 extension and a missing pdo extension. I fixed the gd2 extension issue by editing the php.ini file to change 'gd2' to 'gd', as said here:

In Windows, you'll include the GD DLL php_gd.dll as an extension in php.ini. Prior to PHP 8.0.0, the DLL was named php_gd2.dll.

I fixed the missing pdo extension by commenting out in the php.ini file the line "extension=pdo" for reasons stated here.

that works for me - thanks a lot

@vyndalin
Copy link

vyndalin commented Oct 6, 2024

Hello, thanks for the info. Can you please tell me what I should change if I want the PHP folder to be outside the NGINX folder (i.e in c:\php)

The folder from which PHP is executed, whether it's from C:\php or C:\nginx\php, determines where you are running php-cgi.exe from. In this example, it is being run from the location C:\nginx\php\php-cgi.exe.

I use a PowerShell script to run FastCGI for multiple php versions (php-cgi.exe). This allows me to start one instance with PHP 8.0 and another, for example, with version 8.3. In nginx.conf, I then select the PHP version for the server by specifying the port in the fastcgi_pass parameter, such as fastcgi_pass 127.0.0.1:9123

Write-Host "Running PHP 8.0" Start-Process -FilePath "C:\php-8.0\php-cgi.exe" -ArgumentList "-b 127.0.0.1:9123" -WindowStyle Hidden

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment