You are on page 1of 33

NGINX and PHP

Installation and Configuration


NGINX
 HTTP and reverse proxy server, a mail proxy
server, and a generic TCP/UDP proxy server.
 Originally written by Igor Sysoev.
 Served or proxied 30.46% busiest sites in
January 2018, including Dropbox, Netflix,
Wordpress.com, FastMail.FM
 The sources and documentation are distributed
under the 2-clause BSD-like license.
 Commercial support is available from Nginx,
Inc. (www.nginx.com)
Where to get ?

 Official web site :


 http://www.nginx.org
 Latest version :
 Stable : 1.12.xx
 Mainline : 1.13.xx
 Supported Platform :
 UNIX, Linux, Mac OSX
 MS Windows Server or Desktop
Additional Resources

 E-book can be
downloaded for free at
https://www.nginx.com
/resources/library/com
plete-nginx-
cookbook/?utm_sourc
e=nginxorg&utm_medi
um=homepagebanner
&utm_campaign=com
plete_cookbook
Download

Click here to download


NGINX for Windows

Click here to download


NGINX for Unix
Installing NGINX on MS
Windows Platform

 Extract the downloaded zip file to any folder


you like, i.e. c:\nginx, as long as no space in
folder name.
 Go to folder conf and edit nginx.conf file to
configure nginx.
Running NGINX on MS
Windows Platform
 To start NGINX, go to folder root of the
NGINX folder, and type "nginx" and
press Enter.
 To stop the NGINX, go to folder root of the
NGINX folder, and type "taskkill /f
/IM nginx.exe" and press Enter.
NGINX Configuration

 Prior of running NGINX, we have to


configure it first.
 The configuration file is usually located
at folder nginx/conf/.
 The default name for NGINX
configuration file is nginx.conf.
 The configuration file basically is a text
file, so we can change it using any text
editor program.
Important Settings
 There are few settings in nginx.conf file
that must be set, prior to running the
nginx.exe :
 worker_connections : Maximum number of
connection that can be served.
 sendfile : Enabling sending files capabilities.
 keepalive_timeout : number of seconds
before timeout.
 listen : number of port that NGINX will listen.
 server_name : name or IP address of NGINX
server.
Important Settings, cont…
 root : Location of root folder of the HTML
document (under NGINX folder).
 index : name of index (default) file to open
when user do not supply a file name.
 error_page : name/location of file to be open
when particularly error happens.
 location : redirect location when particular
pattern appears.
 Include : includes another file, or files
matching the specified mask, into configuration.
Warning !!!

 Every time you edit (make changes) in


nginx.conf file, you must shutdown
and start again the NGINX in order for
the change(s) to take effects.
 If you not restart the NGINX, the
changes will not affect anything until
next start.
PHP

 PHP is a popular general-purpose


scripting language that is especially
suited to web development.
 Most popular scripting language in use
for websites.
 PHP is free to download and use.
Where to get ?

 Official web site :


 www.php.net
 Latest version :
 7.0.xx
 Supported Platform :
 UNIX, Linux, Mac OSX
 MS Windows Server 2000 or higher
Unix Platform

Click here to download PHP for


Unix/Linux/OSX Platform
MS Windows Platform

Click here to download PHP


for Windows Platform
MS Windows Platform, cont…
MS Windows Platform, cont…

 Which version to download ?


 Non Thread Safe versions are for computer that
only have single core single thread processor,
for non threaded environment, or for testing
purpose only.
 Nowadays, most platforms have multicore
multithread processor, so its better to download
the Thread Safe versions.
 Always download the version that matched to
the NGINX version installed.
Installing PHP
 Extract the downloaded file into any folder you
choose, preferably inside the NGINX folder,
i.e. c:\nginx\php.
 Add few settings in nginx.conf file :
location ~ \.php$ {
try_files $uri =404;
location ~ \..*/.*\.php$ {return 404;}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
c:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}

 Restart the NGINX Server.


PHP Configuration

 Prior of running PHP, we have to configure it


first.
 The configuration file is located at folder
where the PHP is installed.
 The default name for PHP configuration file
is php.ini.
 The configuration file basically is a text file,
so we can change it using any text editor
program.
Important Settings
 There are few settings in php.ini file that
must be set, before running the PHP :
 engine : Directive to enable/disable PHP.
 short_open_tag : Directive to enable/disable
short tag.
 asp_tags : Directive to enable/disable ASP
like tag.
 Precision : Directive to set how many digit
use in floating point number.
Warning !!!

 Every time you edit (make changes) in


php.ini file, you must restart the
NGINX Server in order for the
change(s) to take effects.
 If you not restart the NGINX Server,
the changes will not affect anything
until next start/restart.
Non-Sucking Service Manager
(NSSM)

 To run NGINX and PHP automatically,


it’s preferable to run them as services.
 NSSM is a service helper which
doesn't suck, because it handle failure
of the application running as a service.
 NSSM monitors the running service
and will restart it if it dies. With NSSM
you know that if a service says it's
running, it really is.
Non-Sucking Service Manager
(NSSM), cont…
 You can configure NSSM to absolve all
responsibility for restarting it and let
Windows take care of recovery actions.
 NSSM logs its progress to the system Event
Log so you can get some idea of why an
application isn't behaving as it should.
 NSSM also features a graphical service
installation and removal facility.
 NSSM is public domain. You may
unconditionally use it and/or its source code
for any purpose you wish.
Where to get ?

 Official web site :


 www.nssm.cc
 Latest version :
 2.24-xxx
 Supported Platform :
 MS Windows 2000 or later
Installing NSSM
 Extract the downloaded file into any folder you
choose, preferably inside the Program Files
folder, i.e. c:\Program Files\.
Installing NGINX as a service
using NSSM

 Open the installed folder.


 Select folder that matched
your operating system.
Installing NGINX as a service
using NSSM, cont…

 Press Shift + RightClick


simultaneously.
 Select Open command
window here.
 Type nssm install.
Installing NGINX as a service
using NSSM, cont…
Select nginx.exe

Select NGINX folder

After everything is done, click


here to install NGINX service

Type the service name, i.e. “NGINX Service”


Installing PHP as a service
using NSSM

 Open the installed folder.


 Select folder that matched
your operating system.
Installing PHP as a service
using NSSM, cont…

 Press Shift + RightClick


simultaneously.
 Select Open command
window here.
 Type nssm install.
Installing PHP as a service
using NSSM, cont…
Select php-cgi.exe

Select PHP folder

After everything is
done, click here to
install NGINX
service

Type “-b 127.0.0.1:9000 -c c:\nginx\php\php.ini”

Type the service name, i.e. “PHP Service”


Running NGINX and PHP
Services

 Press Windows Key + R


simultaneously.
 Type services.msc.
 Select PHP Service, and click Start.
 Select NGINX Service, and click
Start.
 To stop the service, select the service
and click Stop.
Important !!!

 You should run the PHP service prior


to NGINX service.
 Failed to do so can result that NGINX
unable to recognize the PHP service.
 You can test your services by
browsing to localhost (127.0.0.1).

You might also like