Windows 10 Nginx + PHP
Windows 10 Nginx + PHP

Windows 10 Nginx + PHP

NGINX 可以通过 PHP 附带的 FastCGI 守护进程与 Windows 上的 PHP 交互:php-cgi.exe。

您需要运行 php-cgi.exe -b 127.0.0.1:9000 并在 NGINX 配置文件中使用 fastcgi_pass 127.0.0.1:9000

启动后,php-cgi.exe 将继续在命令提示符窗口中侦听连接,要隐藏该窗口,可以使用小型实用程序RunHiddenConsole

第一

  • 下载安装 Nginx,选择 Stable version (稳定版)
  • 下载安装 PHP,也可以使用xampp软件包中的php。

第二

  • 将 nginx 解压到“C:\nginx”
  • 将 php 解压到“C:\php”
  • 在“C”系统中创建文件夹“www”,“C:\www”

第三

  • 在“C:\nginx\conf\nginx.conf”编辑你的 nginx.conf
  • 更改根目录
    root c:/www;
  • 保存 nginx.conf
  • 打开你的 php 文件夹,找到 php-ini-development
  • 编辑你的 php-ini-development,修改相关配置,如下所示。
    extension_dir = "ext" 
    enable_dl = On 
    cgi.force_redirect = 1 
    fastcgi.impersonate = 1 
    cgi.rfc2616_headers = 1 
    extension=php_gd2.dll 
    extension=php_mbstring.dll 
    extension=php_exif.dll 
    extension=php_mysql.dll 
    extension=php_mysqli.dll 
    extension=php_pdo_mysql.dll 
    date.timezone = "Asia/Jakarta" 
  • 保存。
  • 再次打开你的 nginx.conf
  • 取消注释 php 配置并更改为此
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9999
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9999;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
  • 再次保存 nginx.conf。

第四

  • 设置php路径
  • 打开你的环境变量
    • 打开“系统属性”-》“高级”选项卡
    • 单击“环境变量…”
  • 在“系统变量”上编辑“Path”变量
  • 添加新路径“C:\php”

创建 nginx 启动、停止和重启 .bat

  • 先下载RunHiddenConsole,将RunHiddenConsole.exe移动到“C:\”。
  • 创建文件“nginx-start.bat”,在下面编辑文件,使您的 php-cgi.exe 端口与 nginx.conf 中的相同
    @ECHO OFF ECHO Starting PHP FastCGI... C:\RunHiddenConsole.exe C:\xampp\php\php-cgi.exe -b 127.0.0.1:9000 ECHO Starting NGINX start nginx.exe popd EXIT /b
  • 创建文件“nginx-stop.bat”,用这个编辑
    @ECHO OFF taskkill /f /IM nginx.exe taskkill /f /IM php-cgi.exe EXIT /b
  • 创建文件“nginx-restart.bat”
    @ECHO OFF call nginx-stop.bat call nginx-start.bat EXIT /b

测试

  • 双击“nginx-start.bat”
  • 如果一切正常,请打开浏览器,您可以看到这样
  • 在“C:\www”中创建类似“test-php”的文件夹
  • 创建文件“index.php”
    <?php phpinfo(); ?>
  • 打开浏览器并转到“localhost/test-php”
  • 如果一切正常,可以看到
  • 最后,nginx 和 php 现在可以正常使用了。

注意:
有时,如果使用 nginx-start.bat 运行,则 nginx & php 无法正常工作,需要手动运行 php;
打开终端,运行 php “php-cgi -b 127.0.0.1:9000″,这样 nginx & php正常运行

59条评论

  1. naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

  2. You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注