How to install nginx and PHP on Windows

Once in a while you want to have a test web development environment in which you can test your ideas more freely. Considering that most server systems we have dealt with are all linux (I don’t know about you) setting up one on a windows system without using the existing WEMP( Windows Nginx Mysql Php) packages was a bit challenging for us. We then wrote this article to for us to have a place to jump to next time we need that. If somebody else finds it helpful even better!

We assume you have a mysql environment ready. You can easily download and install MYSQL for windows at their website.Now get to work:

Downoad Ngnix

Grab yourself a copy of the latest version of nginx at their website and extract it to a directory on your PC

Mine is installed in :

C:\nginx_php\nginx-1.13.0

Download PHP

Grab a copy of PHP at their website as well and put it inside a “php” directory in where you put nginx. You can see the php directory in the image below.

Download the visual studio dependencies:

At the php download page in the sidebar there is critical info you should know. I quote them here :

More recent versions of PHP are built with VC11, VC14 or VC15 (Visual Studio 2012, 2015 or 2017 compiler respectively) and include improvements in performance and stability.

– The VC11 builds require to have the Visual C++ Redistributable for Visual Studio 2012 x86 or x64 installed

– The VC14 builds require to have the Visual C++ Redistributable for Visual Studio 2015 x86 or x64 installed

– The VC15 builds require to have the Visual C++ Redistributable for Visual Studio 2017 x64 or x86 installed

 

 

Basicaly, depending on the php binary you downloaded, you will need a different visual studio redistributable dependencies. A rather mechanic way to test this is to just run php as shown later in the guide and let the system tell you what it needs an example error is shown below for reference

Missing VCRUNTIME140.dll

This suggests that wee need to install the vs2015 redistribuabutables in this particular case, you can find a good discussion on the subject here. With all the downloads taken care of we can now configure our servers.

Configure nginx

Now we have all the files we need, time for some configuration, we start with nginx, open its configuration file located at

/conf/nginx.conf

in the nginx directory, the full path for my set up is :

C:\nginx_php\nginx-1.13.0\conf\nginx.conf

Open the file and uncomment (remove the #) the lines below :

#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

next edit fastcgi_param to point to the html directory inside nginx, this directory is where your webpage files will be hosted.My configuration is shown below , adjust according to your needs.

fastcgi_param  SCRIPT_FILENAME  C:/nginx_php/nginx-1.13.0/html$fastcgi_script_name;

Configure php

Head into the php directory and rename php.ini-production or php.ini-development to php.ini (depending on your needs). Next open the php.ini file inside the php directory and configure the path to the extension binaries. Mine is configured as shown below :

extension_dir = C:\nginx_php\nginx-1.13.0\php\ext

One last thing, because we use the mysql database, we need to enable the mysql drivers for them to be usable, do this in the same php.ini file as shown below , enable by removing the “;” :

extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll

With all this, we are done with the basic configurations.

Start php

Go into the php directory. Hold SHIFT , right click on an empty spot and choose Open command window here, in the command line interface that shows up type the command :

php-cgi -b 127.0.0.1:9000

and press enter. PHP should now be up and running.

Start nginx

Head into the nginx directory and repeat the same steps, Hold SHIFT , right click on an empty spot and choose Open command window here,in the command line interface that shows up , type the command

nginx

and press enter. Your nginx instance should now be up and running.

Test it all

To test your setup just open your favorite browser and type

localhost

in the browser window and you should see a default nginx page confirming that your server is now working

Your web directory

Our nginx server is now configured to use the

C:\nginx_php\nginx-1.13.0\html

directory as the location for our web pages, if you go in there, you will find an index.html file that is generating the nginx welcome page we just saw above. This directory is configured in the nginx.conf file by the line :

root           html;

Create a new file in there and name it test.php and put in the content below :

<?php phpinfo(); ?>

Save the file and in your browser, visit

localhost/test.php

and you should see a page showing all the configurations for your php instance as shown below:

Now you have a working local server environment. I hope this has been informative for you and thanks for reading.

Posted in Tips and tricks, Tutorials and tagged , , .

Daniel Gakwaya loves computer Hardware/Software.He is a Software Engineer at BLIKOON and lead developer of bliboard-The whiteboard system currently marketed by the company.He is known to hack around on any piece of tech that happens to pick his interest. More on his tech endeavors here
Follow him on Twitter

18 Comments

  1. I’m getting “No input file specified” after following all these steps. Any idea what it could be?

    I’m using php 7 too.

    • Are you sure you uncommented the block below ?


      location ~ \.php$ {
      root html;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      include fastcgi_params;
      }

    • Sounds like your home directory is not well configured. Can you share your configuration file? That way I have a better chance to spot the problem.

    • Sounds like your php backend configuration didn’t take effect. Can I have a look at your configuration file ? So I can try and spot the problem ?

  2. #nginx.conf file
    #user nobody;
    worker_processes 1;
    #error_log logs/error.log;
    #error_log logs/error.log notice;
    #error_log logs/error.log info;
    #pid logs/nginx.pid;
    events {
    worker_connections 1024;
    }
    http {
    include mime.types;
    default_type application/octet-stream;
    #log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
    # ‘$status $body_bytes_sent “$http_referer” ‘
    # ‘”$http_user_agent” “$http_x_forwarded_for”‘;
    #access_log logs/access.log main;
    sendfile on;
    #tcp_nopush on;
    #keepalive_timeout 0;
    keepalive_timeout 65;
    #gzip on;
    server {
    listen 80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
    root html;
    index index.html index.htm;
    }
    #error_page 404 /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    # proxy_pass http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #root html;
    #fastcgi_pass 127.0.0.1:9000;
    #fastcgi_index index.php;
    #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    #include fastcgi.conf;
    #include fastcgi_params;
    fastcgi_pass 127.0.0.1:9123;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    # deny access to .htaccess files, if Apache’s document root
    # concurs with nginx’s one
    #
    #location ~ /\.ht {
    # deny all;
    #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    # listen 8000;
    # listen somename:8080;
    # server_name somename alias another.alias;

    # location / {
    # root html;
    # index index.html index.htm;
    # }
    #}
    # HTTPS server
    #
    #server {
    # listen 443 ssl;
    # server_name localhost;

    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.key;

    # ssl_session_cache shared:SSL:1m;
    # ssl_session_timeout 5m;

    # ssl_ciphers HIGH:!aNULL:!MD5;
    # ssl_prefer_server_ciphers on;

    # location / {
    # root html;
    # index index.html index.htm;
    # }
    #}
    }

    • Please make sure the block below is uncommented


      location ~ \.php$ {
      root html;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      include fastcgi_params;
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.