windows nginx安装 php

寻技术 PHP编程 / Windows 2024年01月19日 54

在搭建 web 服务器时,Nginx 是一款高性能的 web 服务器,而 PHP 是一种常用的 web 开发语言,当我们想把 Nginx 将 PHP 集成在一起使用时,就需要对它们进行安装和配置。本文将介绍在 Windows 环境下安装和配置 Nginx 和 PHP 的详细过程。

安装 Nginx

  1. 下载 Nginx

首先,我们需要在 Nginx 的官网(https://nginx.org/en/download.html)上下载最新的 Windows 版本,选择 32 位或 64 位的版本,再将其解压到一个自定义的目录。例如,可以将 Nginx 解压到 C:
ginx。

  1. 配置 Nginx

接着,我们需要在 Nginx 目录下创建一个 conf 目录,然后在 conf 目录下创建一个 nginx.conf 文件。在 nginx.conf 文件中,输入以下内容:

worker_processes  1;

error_log  logs/error.log;
pid        logs/nginx.pid;

events {
 worker_connections  1024;
}

http {
 include       mime.types;
 default_type  application/octet-stream;

 sendfile        on;
 keepalive_timeout  65;

 server {
   listen       80;
   server_name  localhost;
   root         html;
   index        index.php index.html index.htm;

   location / {
     try_files $uri $uri/ /index.php?$query_string;
   }

   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
     root   html;
   }

   location ~ .php$ {
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     include        fastcgi_params;
   }
 }
}

安装 PHP

  1. 下载 PHP

首先,我们需要在 PHP 的官网(https://windows.php.net/download/)上下载最新的 Windows 版本,选择 32 位或 64 位的版本,然后将其解压到一个自定义的目录。例如,可以将 PHP 解压到 C:php。

  1. 配置 PHP

为了让 PHP 与 Nginx 集成,我们需要在 Nginx 中添加 FastCGI 模块,使 Nginx 可以将 php 的请求转发给 PHP。为此,我们需要在 Nginx 的 conf 目录下的 nginx.conf 中添加以下配置:

location ~ .php$ {
 fastcgi_pass   127.0.0.1:9000;
 fastcgi_index  index.php;
 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 include        fastcgi_params;
}

其中,-b 参数表示 FastCGI 监听的 IP 地址和端口号,-c 参数表示 PHP 的配置文件路径。将保存的命令复制,打开命令行窗口,进入 PHP 目录,并输入命令启动 PHP。

  1. 测试 PHP

完成以上设置后,我们可以测试 PHP 是否正常工作。打开 Nginx 所在的目录,依次打开 conf、html 文件夹,将一个 test.php 文件复制到该文件夹中。打开浏览器,在地址栏输入 localhost/test.php,如果能够正常返回一些 PHP 信息,那么就说明我们已经成功将 PHP 集成到了 Nginx 中。

总结

通过以上步骤,我们已经成功将 Nginx 和 PHP 安装并集成在了一起。如果是一次全新的安装,需要注意在配置过程中遇到问题时及时查询、查看并解决问题。这两个工具能够为我们创建出一个完整的 Web 服务器,从而提供更好的 web 开发体验。对于扩展我们的开发能力来说,这是一个不错的选择。

关闭

用微信“扫一扫”