Installation and configuration of Nginx + PHP5 under Windows

  • 2020-05-06 12:14:30
  • OfStack

install PHP5
First of all, from http: / / www php. net/downloads php download the latest PHP Windows version 5.3, decompression to C: \ php5, put the package of php. ini - recommended, renamed php. ini, then open the modify several options:

 
error_reporting = E_ALL 
display_errors = On 
extension_dir = "C:\php5\ext" 

;  Dynamic extension, which can be removed as needed  extension  Previous comments  ; 
;  Such as load  PDO, MySQL 
extension=php_pdo.dll 
extension=php_pdo_mysql.dll 

; CGI  Set up the  
cgi.fix_pathinfo = 1 


The PHP load extension needs to be aware of the dependencies, for example php_exif.dll needs php_mbstring.dll, you must put php_mbstring.dll before php_exif.dll to load successfully. Some extensions rely on additional dll files, such as PHP 5.0+, php_mysqli.dll depends on libmysql.dll, while php_oci8.dll, you need to install Oracle 8 clients. If you are not familiar with these dependencies, refer to the install.txt file in the installation package.

Depending on the search order of the files: first, the directory where php.exe is located. If it is in ISAPI mode, then the launch location of Web Server will be searched, such as bin directory of Apache. Next is the directory in the Windows PATH environment variable. Do not copy any files into the Windows directory here. If necessary, you can add C:\php5 to PATH to facilitate the upgrade of PHP in the future.

installs Nginx
starts with v0.7.52, Nginx releases Nginx version Windows, you can download
from its official website http://nginx.net

If you need an older version of Nginx for Windows, check out the Kevin Worthington website.

I used 0.8.29, after downloading, unzip the file to C:\nginx.

So how do you configure Nginx to work with PHP?
is configured with PHP FastCGI
Nginx requires cooperation with FastCGI Server to process requests. There are two ways to run PHP FastCGI Server. One is to use PHP's built-in FastCGI manager,

C:/php5/php-cgi.exe -b 127.0.0.1:9000 -c C:/php5/php.ini

Another way is to use third-party tools such as PHP-FPM, cgi-fcgi, etc. Obviously! It's extremely painful to use these tools in Windows, and you probably need something like Cygwin, which some people do, although I think that's self-defeating.

Next, modify Nginx and forward the php request to PHP FastCGI Server:

 
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
location ~ ^(.+\.php)(.*)$ { 
root D:/public_html; 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
include php.conf; 
} 

root ($document_root) refers to your php scripts root, which is set to the root of your website. Under Windows, it is important to note that the path of root is best to use "/" as the path separator instead of the default "\" of Windows, otherwise it is easy to cause problems. For example, this path: D:\public_html\test will not work and Nginx will throw 500 errors because \t in \test is resolved as a TAB. Of course, it is also possible to escape with a backslash, e.g. D:\\public_html\\test.

php.conf configuration file:
 
#  Connect to the machine  9000  Port, port here means  PHP FastCGI Server  Open port,  
#  Please contact with  php-cgi.exe  Keep the ports open the same  
#  when  Nginx  received  php  Requests for files are automatically forwarded to  PHP FastCGI Server 
fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php; 

# Nginx  The default is no  CGI PATH_INFO . SCRIPT_NAME  Is also not standard  PATH_INFO )  
#  The following two lines of instructions can be read from  SCRIPT_NAME  Stripping out the  PATH_INFO 
fastcgi_split_path_info ^(.+\.php)(.*)$; 
fastcgi_param PATH_INFO $fastcgi_path_info; 

include fastcgi_params; 

Create a separate php.conf save configuration purely to simplify nginx.conf, personal habits only, can also be written in the main configuration file.

Es18en.ini, cgi.fix_pathinfo = 1, this is very important, PHP will fix SCRIPT_FILENAME as the real file address, otherwise PHP will not find the php file to be processed.

Some other Settings for , master server:

 
#  Number of processes opened by default  
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 { 
#  The maximum number of connections a process can handle,  
#  Local development, no default required  1024 , here changed to  64 
worker_connections 64; 
} 

Nginx throws 403 ERROR when the default index.php index.html home page file does not exist under a directory. Add the following command:
 
autoindex on; 
autoindex_exact_size on; 
autoindex_localtime on; 

OK, integrated
Create start_nginx.bat to launch both PHP FastCGI and Nginx:
 
@echo off 
REM Windows  The invalid  
REM set PHP_FCGI_CHILDREN=5 

REM  Maximum number of requests per process, or set to  Windows  The environment variable  
set PHP_FCGI_MAX_REQUESTS=1000 

echo Starting PHP FastCGI... 
RunHiddenConsole C:/php5/php-cgi.exe -b 127.0.0.1:9000 -c C:/php5/php.ini 

echo Starting nginx... 
C:/nginx/nginx.exe 

RunHiddenConsole.exe is a small program to hide the DOS window. You can download it here.
Es233en_nginx.bat will also have DOS Windows when opened, but you can safely close Nginx and php-cgi.exe will not be closed.

also stop_nginx.bat, used to close:

 
@echo off 
echo Stopping nginx... 
taskkill /F /IM nginx.exe > nul 
echo Stopping PHP FastCGI... 
taskkill /F /IM php-cgi.exe > nul 
exit 

That's pretty much it.

Related articles: