Nginx + PHP7 Alongside IIS on Windows System

Robert Long
6 min readDec 21, 2019

--

Introduction

This little tutorial will cover how to run Nginx and integrate it with PHP 7 on a Windows Server system while running alongside IIS. This trick is particularly useful if you want to create a solution in PHP or if you already have an existing solution in PHP and you cannot turn off IIS.

Now before I begin, I go over some of the reasons why you may end up in this situation so you can determine if this approach fits your needs. Here are some of the reasons why you may not be able to turn off IIS…

  • If your company has purchased or is running a solution that exclusively uses IIS and do not have a replacement for it.
  • There are exclusive features on IIS that your company needs and cannot be easily replaced by other solutions in your organization.
  • There are specific features, systems, or pipelines utilized by either by your organization or operating system and it is difficult to find alternatives to these solutions so it’s better to write solutions on top of the windows system.

So if any of these issues fits your criteria, I have a solution that can allow you to run your PHP application on a windows system and take advantage of the existing features in the Windows environment.

The last thing I would like to address is if you’re running Python, Go, or NodeJS server solutions, and you want to host it on a windows machine using Nginx, I have a tutorial for that here so check out that video. This one is specific for PHP 7.

Step 1: Download & Extract Nginx

http://nginx.org/en/download.html

Extract the files to a folder such as c:\nginx server\nginx-1.16.1

Step 2: Download & Extract PHP

https://www.php.net/downloads

It is recommended to use PHP 7.3 at the current time because of issues with PHP7.4 using FastCGI on windows.

Extract the files to a folder such as c:\nginx server\php7.3

Step 3: Download & Extract NSSM

https://nssm.cc/

Extract the files to a folder such as c:\nginx server\nssm

NSSM is a service manager used to install php and nginx as services so it can run in the background even if you’re not logged on to the system.

Step 4: Create a Sample Directory

For test purposes, we will be creating a sample directory. If you already have an existing directory in mind, just make sure your directory read/write privileges for User under the “Security” tab of the properties window.

  • Create a folder c:\sample
  • Create an index.html with some text in the file for testing purposes
  • Right click on your C:\sample has permissions for read and write access to Users

Step 5: Configuring Nginx

Goto your config file {nginx folder}/conf/nginx.conf and modify the server node to look like so…

server {
listen 8022;
server_name 127.0.0.1;

root C:\\sample;
index index.php index.html index.htm;

#location ~ \.php$ {
# fastcgi_pass 127.0.0.1:8023;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#}
}

Step 6: Test Run

Open Command Prompt > Run as Administrator > {nginx folder path}\nginx.exe

If there are no errors, that’s a good sign…

Go to your browser (hopefully chrome/firefox) visit: http://127.0.0.1:8022

You should see your index.html page being served to you correctly.

Stopping nginx…

  • Run command prompt with Administrative Privileges
  • Issue the folllowing: taskkill /f /IM nginx.exe

Step 7: NSSM & Nginx

Install Nginx as a service on your Windows Server machine by launching the Command Prompt as an Administrator

Go to your nssm directory: cd {nssm directory}\win32

Once in your directory, issue: nssm.exe install “nginx service”

A dialog will open with…

  • Path: {path_to_your_nginx_directory}\nginx.exe
  • Click Install Service to register the service
  • [optional] To edit the service simply issue nssm.exe edit “nginx service”

Starting the service…

  • Start Menu > Services > Find “nginx service” > right click > Start

** if you are having issues starting the nginx service, open the command prompt with administrative privileges and issue the following: taskkill /f /IM nginx.exe

Step 8: Running PHP 7

Now we need to run PHP, so modify your c:\sample\index.html and rename it to index.php. Then edit the file and change the source code to . . .

<?php
phpinfo();

Launch PHP FastCGI…

  • Launch Command Prompt with Administrative Privileges
  • cd {your_php7_directory}
  • php-cgi.exe -b 127.0.0.1:8023
  • If you see no errors, that’s a good sign.

If you are getting an VCRUNTIME140.dll error, make sure to download the runtime for Visual Studio C++ 2015: here

If you are getting this error, check the like above.

*As a note, this tutorial recommends at this time (Dec 2019) that you use PHP 7.3 since we are encountering issues with 7.4 FastCGI with Nginx on Windows. If you’re using PHP 7.4 or higher and encountering issues, try using 7.3.

Step 9: Test Run with Nginx

Now we have to go back to nginx and tell it to talk to PHP…

  • Open your nginx config file with an editor {nginx directory}/conf/nginx.conf
  • Remove the #’s (comments) in our server (see below)
  • Save the file!
server {
listen 8022;
server_name 127.0.0.1;
root C:\\sample;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:8023;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Restart & Retest…

  • Goto your Services > right click on “nginx service” > restart
  • If you’re having problems, issue taskkill /f /IM nginx.exe in CMD with Admin Privs
  • Goto url http://127.0.0.1:8022
  • You should now see the php info page.
  • If you do not, make sure you changed your sample\index.html file is changed to index.php and the source code is changed to phpinfo()

Step 10: Registering PHP as a Service

At this point, you should have Nginx talking to PHP whenever it detects a *.php file. Congratulations, you are running PHP7 on a windows system. Now to cement the two…

Shutdown running PHP instance…

  • Launch Command Prompt with Admin Privs
  • taskkill /f /IM php-cgi.exe

Register PHP as a Service…

  • Goto your Command Prompt with Admin Privs
  • cd {nssm_directory}\win32
  • nssm.exe install “php7 service”

You will be presented with some fields, populate them appropriately…

  • Path: {path_to_php7_directory}\php-cgi.exe
  • Arguments: -b 127.0.0.1:8023
  • Click Install Service
  • [optional] To edit this service, issue nssm.exe edit “php service”

Start the PHP7 service…

  • Start Menu > type “services” > Launch Services
  • Find “php7 service
  • Right click > Start

Go to url http://127.0.0.1:8022

  • You should see the php info page.

At this point, you can add additional servers to the nginx config file by adding additional server blocks (server {}) on different ports and use the same PHP7 fast-cgi url originally http://127.0.0.1:8023 by adding the ~ \.php$ block to your new server. You can now modify the fast-cgi url to be whatever port your heart desires. If you want more examples I have included my own windows configuration server file here. You can find more online or reach out to me directly.

Step 11: IIS Reverse Proxy to Nginx

The last step is to get IIS to talk to Nginx; we do this by using reverse proxy (URL Rewrite). To setup Reverse Proxy on IIS, please go here.

Want More?

Website: https://intully.com

FB Community: https://intully.com

--

--

Responses (2)