Installation and configuration methods for Nginx + PHP5 under Windows _nginx

Source: Internet
Author: User
Install PHP5
First, download the latest PHP 5.3 Windows version from http://www.php.net/downloads.php, extract it to C:\php5, and rename the php.ini-recommended in the zip package to PHP.ini, and then turn on modifying several options:

Copy Code code as follows:

error_reporting = E_all
Display_errors = On
Extension_dir = "C:\php5\ext"

; Dynamic expansion, you can remove the extension before the annotation as needed;
; such as loading PDO, MySQL
Extension=php_pdo.dll
Extension=php_pdo_mysql.dll

; CGI settings
Cgi.fix_pathinfo = 1


PHP load extensions need to pay attention to dependencies, such as Php_exif.dll need Php_mbstring.dll, you have to put Php_mbstring.dll in front of Php_exif.dll to load successfully. Some extensions rely on additional DLL files, such as PHP 5.0+, Php_mysqli.dll rely on Libmysql.dll, and Php_oci8.dll, you need to install Oracle 8 clients. If you are not too familiar with these dependencies, refer to the Install.txt file in the installation package.

Search order for dependent files: first, the directory where Php.exe resides, and if it is ISAPI mode, search the startup location of the Web Server, such as the Apache bin directory, followed by the directory in the Windows PATH environment variable. Do not copy any files to the Windows directory, if necessary, you can add C:\php5 to the PATH to facilitate later PHP upgrades.

Install Nginx
Starting with v0.7.52, Nginx started publishing the Windows version of Nginx, which you can download on its official website:
Http://nginx.net

If you need an older version of Nginx for Windows, you can look for it on Kevin Worthington's website.

I am using 0.8.29, after downloading good, decompression release file to C:\nginx.

So how do you configure Nginx so that it works with PHP?
Configure PHP FastCGI
Nginx need to work with FastCGI server to handle requests, there are two ways to run PHP FastCGI server, one is to use the PHP 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, and so on. Obviously! It's extremely painful to use these tools in Windows, and you might need something like Cygwin, and someone did, though I thought it was a matter of trouble.

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

Copy Code code as follows:

# 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 is $document _root refers to your PHP scripts root directory, set to your site root directory. Under Windows, you need to be aware of the root path, preferably using "/" as the path separator, rather than Windows default "\", otherwise prone to problems, such as the path: D:\public_html\test, will not work, Nginx would throw 500 Error, because \ t is parsed as a tab in \test. Of course, plus a backslash escape is also possible, such as: D:\\public_html\\test.

php.conf configuration file:
Copy Code code as follows:

# Connect to the native 9000 port, where the port refers to the port on which the PHP FastCGI Server is opened,
# Please keep in line with the ports opened by Php-cgi.exe
# when Nginx receives a request for a PHP file, it is automatically forwarded to the PHP FastCGI Server
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;

# Nginx default is not supported CGI Path_info,script_name values are also not standard (blends the Path_info)
# The following two lines of instruction can be stripped out of the Script_name Path_info
Fastcgi_split_path_info ^ (. +\.php) (. *) $;
Fastcgi_param path_info $fastcgi _path_info;

Include Fastcgi_params;

Create a separate php.conf save configuration, simply to streamline nginx.conf, personal habits, or write them all in the main configuration file.

Modify PHP.ini, set Cgi.fix_pathinfo = 1, this is very important, PHP will fix script_filename for the real file address, otherwise PHP will not be able to find the PHP file to be processed.

Some other settings, master server:

Copy Code code as follows:

# 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 processed by a process,
# Local development, do not need the default 1024, this is changed to 64
Worker_connections 64;
}

When a directory does not exist under the default index.php index.html and other home files, Nginx will throw 403 ERROR, if you need to list this directory, you can add the following command in HTTP {...}:
Copy Code code as follows:

AutoIndex on;
Autoindex_exact_size on;
Autoindex_localtime on;

OK, integrated together
Create Start_nginx.bat to start both PHP FastCGI and Nginx:
Copy Code code as follows:

@echo off
Invalid under REM Windows
REM Set php_fcgi_children=5

REM the maximum number of requests processed per process, or set as Windows 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, which can be downloaded here.
After the Start_nginx.bat is opened, there will also be a DOS window, but it can be safely turned off and will not close Nginx and php-cgi.exe.

same stop_nginx.bat, used to close:

Copy Code code as follows:

@echo off
echo Stopping Nginx ...
taskkill/f/im nginx.exe > nul
echo Stopping PHP FastCGI ...
taskkill/f/im php-cgi.exe > nul
Exit

Here the basic configuration is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.