Home Backend Development PHP Tutorial Windows上Nginx+PHP5的安装与配置方法

Windows上Nginx+PHP5的安装与配置方法

Jun 13, 2016 pm 01:07 PM
error fastcgi nginx php windows

Windows下Nginx+PHP5的安装与配置方法

安装 PHP5
首先,从 http://www.php.net/downloads.php 下 载最新的 PHP5.2.9-2 Windows 版本,解压至 C:\php,把压缩包中的 php.ini-recommended,更名为 php.ini,然后打开修改几个选项:

error_reporting = E_ALL
display_errors = On
extension_dir = "C:\php\ext"

; 动态扩展,可以根据需要去掉 extension 前面的注释 ;
; 如加载 PDO, MySQL
extension=php_pdo.dll
extension=php_pdo_mysql.dll

; CGI 设置
cgi.force_redirect = 1
cgi.fix_pathinfo = 1
cgi.rfc2616_headers = 1

PHP 加载扩展需要注意依赖性,比如 php_exif.dll 需要 php_mbstring.dll,你必须要把 php_mbstring.dll 放在 php_exif.dll 前面才能加载成功。有些扩展依赖额外的 dll 文件,如 PHP 5.0+ ,php_mysqli.dll 依赖 libmysql.dll,而 php_oci8.dll,你则需要安装 Oracle 8 的客户端。如果你对这些依赖性不是太了解,可以参考一下安装包中的 install.txt 文件。

依赖文件的搜索顺序:首先是 php.exe 所在的目录,如果是 ISAPI 模式,那么会搜索 Web Server 的启动位置,比如 Apache 的 bin 目录;其次是 Windows PATH 环境变量中的目录。这里不要复 制任何文件到 Windows 目录中,有必要的话,可以把 C:\php5 加到 PATH 中,有利于以后 PHP 的升级。

安装 Nginx
从 v0.7.52 开始,Nginx 开始发布 Windows 版本的 Nginx,你可以在其官方网站上面下载:http://nginx.net
我使用的是 0.8.37,下载好以后,解压释放文件到 D:\nginx。

配置 PHP FastCGI
Nginx 需要和 FastCGI Server 配合才能处理请求,有两种方式运行 PHP FastCGI Server,一种就是使用 PHP 内置的 FastCGI 管理器:
命令行下面执行c:/php/php-cgi.exe -b 127.0.0.1:9000 -c c:/php/php.ini以启动PHP FastCGI

修改 Nginx 的配置文件 d\nginx\conf\nginx.conf,找到 php 相关的部分,修改如下:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root d:/public_html;
include php.conf;
}
root 也就是 $document_root 指的是你的 php scripts 根目录,设置为你的网站根目录。在 Windows 下,需要注意的是 root 的路径,最好使用 "/" 作为路径分隔符,而不是 Windows 默认的 "\",否则容易出问题,比如,这个路径:d:\public_html\test,就不会起作用,Nginx 会抛出 500 错误,原因是 \test 中 \t 被解析为制表符。当然再加上一个反斜杠转义也是可以的,如:d:\\public_html\\test。

php.conf 是我自己创建的用来保存 php 配置的文件,其实里面只有 3 行命令:
# 连接到本机 9000 端口,这里的端口是指 PHP FastCGI Server 开启的端口,
# 请与 php-cgi.exe 开启的端口保持一致
# 当 Nginx 收到 php 文件的请求时,会自动转发到 PHP FastCGI Server
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
之所以要创建一个独立的 php.conf 保存配置为了精简 nginx.conf,当在 nginx 中配置多个虚拟主机时,每个虚拟主机都需要配置 php,那么主配置文件就会变得重复、臃肿。

需要修改一下 d:\nginx\conf\fastcgi_params 文件,加入一行:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

并且修改 php.ini,设置 cgi.fix_pathinfo = 1,这非常重要,否则 PHP 将无法找到需要处理的 php 脚本。

一些其他的设置,主服务器:
# 默认开启的进程数
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 {
# 一个进程所处理的最大连接数上限,
# 本地开发,不需要默认的 1024,这里改为 64
worker_connections 64;
}

当某个目录下面不存在默认 index.php index.html 等首页文件时,Nginx 会抛出 403 ERROR,如果你需要罗列此目录,则可以在 http {… } 中加入如下命令:
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;

start_nginx.bat,用于同时启动 PHP FastCGI 和 Nginx:
@echo off
echo Starting PHP FastCGI...
RunHiddenConsole c:/php/php-cgi.exe -b 127.0.0.1:9000 -c c:/php/php.ini
echo Starting nginx...
d:/nginx/nginx.exe
RunHiddenConsole.exe 是一个用来隐藏 DOS 窗口的小程序,可以在这里下载RunHiddenConsole.zip (1.01 kb)。
start_nginx.bat 开启后,也会有 DOS 窗口,但是可以安全的关掉,并不会关闭 Nginx 和 php-cgi.exe。
stop_nginx.bat,用来关闭:
@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit
到这里基本配置完毕了。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated? How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated? Feb 21, 2025 pm 10:54 PM

The way to update ByBit exchanges varies by platform and device: Mobile: Check for updates and install in the app store. Desktop Client: Check for updates in the Help menu and install automatically. Web page: You need to manually access the official website for updates. Failure to update the exchange can lead to security vulnerabilities, functional limitations, compatibility issues and reduced transaction execution efficiency.

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Coinsuper exchange software channel official website entrance Coinsuper exchange software channel official website entrance Feb 21, 2025 pm 10:39 PM

The official website entrance of the Coinsuper Exchange: https://www.coinsuper.com. The client download channels are: Windows client, macOS client, and mobile (iOS/Android). Registration requires an email, mobile phone number and password, and you need to complete real-name authentication before you can trade. The platform provides a variety of digital asset transactions, including Bitcoin, Ethereum, etc., with the transaction fee rate of 0.1% for both orders and acceptors. Security safeguards include cold wallet storage, dual-factor verification, anti-money laundering and anti-terrorism financing measures, and with security public

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

See all articles