_php tips for configuring nginx+php environments in Windows platforms

Source: Internet
Author: User
Tags phpinfo vc9 web services win32

Having just seen the word nginx, I am curious about its reading method (engine x), my literal translation is "engine X", the general introduction of the "Chi" Table performance, and "X" most of the appearance is to indicate "Xtras (additional effect)", then the meaning of the whole word is similar to "extreme effect", "extra performance." Of course, this is not to come to chit chat when, above is the topic of digression.

Nginx compared to the advantages of our familiar apache,iis, my shallow understanding of shallow, lies in "reverse proxy" and "Load balance". So given the ability to save resources for a Web server, it can provide Web services instead of Apache. So on the point, Nginx has so many advantages, how to configure the nginx+php environment under Windows? Online to see or so many reprinted to reprint the article. Here is the process of my configuration, to introduce:

1, first need to prepare the application package.

nginx:nginx/windows-1.0.4
Php:php-5.2.16-nts-win32-vc6-x86.zip
RunHiddenConsole:RunHiddenConsole.zip
Nginx PHP is run fastcgi, so we download the NTS PHP package, which is not thread safe.

2, installation and configuration.

1 installation and configuration of PHP.
Download the good PHP package directly, to D disk Wnmp directory (D:\WNMP), where the extracted folder renamed to PHP5. Enter the folder to modify the php.ini-recommended file to php.ini and open it with EditPlus or notepad++. Found it

Extension_dir = "./ext"

Change to

Extension_dir = "D:/wnmp/php5/ext"

Look down, then find

; Extension=php_mysql.dll;extension=php_mysqli.dll

In practice, you may want to open

Extension=php_bz2.dll
extension=php_curl.dll
extension=php_gd2.dll
extension=php_mbstring.dll
Extension=php_exif.dll
extension=php_mcrypt.dll
extension=php_mhash.dll
Extension=php_msql.dll
extension=php_mssql.dll
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_ Openssl.dll
extension=php_pdo.dll
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
Extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
extension=php_snmp.dll
extension=php_ Sockets.dll
extension=php_sqlite.dll
extension=php_xsl.dll
Extension=php_zip.dll

The previous specified PHP ext path, as long as the need to extend the package in front of the corresponding ";" get rid of it. Here, open Php_mysql.dll and Php_mysqli.dll, and let PHP support MySQL.

Of course, don't forget. A very important step is to copy the Libmysql.dll file in the PHP5 directory to the C:\Windows directory, you can also specify the path in the system variables, of course, I chose a more convenient method ^_^. (Test found that it is also possible to not replicate)

Here, PHP can already support MySQL.

Next we're going to configure PHP so that PHP can be combined with nginx. Found it

; cgi.fix_pathinfo=1

We remove the semicolon here:

Cgi.fix_pathinfo=1

This step is very important, here is the PHP CGI settings.

2) Installation and configuration of Nginx

Also extract the downloaded nginx-1.0.4 package into the D-disk WNMP directory and rename it to Nginx. Next, we'll configure Nginx to work with PHP. Enter Nginx's conf directory, open nginx configuration file nginx.conf, find

Location/{
  root html; #这里是站点的根目录 index index.html
  index.htm
}

Change root HTML to root d:/wnmp/www; plus index.php, that is:

Location/{
  root d:/wnmp/www;  #这里是站点的根目录 
  index index.php index.html index.htm;
}

Note here that the path delimiter should be used/instead of using Windows \ To avoid ambiguity.
Go down and find

# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root HTML;
# Fastcgi_pass 127.0.0.1:9000;
# Fastcgi_index index.php;
# Fastcgi_param Script_filename/scripts$fastcgi_script_name;
# include Fastcgi_params;
#}

Remove the front # First, and also change root HTML to root d:/wnmp/www. Then the marked red/scripts to $document_root, where the $document_root refers to the previous root refers to the site path, which is changed after:

# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
Location ~ \.php$ {
  root d:/wnmp/www ;
  Fastcgi_pass 127.0.0.1:9000;
  Fastcgi_index index.php;
  Fastcgi_param script_filename $document _root$fastcgi_script_name; 
  Include Fastcgi_params;
}

Set Error.log storage directory, will #error_log Logs/error.log, the default error.log is stored in the Nginx directory logs installation directory.

Save the configuration file and it's OK.

nginx+php environment on the preliminary configuration well, to run and see. We can enter the command:

Php-cgi.exe-b 127.0.0.1:9000-c D:/wnmp/www/php/php.ini

To start PHP and manually start the Nginx (which may not be a good line). Of course, you can also use scripts to implement.

First of all, the download of the Runhiddenconsole.zip package into the Nginx directory, RunHiddenConsole.exe is the role of the command line after the execution of the script can automatically close the script, and the process opened from the script is not closed. Then to create the script, named Start_nginx.bat, we edit it in notepad++:

@echo
off rem windows invalid
rem set php_fcgi_children=5

REM The maximum number of requests processed per process, or set to Windows environment variable
set Php_fcgi_ max_requests=1000
 
Echo starting PHP FastCGI
... Runhiddenconsole d:/phpsetup/php-5.4.45-nts-win32-vc9-x86/php-cgi.exe-b 127.0.0.1:9000-c D:/phpsetup/ Php-5.4.45-nts-win32-vc9-x86/php.ini
 
Echo starting Nginx
... Runhiddenconsole d:/phpsetup/nginx-1.6.0/nginx.exe-p d:/phpsetup/nginx-1.6.0

Create a different script named Stop_nginx.bat to turn off the Nginx:

@echo off
echo stopping Nginx 
... taskkill/f/im nginx.exe > Nul
echo stopping PHP FastCGI ...
taskkill/f/im php-cgi.exe > Nul
exit

In this way, our service scripts are also created. Double-click Start_nginx.bat and see if the process manager has two nginx.exe processes and a php-cgi.exe process.

The Nginx service starts, and PHP runs in a fastcgi way.
To the Site directory, create a new phpinfo.php file, edit it inside

<?php
phpinfo ();
? >

After saving, open the browser input http://localhost/phpinfo.php, if you see

On the description, nginx+php environment has been configured well, hehe ~

The solution of Nginx 403 Forbidden

Common, causes the Nginx 403 forbidden to have two kinds of reasons, first is the missing index file, two permissions question.

1, missing index.html or index.php files

The code is as follows:

server { 
 listen; 
 server_name localhost; 
 Index index.php index.html; 
 root/var/www;
}

If there is no index.php,index.html under the/var/www, direct access to the domain name, can not find the file, will be reported 403 forbidden.
For example: You access www.test.com and this domain name, corresponding to the root specified by the index file does not exist.

Permission Issues

For PHP, this error can result if the Nginx user does not have permission to the Web directory.
Workaround: Modify the Web directory's read and write permissions, or change the Nginx user to the directory of the user, a restart can be resolved. Such as:
The code is as follows (Linux):

Chown-r Nginx_user:nginx_user/htdocs

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.