Setting up Windows
There are many tools available that have Apache, PHP, and MySQL bundled for Windows, provide easy installation, and are very easy to use. Most of these tools already provide support for PHP 7 with Apache, such as through XAMPP, WAMPP, and EasyPHP. EasyPHP is the only one that also provides support for NGINX and provides easy steps to changes webserver from NGINX to Apache or Apache to Nginx.
Note
XAMPP is also available for Linux and Mac OS X. However, WAMP and EasyPHP are only available for Windows. Any of these three can be used for this book, but we recommend EasyPHP as it supports NGINX, and for this book, we mostly use NGINX.
Any of the three tools can be used, but we require more control over every element of our web server tools, so we will also install NGINX, PHP 7, and MySQL individually and then connect them together.
Note
NGINX Windows binaries can be downloaded from http://nginx.org/en/download.html. We recommend using a stable version, though there is no problem with using a mainline version. PHP Windows binaries can be downloaded from http://windows.php.net/download/. Download either 32-bit or 64-bit binaries of the non-thread safe version according to your system.
Perform the following steps:
- Download NGINX and PHP Windows binaries mentioned in the information box. Copy NGINX to a suitable directory. For example, we have a completely separate D drive for development purposes. Copy NGINX to this development drive or any other directory. Now, copy PHP either to the NGINX directory or to any other secure folder location.
- In the PHP directory, there will be two
.ini
files,php.ini-development
andphp.ini-production
. Rename either one of them tophp.ini
. PHP will be using this configuration file. - Hold the Shift key and right click in the PHP directory to open the command-line window. The command-line window will be opened in the same location path. Issue the following command to start PHP:
php-cgi –b 127.0.0.1:9000
The
–b
option starts PHP and binds to the path for external FastCGI servers. The preceding command binds PHP to loop back the127.0.0.1
IP on port9000
. Now, PHP is accessible on this path. - To configure NGINX, open the
nginx_folder/conf/nginx.conf
file. The first thing to do is to add root and index to the server block, as follows:server { root html; index index.php index.html index.htm;
Tip
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
- Log in or register to our website using your e-mail address and password.
- Hover the mouse pointer on the SUPPORT tab at the top.
- Click on Code Downloads & Errata.
- Enter the name of the book in the Search box.
- Select the book for which you're looking to download the code files.
- Choose from the drop-down menu where you purchased this book from.
- Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR / 7-Zip for Windows
- Zipeg / iZip / UnRarX for Mac
- 7-Zip / PeaZip for Linux
- Now, we need to configure NGINX to use PHP as FastCGI on the path mentioned before on which it is started. In the
nginx.conf
file, uncomment the following location block for PHP:location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME complete_path_webroot_folder$fastcgi_script_name; include fastcgi_params; }
Note the
fastcgi_param
option. The highlightedcomplete_path_webroot_folder
path should be the absolute path to the HTML directory inside thenginx
folder. Let's say that your NGINX is placed at theD:\nginx
path; then, the absolute path to theHTML
folder will beD:\nginx\html
. However, for the precedingfastcgi_param
option,\
should be replaced by/
. - Now, restart NGINX by issuing the following command in the root of the NGINX folder:
nginx –s restart
- After NGINX is restarted, open your browser and enter the IP or hostname of your Windows server or machine, and we will see the NGINX welcome message.
- Now, to verify the PHP installation and its working with NGINX, create an
info.php
file in webroot and enter the following code in it:<?php phpinfo(); ?>
- Now, in the browser, access your_ip/info.php, and we will be presented with a page full of PHP and server information. Congratulations! We configured NGINX and PHP to work perfectly together.
Note
On Windows and Mac OS X, we recommend that you use a virtual machine installed with all the tools on a Linux flavor to get the best performance out of the server. It is easy to manage everything in Linux. There are vagrant boxes available that have everything ready to use. Also, a custom virtual machine configuration with all the tools, including NGINX, Apache, PHP 7, Ubuntu, Debian, or CentOS, and other great ones, can be made at https://puphpet.com, which is an easy-to-use GUI. Another nice tool is Laravel Homestead, which is a Vagrant box with great tools.