Installation and configuration of nginx and PhP5 in Windows

Source: Internet
Author: User

Install PhP5
First, download the latest PHP 5.3 Windows version from the http://www.php.net/downloads.php, decompress to c: \ PhP5, the compressed package of PHP. ini-recommended, renamed PHP. ini, and then open the modify several options:

CopyCode The Code is as follows: error_reporting = e_all
Display_errors = on
Extension_dir = "C: \ PhP5 \ Ext"

; Dynamic expansion. You can remove the comments before extension as needed;
Such as loading PDO and MySQL
Extension = php_pdo.dll
Extension = php_pdo_mysql.dll

; CGI settings
CGI. fix_pathinfo = 1

Pay attention to dependencies when loading extensions in PHP. For example, php_exif.dll requires php_mbstring.dll. You must put php_mbstring.dll in front of php_exif.dll to load it successfully. Some extensions depend on additional DLL files, such as PHP 5.0 + and php_mysqli.dll depend on libmysql. dll, while php_oci8.dll requires you to install the Oracle 8 client. If you are not familiar with these dependencies, refer to the install.txt file in the installation package.

Search sequence of dependent files: first, the directory where php.exe is located. If it is in ISAPI mode, the startup location of the web server will be searched, such as the bin directory of Apache; the second is the directory in the Windows PATH environment variable. Do not copy any files to the Windows directory. If necessary, add c: \ PhP5 to the path to facilitate PHP upgrade in the future.

Install nginx
Starting from v0.7.52, nginx has been released for windows. You can download it from its official website:
Http://nginx.net

If you need an earlier version of nginx for Windows, you can find it on Kevin Worthington's website.

I used 0.8.29. After downloading it, decompress and release the file to c: \ nginx.

So how to configure nginx so that it can work with PHP?
Configure PHP FastCGI
Nginx needs to work with the FastCGI server to process requests. You can run the PHP FastCGI server in either of the following ways:

C:/PhP5/php-cgi.exe-B 127.0.0.1: 9000-C:/PhP5/PHP. ini

Another way is to use third-party tools such as PHP-FPM and CGI-fcgi. Apparently! It is extremely painful to use these tools in windows. You may need cygwin and other things. Some people have done so, although I think it is self-seeking.

Next, modify nginx and forward PHP requests to the PHP FastCGI Server:

Copy code The Code is 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: $ document_root refers to your PHP scripts root directory, which is set as your website root directory. In Windows, you must note the root path. It is best to use "/" as the path separator instead of the default "\" in windows. Otherwise, problems may occur. For example, this path: D: \ public_html \ test does not work. nginx throws a 500 error because \ t in \ test is parsed as a tab. Of course, you can add a backslash escape, for example, D: \ public_html \ test.

PHP. conf configuration file: Copy codeThe Code is as follows: # connect to port 9000 of the local machine. The port here refers to the port opened by the PHP FastCGI server,
# Be consistent with the port opened by the php-cgi.exe
# When nginx receives a PHP file request, it will automatically forward it to the PHP FastCGI server.
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;

# By default, nginx does not support CGI path_info, and the value of script_name is not standard (combining path_info)
# The following two lines of commands can be stripped from script_name to path_info
Fastcgi_split_path_info ^ (. + \. php) (. *) $;
Fastcgi_param path_info $ fastcgi_path_info;

Include fastcgi_params;

Create an independent PHP. conf file to save the configuration. It is purely for the sake of streamlining nginx. conf. You can also write it all in the main configuration file.

Modify PHP. ini and set CGI. fix_pathinfo = 1. This is very important. php will correct script_filename to the actual file address. Otherwise, PHP will not be able to find the PHP file to be processed.

For some other settings, the master server:

Copy code The Code is as follows: # Number of processes enabled 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 {
# Maximum number of connections processed by a process,
# For local development, the default 1024 value is not required. Here it is changed to 64
Worker_connections 64;
}

If the default index. php index.html or other homepage files do not exist under a directory, nginx will throw a 403 error. If you need to list this directory, you can go to HTTP {... }, Add the following command:Copy codeThe Code is as follows: Autoindex on;
Autoindex_exact_size on;
Autoindex_localtime on;

OK, integrated
Create start_nginx.bat to start both PHP FastCGI and nginx:Copy codeThe Code is as follows: @ echo off
Invalid in rem windows
Rem set php_fcgi_children = 5

Rem the maximum number of requests processed by each process, or set it to a Windows environment variable
Sets php_fcgi_max_requests = 1000

Echo starting PHP FastCGI...
Runhiddenconsole C:/PhP5/php-cgi.exe-B 127.0.0.1: 9000-C:/PhP5/PHP. ini

Echo starting nginx...
C:/nginx/nginx.exe

Runhiddenconsole.exe is a small window used to hide DoSProgram, Which can be downloaded here.
After start_nginx.bat is enabled, there will also be a DOS window, but it can be safely disabled, and does not close nginx and php-cgi.exe.

Similarly, stop_nginx.bat is used to disable:

Copy code The Code is 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

The basic configuration is complete.

Related Article

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.