Skip to content

Instantly share code, notes, and snippets.

@odan
Last active April 23, 2024 05:23
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • 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)

@odan
Copy link
Author

odan commented Jan 25, 2024

@abdinegoro There is no change needed. Just cd into the other directory, cd c:\php.

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