欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

windows 下 nginx nginx-1.29.2.zip 下载下来后,如何运行 如何安装 nginx AG chatgpt 有大用 有大大用 有大大大用

nginx 下载见   /node-admin/23294

在 Windows 下运行 Nginx(以 nginx-1.29.2.zip 为例)非常简单,以下是详细步骤:


1. 解压 Nginx.zip

将下载的 nginx-1.29.2.zip 解压至任意目录(如 C:\nginx),确保路径不含中文或空格。
解压后目录结构如下:

C:\nginx
    ├── conf\       # 配置文件目录(nginx.conf)
    ├── html\       # 默认网站根目录(index.html)
    ├── logs\       # 日志文件目录
    └── nginx.exe   # 主程序




2. 启动 Nginx

方法 1:直接运行(推荐)

  • 打开 CMD(管理员权限),进入 nginx.exe 所在目录:

    cmd            
    cd C:\nginx            

           

       

  • 启动 Nginx

    cmd            
    start nginx            

           

           

    不会有任何提示,但 Nginx 已在后台运行。

方法 2:通过 PowerShell

powershell    
# 进入 Nginx 目录 cd C:\nginx # 启动 .\nginx.exe    



注意:避免直接双击 nginx.exe,否则关闭 CMD 窗口后 Nginx 会退出。


3. 验证 Nginx 是否运行

  1. 查看进程

  • 打开任务管理器 → 详细信息 → 确认 nginx.exe 进程存在(通常会有 1 个主进程 + 1 个工作进程)。

  • 或用命令检查:

    cmd                
    tasklist /fi "imagename eq nginx.exe"                

               

           

  • 访问默认页面

    • 浏览器打开 http://localhost,如果看到 Welcome to nginx! 页面,说明运行成功。


    4. 其他常用命令

    命令说明
    nginx -s stop            快速关闭(强制终止)
    nginx -s quit            优雅关闭(处理完当前请求后退出)
    nginx -s reload            重新加载配置(修改 nginx.conf 后生效)
    nginx -t            测试配置文件语法(验证配置是否正确)

    注意:所有命令需在 nginx.exe 所在目录执行,或指定完整路径(如 C:\nginx\nginx -s reload)。


    5. 关闭 Nginx

    • 方法 1(推荐)

      cmd            
      nginx -s quit            

             

         

    • 方法 2(强制终止)

      cmd            
      nginx -s stop            

             

             

      或直接在任务管理器中结束 nginx.exe 进程。


    6. 注册为 Windows 服务(可选)

    如需开机自启,可用 winsw 等工具将 Nginx 注册为服务。
    参考步骤:

    1. 下载 WinSW        https://github.com/winsw/winsw/releases  

    2. image.png

    3. 并命名为 nginx-service.exe

    4. 创建 nginx-service.xml 配置文件:

      xml            
      <service>  <id>nginx</id>  <name>Nginx</name>  <description>Nginx HTTP Server</description>  <executable>C:\nginx\nginx.exe</executable>  <logpath>C:\nginx\logs</logpath>  <startargument>-p C:\nginx</startargument> </service>            

             

         

    5. 以管理员身份运行:

      cmd            
      nginx-service.exe install nginx-service.exe start            

             

         


    常见问题

    • 端口占用(默认 80 端口被占用): 修改 conf/nginx.conf 中的 listen 80; 为其他端口(如 8080)。

    • 启动失败: 检查 logs/error.log 中的错误信息。


    按照上述步骤操作后,Nginx 即可在 Windows 下正常运行。如需进一步配置(如虚拟主机),可修改 conf/nginx.conf 文件。


    php 代码 写在哪个目录

    在 Nginx 环境下,PHP 代码应当存放在特定的目录中,以便 Nginx 能正确解析 PHP 文件并通过 PHP-FPM(FastCGI 进程管理器)处理。以下是详细说明:


    1. PHP 代码存放目录

    默认情况下,Nginx 的网站根目录是:

    • Windows<nginx安装目录>/html/
      (如 C:\nginx\html\

    • Linux/usr/share/nginx/html/ 或 /var/www/html/

    你可以在以下目录存放 PHP 文件(如 index.php):

    nginx
        ├── html
        │   ├── index.php       # PHP 主文件
        │   └── subdir          # 子目录也可以存放 PHP 代码
        │       └── test.php
        ├── conf
        ├── logs
        └── nginx.exe
    

    示例 index.php

    php
    <?php phpinfo(); ?>

    2. 如何修改 Nginx 的网站根目录(可选)

    如果你希望 PHP 代码存放在其他目录(如 C:\my_php_project),需要修改 nginx.conf

    1. 打开 conf/nginx.conf 文件。

    2. 找到 server 块,修改 root 指向你的目录:

      nginx
      server {    listen       80;  # 默认 HTTP 端口    server_name  localhost;    # 修改此处为你的 PHP 代码目录    root   C:/my_php_project;  # Windows 路径用 / 或转义 \\    index  index.php index.html index.htm;    location / {        try_files $uri $uri/ =404;    }    # 配置 PHP 解析    location ~ \.php$ {        fastcgi_pass   127.0.0.1:9000;  # PHP-FPM 默认端口        fastcgi_index  index.php;        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        include        fastcgi_params;    } }
    3. 重新加载 Nginx:

      cmd
      nginx -s reload

    3. 配置 PHP-FPM(必需)

    Nginx 本身不解析 PHP,需搭配 PHP-FPM(PHP FastCGI 进程管理器):

    1. 下载 PHP

    • 从 PHP 官网 下载 Windows 版(选择 Thread Safe 版本,如 php-8.3.x-nts-Win32-vs16-x64.zip)。

  • 配置 PHP-FPM

    • 解压 PHP 到目录(如 C:\php)。

    • 复制 php.ini-development 为 php.ini 并修改:

      ini
      extension_dir = "ext" cgi.fix_pathinfo = 1
    • 启动 php-cgi.exe(用于 PHP-FPM):

      cmd
      php-cgi.exe -b 127.0.0.1:9000 -c C:\php\php.ini
  • 验证

    • 在 html 目录创建 test.php

      php
      <?php echo "Hello, PHP!"; ?>
    • 访问 http://localhost/test.php,看到输出即成功。


    4. 常见问题排查

    • 403 Forbidden:检查目录权限或 root 路径是否正确。

    • 502 Bad Gateway:PHP-FPM 未运行,确认 fastcgi_pass 地址(如 127.0.0.1:9000)与 PHP-FPM 端口一致。

    • 空白页面:PHP 代码错误,检查 php.ini 的 display_errors = On 并查看 logs/error.log


    总结

    关键点说明
    PHP 目录html/ 或自定义目录(需修改 nginx.conf
    PHP 解析依赖 PHP-FPM(fastcgi_pass 127.0.0.1:9000
    调试检查 logs/error.log 和 PHP-FPM 状态

    如果仍有问题,请提供具体的错误日志(logs/error.log)继续分析! ?



    普通分类: