Installation and configuration of Nginx+PHP5 under Windows

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

Here are the steps I took to install Nginx and PHP5.2 in Windows   2003, but windows version nginx performed much worse than Linux/Uninx version Nginx.

Install PHP5
First of all, from http: / / www php. net/downloads php under load the latest PHP5. 2.9 2 Windows version, to extract the C: \ php, php of the compressed package. ini - recommended, renamed php. ini, then open the modified several options:

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

; Dynamic extension, you can remove comments before extension as needed;
; Such as loading PDO, MySQL
extension=php_pdo.dll
extension=php_pdo_mysql.dll

; CGI set
cgi.force_redirect = 1
cgi.fix_pathinfo = 1
cgi.rfc2616_headers = 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 the Oracle 8 client. If you are not familiar with these dependencies, refer to the install.txt file in the installation package.

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

Install Nginx
From v0. 7.52, Nginx started publishing Windows Nginx version, you can download it on its official website: http: / / nginx net
I used 0.8.37. After downloading, unzip the file to D:\nginx.

Configuration PHP FastCGI
Nginx needs to work with FastCGI Server to process requests. There are two ways to run PHP FastCGI Server, and one is to use FastCGI manager built into PHP:
Command line below to perform c: / php php - cgi. exe - b 127.0.0.1:9000 - c c: / php/php ini to start PHP FastCGI

Modify the Nginx configuration file d\nginx\conf\nginx # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root       d:/public_html;
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 root path is best to use "/" as the path separator rather than 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 character. It is also possible to escape with a backslash, e.g. d:\\public_html\\test.

php.conf is the file I created to save the php configuration. There are only three lines:
Connect to the native 9000 port, which is the port opened by PHP FastCGI Server,
Please keep
port open with php-cgi.exe When Nginx receives a request for php files, it is automatically forwarded to PHP FastCGI Server
fastcgi_pass       127.0.0.1:9000;
fastcgi_index     index.php;
include         fastcgi_params;
The reason for creating a separate php.conf save configuration to simplify nginx.conf, when multiple virtual hosts are configured in nginx, each virtual host needs to be configured with php, and the master configuration file becomes redundant and bloated.

Need to modify d:\nginx\conf\fastcgi_params file, add a line:
fastcgi_param   SCRIPT_FILENAME       $document_root$fastcgi_script_name;

Also, it is important to modify php.ini and set cgi.fix_pathinfo = 1 , otherwise PHP will not find the php script that needs to be processed.

Some other Settings, master server:
The number of processes opened by default is
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 {
Maximum number of connections a process can handle,
Local development, no need for the default 1024, here change 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;
 
start_nginx.bat , for launching PHP FastCGI and Nginx:
at the same time @echo off
echo Starting PHP FastCGI...
RunHiddenConsole c:/php/php-cgi.exe -b 127.0.0.1:9000 -c c:/php/php.ini
echo Starting nginx...
d:/nginx/nginx.exe
Es321en.exe is a small program to hide the DOS window. You can download RunHiddenConsole.zip (1.01 kb) here.
Es328en_nginx.bat will also have DOS Windows when opened, but you can safely close them without closing Nginx and php-cgi.exe.
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.

If you feel more troublesome, here is the recommended software phpfind, after the installation of windows system configuration nginx+php.