Home Backend Development PHP Tutorial windows平台中配置nginx+php环境_PHP

windows平台中配置nginx+php环境_PHP

May 29, 2016 am 11:47 AM
windows

刚看到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
RunHiddenConsole:RunHiddenConsole.zip
nginx下php是以FastCGI的方式运行,所以我们下载非线程安全也就是nts的php包。

2、安装与配置。

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

extension_dir = "./ext"
Copy after login

更改为

extension_dir = "D:/wnmp/php5/ext"
Copy after login

往下看,再找到

;extension=php_mysql.dll;extension=php_mysqli.dll
Copy after login

实际中,可能还要开启

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
Copy after login

前面指定了php的ext路径后,只要把需要的扩展包前面所对应的“;”去掉,就可以了。这里打开php_mysql.dll和php_mysqli.dll,让php支持mysql。

当然不要忘掉很重要的一步就是,把php5目录下的libmysql.dll文件复制到C:\Windows目录下,也可以在系统变量里面指定路径,当然这里我选择了更为方便的方法^_^。(测试发现不复制也是可以的)

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

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

;cgi.fix_pathinfo=1
Copy after login

我们去掉这里的分号:

cgi.fix_pathinfo=1
Copy after login

这一步非常重要,这里是php的CGI的设置。

2)nginx的安装与配置

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

location / {
  root html;   #这里是站点的根目录 index index.html
  index.htm;
}
Copy after login

将root html;改为root D:/wnmp/www;加上index.php,即:

location / {
  root D:/wnmp/www;  #这里是站点的根目录 
  index index.php index.html index.htm;
}
Copy after login

这里需要注意,路径分隔符请使用/而不要使用Windows中的\以防歧义。
再往下,找到

# 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;
#}
Copy after login

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

# 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;
}
Copy after login

设定error.log的存放目录,将#error_log logs/error.log;的#去处,默认error.log是存放在Nginx安装目录中logs目录下。

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

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

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

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

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

@echo off
REM Windows 下无效
REM set PHP_FCGI_CHILDREN=5

REM 每个进程处理的最大请求数,或设置为 Windows 环境变量
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

Copy after login

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

@echo off
echo Stopping nginx... 
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit
Copy after login

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

这样nginx服务就启动了,而且php也以fastCGI的方式运行了。
到站点目录下,新建一个phpinfo.php的文件,在里面编辑

<&#63;php
phpinfo();
&#63;>
Copy after login

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

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

Nginx 403 forbidden的解决办法

常见的,引起nginx 403 forbidden有二种原因,一是缺少索引文件,二权限问题。

1、缺少index.html或者index.php文件

代码如下:

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

如果在/var/www下面没有index.php,index.html的时候,直接访问域名,找不到文件,会报403 forbidden。
例如:你访问www.test.com而这个域名,对应的root指定的索引文件不存在。

权限问题

对于PHP而言,如果nginx用户没有web目录的权限,则会导致该错误。
解决办法:修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重起一下就能解决。 如:
代码如下(Linux下):

chown -R nginx_user:nginx_user /htdocs

Copy after login

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 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
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
How to copy and paste mysql How to copy and paste mysql Apr 08, 2025 pm 07:18 PM

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! Apr 17, 2025 pm 09:54 PM

In Laravel development, dealing with complex model relationships has always been a challenge, especially when it comes to multi-level BelongsToThrough relationships. Recently, I encountered this problem in a project dealing with a multi-level model relationship, where traditional HasManyThrough relationships fail to meet the needs, resulting in data queries becoming complex and inefficient. After some exploration, I found the library staudenmeir/belongs-to-through, which easily installed and solved my troubles through Composer.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Recommended system maintenance and optimization tools in Mac system Recommended system maintenance and optimization tools in Mac system Apr 12, 2025 pm 04:45 PM

Mac system maintenance includes: disk management (use OmniDiskSweeper to clean disk space, use disk tools to check disk errors) memory management (use Activity Monitor to monitor memory usage, end over-occupying processes) startup item management (use Linc or LaunchControl to manage startup items, disable unnecessary startup items) system cache cleaning (use CleanMyMac X or manually clean system cache) software update (timely update system and applications) regular backup (use Time Machine to backup data regularly) good usage habits (not installing applications excessively, cleaning files regularly, and monitoring system logs)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles