Configure the nginx + php environment on windows

Source: Internet
Author: User
Tags vc9
Compared with linux, it is much easier to configure the web environment, whether it is a one-click installation package or independent configuration. this time, NGINX is used as the local web testing environment. I just saw the word nginx. I'm curious about its reading method (engine x). my literal translation is "engine x", which generally leads to the performance of the "primary generation" table, most of "x" indicates "xtras (extra effect)", so the entire word means "ultimate effect" and "extra performance ". Of course, this is not a question.

Nginx has the advantages of apache and IIS, which we are familiar with. I have learned a little about it in terms of "reverse proxy" and "server load balancer ". Therefore, because it can save resources for the Web server, it can provide Web services instead of apache. So now that nginx has so many advantages, how can I configure the nginx + php environment in windows? There are still so many reposted articles on the Internet. Here I will introduce the configuration process:

1. the application package to be prepared first.

Nginx: nginx/Windows-1.0.4
Php: php-5.2.16-nts-Win32-VC6-x86.zip
RunHiddenConsole: RunHiddenConsole.zip
In nginx, php runs in FastCGI mode. Therefore, we can download the php package for non-thread security, that is, the nts.

2. installation and configuration.

1) install and configure php.
Directly decompress the downloaded php package to the wnmp Directory (D: \ wnmp) on the D drive. here, rename the decompressed folder to php5. Go to the folder and modify the php. ini-recommended file to php. ini, and open it with Editplus or Notepad ++. Find

extension_dir = "./ext"

Change

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

Look down and find again

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

In practice, it may be necessary to enable

extension=php_bz2.dllextension=php_curl.dllextension=php_gd2.dllextension=php_mbstring.dllextension=php_exif.dllextension=php_mcrypt.dllextension=php_mhash.dllextension=php_msql.dllextension=php_mssql.dllextension=php_mysql.dllextension=php_mysqli.dllextension=php_openssl.dllextension=php_pdo.dllextension=php_pdo_mssql.dllextension=php_pdo_mysql.dllextension=php_pdo_pgsql.dllextension=php_pdo_sqlite.dllextension=php_snmp.dllextension=php_sockets.dllextension=php_sqlite.dllextension=php_xsl.dllextension=php_zip.dll

After specifying the php ext path, you only need to remove the ";" corresponding to the extension package. Open php_mysql.dll and php_mysqli.dll to allow php to support mysql.

Of course, do not forget the important step is to put libmysql under the php5 directory. copy the dll file to the C: \ Windows directory. you can also specify the path in the system variables. of course, here I chose a more convenient method ^_^. (The test finds that it is also possible not to copy)

Now, php supports mysql.

Next we will configure php so that php can be combined with nginx. Find

;cgi.fix_pathinfo=1

Remove the semicolon here:

cgi.fix_pathinfo=1

This step is very important. here is the CGI setting of php.

2) nginx installation and configuration

Decompress the downloaded nginx-1.0.4 package to the wnmp Directory of the D disk and rename it nginx. Next, Configure nginx to work with php. Go to the nginx conf Directory, open the nginx configuration file nginx. conf, and find

Location/{root html; # Here is the site's root directory index index.html index.htm ;}

Change root html; to root D:/wnmp/www; add index. php, that is:

Location/{root D:/wnmp/www; # Here is the index. php index.html index.htm ;}

Note that the path separator must be/instead of the \ in Windows to prevent ambiguity.
Next, 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;#}

First remove the previous # and change the root html; to root D:/wnmp/www ;. Change the/scripts marked in red to $ document_root. here, $ document_root refers to the site path indicated by the root, which is changed:

# 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 the directory where error. log is stored. set the # Location of # error_log logs/error. log;. by default, error. log is stored in the logs directory in the Nginx installation directory.

Save the configuration file.

The nginx + php environment has been initially configured. Run the following command:

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

To start php and manually start nginx (may not work ). Of course, you can also use a script to implement it.

First, decompress the downloaded runhiddenconsole.zippackage to the nginxdirectory. runhiddenconsole.exe automatically closes the script after executing the command line script, but the process enabled from the script is not closed. Create a script named start_nginx.bat. edit it in Notepad ++:

@ Echo offREM in Windows the REM set PHP_FCGI_CHILDREN is invalid = 5REM the maximum number of requests processed by each process, or set it to the 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 another script named stop_nginx.bat to disable nginx:

@echo offecho Stopping nginx... taskkill /F /IM nginx.exe > nulecho Stopping PHP FastCGI...taskkill /F /IM php-cgi.exe > nulexit

In this way, all our service scripts have been created. Double-click start_nginx.bat. check whether the process manager has two nginx.exeprocesses and a php-cgi.exe process?

In this way, the nginx service is started and php runs in fastCGI mode.
Go to the site directory, create a phpinfo. php file, and edit it in it.

<?phpphpinfo();?>

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

The nginx + php environment has been configured ~

Nginx 403 forbidden solution

Common causes of nginx 403 forbidden are: missing index files and permissions.

1. missing index.html or index. php files

The code is as follows:

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

If no index.php or index.html is available under/var/www, the system will report 403 forbidden if the domain name is directly accessed and the file cannot be found.
For example, the index file specified by root does not exist when you access www.test.com.

Permission problems

For PHP, this error occurs if the nginx user does not have the web Directory permission.
Solution: modify the read and write permissions of the web directory, or change the start user of nginx to the user of the Directory. For example:
The code is as follows (in 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.