Home php教程 php手册 windows下配置nginx+php环境

windows下配置nginx+php环境

Jun 14, 2016 am 12:01 AM
nginx php windows code Open source environment programming programming language software development Configuration

  刚看到nginx这个词,我很好奇它的读法(engine x),我的直译是“引擎x”,一般引“擎代”表了性能,而“x”大多出现是表示“xtras(额外的效果)”,那么整个词的意思就是类似“极致效果”,“额外性能”。当然这里不是要来唠嗑,以上是题外话。

  nginx相较于我们熟悉的apache、IIS的优势,就我浅入浅出的了解,在于“反向代理”和“负载均衡”。因此考虑到能够为Web服务器节省资源,它可以代替apache来提供Web服务。那么上正题了,nginx有这么多优势,那在windows下如何来配置nginx+php环境?网上看到还是那么多转载来转载去的文章。这里就我配置的过程,来介绍一下:

1、首先需要准备的应用程序包。

  nginx:nginx/Windows-1.0.4

  php:php-5.2.16-nts-Win32-VC6-x86.zip (nginx下php是以FastCGI的方式运行,所以我们下载非线程安全也就是nts的php包)

  (还会用到)RunHiddenConsole:RunHiddenConsole.zip

2、安装与配置。

 1)php的安装与配置。

  直接解压下载好的php包,到D盘wnmp目录(D:\wnmp),这里把解压出来的文件夹重命名成php5。进入文件夹修改php.ini-recommended文件为php.ini,并用Editplus或者Notepad++打开来。找到

<div>
<span style="color: #000000;">extension_dir </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">./ext</span><span style="color: #800000;">"</span>
</div>
Copy after login

更改为

<div>
<span style="color: #000000;">extension_dir </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">D:/wnmp/php5/ext</span><span style="color: #800000;">"</span>
</div>
Copy after login
往下看,再找到
<div>
<span style="color: #000000;">;extension</span><span style="color: #000000;">=</span><span style="color: #000000;">php_mysql.dll<br>;extension</span><span style="color: #000000;">=</span><span style="color: #000000;">php_mysqli.dll</span>
</div>
Copy after login

前面指定了php的ext路径后,只要把需要的扩展包前面所对应的“;”去掉,就可以了。这里打开php_mysql.dll和php_mysqli.dll,让php支持mysql。当然不要忘掉很重要的一步就是,把php5目录下的libmysql.dll文件复制到C:\Windows目录下,也可以在系统变量里面指定路径,当然这里我选择了更为方便的方法^_^。

到这里,php已经可以支持mysql了。

  接下来我们来配置php,让php能够与nginx结合。找到

<div>
<span style="color: #000000;">;cgi.fix_pathinfo</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span>
</div>
Copy after login

我们去掉这里的封号。

<div>
<span style="color: #000000;">cgi.fix_pathinfo</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span>
</div>
Copy after login
这一步非常重要,这里是php的CGI的设置。

 2)nginx的安装与配置。

  把下载好的nginx-1.0.4的包同样解压到D盘的wnmp目录下,并重命名为nginx。接下来,我们来配置nginx,让它能够和php协同工作。进入nginx的conf目录,打开nginx的配置文件nginx.conf,找到

<div>
<span style="color: #000000;">location </span><span style="color: #000000;">/</span><span style="color: #000000;"> {<br>      root   html;      #这里是站点的根目录<br>      index  index.html index.htm;<br>}</span>
</div>
Copy after login

root  html;改为root   D:/wnmp/www;

再往下,找到

<div>
<span style="color: #000000;"># pass the PHP scripts to FastCGI server listening on </span><span style="color: #800080;">127.0</span><span style="color: #000000;">.</span><span style="color: #800080;">0.1</span><span style="color: #000000;">:</span><span style="color: #800080;">9000</span><span style="color: #000000;"><br>#<br>#location </span><span style="color: #000000;">~</span><span style="color: #000000;"> \.php$ {<br>#    root           html;<br>#    fastcgi_pass   </span><span style="color: #800080;">127.0</span><span style="color: #000000;">.</span><span style="color: #800080;">0.1</span><span style="color: #000000;">:</span><span style="color: #800080;">9000</span><span style="color: #000000;">;<br>#    fastcgi_index  index.php;<br>#    fastcgi_param  SCRIPT_FILENAME  </span><span style="color: #ff0000;">/</span><span style="color: #000000;"><span style="color: #ff0000;">scripts</span>$fastcgi_script_name;<br>#    include        fastcgi_params;<br>#}</span>
</div>
Copy after login

先将前面的“#”去掉,同样将root  html;改为root   D:/wnmp/www;。再把标记为红色的/scripts改为“$document_root”,这里的“$document_root”就是指前面“root”所指的站点路径,这是改完后的:

<div>
<span style="color: #000000;">#</span><span style="color: #000000;"> pass the PHP scripts to FastCGI server listening </span><span style="color: #0000ff;">on</span><span style="color: #000000;"> </span><span style="color: #000000;">127.0</span><span style="color: #000000;">.</span><span style="color: #000000;">0.1</span><span style="color: #000000;">:</span><span style="color: #000000;">9000</span><span style="color: #000000;"><br></span><span style="color: #000000;">#</span><span style="color: #000000;"><br>location ~ </span><span style="color: #000000;">\.</span><span style="color: #000000;">php</span><span style="color: #000000;">$</span><span style="color: #000000;"> {<br>      root           <span style="color: #ff0000;">D:</span></span><span style="color: #ff0000;">/wnmp/www;</span><span style="color: #000000;"><br>      fastcgi_pass   </span><span style="color: #800080;">127.0.0.1<span style="color: #000000;">:</span>9000</span><span style="color: #000000;">;</span><span style="color: #000000;"><br>      fastcgi_index  index</span><span style="color: #000000;">.</span><span style="color: #000000;">php</span><span style="color: #000000;">;</span><span style="color: #000000;"><br>      fastcgi_param  SCRIPT_FILENAME  </span><span style="color: #ff0000;">$document_root</span><span style="color: #000000;">$</span><span style="color: #000000;">fastcgi_script_name</span><span style="color: #000000;">;</span><span style="color: #000000;"><br>      include        fastcgi_params</span><span style="color: #000000;">;</span><span style="color: #000000;"><br>}</span>
</div>
Copy after login

保存配置文件,就可以了。

  nginx+php的环境就初步配置好了,来跑跑看。我们可以输入命令 windows下配置nginx+php环境

来启动php,并手动启动nginx,当然也可以利用脚本来实现。

  首先把下载好的RunHiddenConsole.zip包解压到nginx目录内,RunHiddenConsole.exe的作用是在执行完命令行脚本后可以自动关闭脚本,而从脚本中开启的进程不被关闭。然后来创建脚本,命名为“start_nginx.bat”,我们在Notepad++里来编辑它

<div>
<span style="color: #0000ff;">@echo</span><span style="color: #000000;"> </span><span style="color: #0000ff;">off</span><span style="color: #000000;"><br></span><span style="color: #008000;">REM</span><span style="color: #008000;"> Windows 下无效</span><span style="color: #008000;"><br>REM</span><span style="color: #008000;"> set PHP_FCGI_CHILDREN=5</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #008000;">REM</span><span style="color: #008000;"> 每个进程处理的最大请求数,或设置为 Windows 环境变量</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">set</span><span style="color: #000000;"> PHP_FCGI_MAX_REQUESTS</span><span style="color: #000000;">=</span><span style="color: #000000;">1000</span><span style="color: #000000;"><br> <br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> Starting PHP FastCGI</span><span style="color: #000000;">...</span><span style="color: #000000;"><br>RunHiddenConsole D:</span><span style="color: #000000;">/</span><span style="color: #000000;">wnmp</span><span style="color: #000000;">/</span><span style="color: #000000;">php5</span><span style="color: #000000;">/</span><span style="color: #000000;">php-cgi</span><span style="color: #000000;">.</span><span style="color: #000000;">exe -b </span><span style="color: #000000;">127.0</span><span style="color: #000000;">.</span><span style="color: #000000;">0.1</span><span style="color: #000000;">:</span><span style="color: #000000;">9000</span><span style="color: #000000;"> -c D:</span><span style="color: #000000;">/</span><span style="color: #000000;">wnmp</span><span style="color: #000000;">/</span><span style="color: #000000;">php5</span><span style="color: #000000;">/</span><span style="color: #000000;">php</span><span style="color: #000000;">.</span><span style="color: #000000;">ini<br> <br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> Starting nginx</span><span style="color: #000000;">...</span><span style="color: #000000;"><br>RunHiddenConsole D:</span><span style="color: #000000;">/</span><span style="color: #000000;">wnmp</span><span style="color: #000000;">/</span><span style="color: #000000;">nginx</span><span style="color: #000000;">/</span><span style="color: #000000;">nginx</span><span style="color: #000000;">.</span><span style="color: #000000;">exe -p D:</span><span style="color: #000000;">/</span><span style="color: #000000;">wnmp</span><span style="color: #000000;">/</span><span style="color: #000000;">nginx</span>
</div>
Copy after login

再另外创建一个名为stop_nginx.bat的脚本用来关闭nginx

<div>
<span style="color: #0000ff;">@echo</span><span style="color: #000000;"> </span><span style="color: #0000ff;">off</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> Stopping nginx</span><span style="color: #000000;">...</span><span style="color: #000000;"> </span><span style="color: #000000;"> </span><span style="color: #000000;"><br>taskkill </span><span style="color: #000000;">/</span><span style="color: #000000;">F </span><span style="color: #000000;">/</span><span style="color: #000000;">IM nginx</span><span style="color: #000000;">.</span><span style="color: #000000;">exe </span><span style="color: #000000;">></span><span style="color: #000000;"> nul</span><span style="color: #000000;"></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> Stopping PHP FastCGI</span><span style="color: #000000;">...</span><span style="color: #000000;"><br>taskkill </span><span style="color: #000000;">/</span><span style="color: #000000;">F </span><span style="color: #000000;">/</span><span style="color: #000000;">IM php-cgi</span><span style="color: #000000;">.</span><span style="color: #000000;">exe </span><span style="color: #000000;">></span><span style="color: #000000;"> nul<br></span><span style="color: #0000ff;">exit</span>
</div>
Copy after login

做好后,是这样的

windows下配置nginx+php环境

这样,我们的服务脚本也都创建完毕了。双击start_nginx.bat看看进程管理器是不是有两个nginx.exe的进程和一个php-cgi.exe的进程呢?

windows下配置nginx+php环境

这样nginx服务就启动了,而且php也以fastCGI的方式运行了。

到站点目录下,新建一个phpinfo.php的文件,在里面编辑

<div>
<span style="color: #000000;"></span><span style="color: #000000;">php<br>    </span><span style="color: #008080;">phpinfo</span><span style="color: #000000;">();<br></span><span style="color: #000000;">?></span>
</div>
Copy after login

保存后,打开浏览器输入“http://localhost/phpinfo.php”,如果看到

windows下配置nginx+php环境

就说明,nginx+php的环境已经配置好了,呵呵~

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

How to handle high DPI display in C? How to handle high DPI display in C? Apr 28, 2025 pm 09:57 PM

Handling high DPI display in C can be achieved through the following steps: 1) Understand DPI and scaling, use the operating system API to obtain DPI information and adjust the graphics output; 2) Handle cross-platform compatibility, use cross-platform graphics libraries such as SDL or Qt; 3) Perform performance optimization, improve performance through cache, hardware acceleration, and dynamic adjustment of the details level; 4) Solve common problems, such as blurred text and interface elements are too small, and solve by correctly applying DPI scaling.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

What are the cross-platform development environments in C? What are the cross-platform development environments in C? Apr 28, 2025 pm 09:24 PM

Cross-platform development in C is recommended to use VisualStudioCode, CLion and QtCreator. 1. VisualStudioCode is lightweight and flexible, suitable for multi-platform development, but the initial configuration is more complicated. 2. CLion integrates CMake, suitable for cross-platform projects, but licenses are expensive. 3.QtCreator supports cross-platform development, with built-in Qt library, but the learning curve is steep.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

See all articles