Nginx is a lightweight high-performance Http WebServer written in an event-driven manner, so compared to Apache
In terms of Nginx, it is more stable, has better performance, and is simple to configure and consumes less resources. Below is my Windows 7 installation of Nginx and PHP5.3
A step of.
Install PHP5
First, download the latest from http://www.php.net/downloads.php
PHP 5.3 Windows version, decompress to C:\php5, rename php.ini-recommended in the compressed package to
php.ini, and then open and modify several options:
error_reporting = E_ALL display_errors = On extension_dir = “C:\php5\ext” ; Dynamic extension, you can remove the comment in front of extension as needed; ; If loading PDO, MySQL extension=php_pdo.dll extension=php_pdo_mysql.dll ; CGI Settings cgi.fix_pathinfo= |
Pay attention to dependencies when loading extensions in PHP, for example, php_exif.dll needs php_mbstring.dll, you must put
php_mbstring.dll must be placed in front of 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, and php_oci8.dll, you need to install Oracle 8
client. If you are not familiar with these dependencies, you can refer to the install.txt file in the installation package.
The search order of dependent files: first is the directory where php.exe is located. If it is in ISAPI mode, it will search for Web Server
The startup location of Apache, such as the bin directory of Apache; followed by Windows PATH
directory in the environment variable. Here do not copy any files into the Windows directory, if necessary, you can put C:\php5
Add it to PATH to facilitate future PHP upgrades.
Install Nginx
Starting from v0.7.52, Nginx started releasing the Windows version of
Nginx, you can download it from its official website:
http://nginx.net
If you need an older version of Nginx for Windows, you can find it in Kevin
Look it up on Worthington ‘s website.
I am using 0.8.29. After downloading, unzip the release file to C:\nginx.
So how to configure Nginx to work with PHP?
Configuring PHP FastCGI
Nginx needs to cooperate 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: -c C:/php5/php.ini |
Another way is to use third-party tools, such as PHP-FPM, cgi-fcgi, etc. Obviously! To run on Windows
It’s a pain in the ass to use these tools in , you’ll probably need something like Cygwin, and some people do, although I think that’s asking for trouble.
Next, modify Nginx to forward php requests 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, that is, $document_root refers to your php scripts root directory, which is set to your website root directory. exist
Under Windows, you need to pay attention to the path of root, it is better to use “/” as the path separator instead of the default Windows
“\”, otherwise it is easy to go wrong, for example, this path: D:\public_html\test, it will not work, Nginx will throw a 500 error, the reason is
\t in \test is parsed as a tab. Of course, it is also possible to add a backslash escape, such as: D:\\public_html\\test.
php.conf configuration file:
# Connect to port 9000 of this machine, the port here refers to the port opened by PHP FastCGI Server, # Please be consistent with the port opened by php-cgi.exe # When Nginx receives a request for a php file, it will automatically forward it to PHP FastCGI Server fastcgi_pass 127.0.0.1:; fastcgi_index index.php; # Nginx does not support CGI PATH_INFO by default, and the value of SCRIPT_NAME is not standard (combined with PATH_INFO) # The following two�� directive, which can strip PATH_INFO from SCRIPT_NAME fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; |
Create a separate php.conf to hold the configuration, purely for simplicity
nginx.conf is just a personal habit, and it can also be written in the main configuration file.
Modify php.ini, set cgi.fix_pathinfo = 1, this is very important, PHP will fix SCRIPT_FILENAME
is the real file address, otherwise PHP will not be able to find the php file that needs to be processed.
Some other settings, master server:
# The number of processes enabled by default worker_processes ; error_log logs/error.log; events { |
When there is no home page file such as default index.php index.html under a certain directory, Nginx will throw 403
ERROR, if you need to list this directory, you can add the following command to http {… }:
autoindex on; autoindex_exact_size on; autoindex_localtime on; |
OK, put it together
Create start_nginx.bat to start PHP FastCGI and Nginx at the same time:
@echo off Invalid under REM Windows REM set PHP_FCGI_CHILDREN=5 REM Maximum number of requests handled per process, or set as a Windows environment variable set PHP_FCGI_MAX_REQUESTS= echo Starting PHP FastCGI… RunHiddenConsole C:/php5/php-cgi.exe -b 127.0.0.1: -c C:/php5/php.ini echo Starting nginx… C:/nginx/nginx.exe |
RunHiddenConsole.exe is a small program used to hide DOS windows, which can be downloaded here.
After start_nginx.bat is turned on, there will also be a DOS window, but it can be turned off safely, and it will not turn off Nginx and
php-cgi.exe.
The same 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 |
The basic configuration is here.