nginx+php配置

一、下载文档

1.下载php: https://windows.php.net/download/https://windows.php.net/download/ (注意,需下载正确的文件,最好下载Thread Safe安全版,我就是因为下载错文件,绕了好久。

 2.下载nginx: nginx: downloadhttp://nginx.org/en/download.html

二、配置nginx

修改Nginx的conf文件,Nginx的配置文件默认位置为:/etc/nginx/nginx.conf

1. 去掉worker_processes前的#号,开启一个进程

2. 添加events

3. 设置http->设置server->支持php

...
worker_processes  1;
...
events {
    worker_connections  1024;
}
....
server {
        listen       801;
        server_name  localhost;
        ...

      location / {
            root   html;
            index  index.html index.htm  index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        location ~ \.php$ {
            root      html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
          fastcgi_param     SCRIPT_FILENAME    $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
    }

4.测试nginx是否安装成功  
启动命令:start nginx 
在浏览器输入:http://localhost:801/,就会打开html下的index.html文件
如果不能正常打开,查看端口号是否被占用

三、配置php

复制php.ini-development,命名为“php.ini",开始进行配置

1.查找doc_root,配置需访问的源码目录
 

doc_root ="D:/.../nginx-1.16.1/html"

2.查找extension_dir,配置目录

extension_dir = "D:/.../php-8.0.12/ext"

3.查找cgi.fix_pathinfo,设置为0
搜索“php_mysql”,找到:”extension=php_mysql.dll和extension=php_mysqli.dll  去掉前面的“;”extension=php_mysql.dll和extension=php_mysqli.dll   (支持MYSQL数据库)

四、配置完成后,在cmd输入“php-cgi.exe -b 127.0.0.1:9000” 命令,确保“127.0.0.1:9000“的地址与nginx.conf中的 location ~ \.php$ 中的地址一样。如果cmd窗口关闭的话,服务关闭,就不能正常访问php页面,如果不需要cmd窗口,让服务在后台运行,可进行第五步骤

五:后台启动服务

1.在php目录下新建文件php-cgi.vbs,用php-cgi.vbs文件启动php-cgi:打开php-cgi.vbs,写入启动编码:

set wscriptObj = CreateObject("Wscript.Shell")
wscriptObj.run "php-cgi -b 127.0.0.1:9000",0

2.在Nginx目录下新建启动项:runServer.bat和停止项stopServer.bat
在启动项runServer.bat中输入:

@echo off
echo Starting nginx...
cd %~dp0nginx
start "" "D:/.../nginx-1.16.1/nginx.exe"

#echo Starting mysql...
#net start mysql

echo Starting PHP FastCGI...
cd %~dp0PHP
start "" "D:/.../php-8.0.12/php-cgi.vbs"

pause

Exit

 在停止项stopServer.bat中输入:

@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
echo Stopping mysql...
net stop mysql
pause
exit

附上nginx.conf配置


#user  nobody;
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 {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    

    server {
        listen       801;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

      location / {
            root   html;
            index  index.html index.htm  index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # 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    $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root  D:\software\nginx-1.16.1\html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


文章标签:

原文连接:https://blog.csdn.net/qq_39704803/article/details/121080517

相关推荐

极智开发 | 讲解 Nginx 特性之一:反向代理

编程技巧│超实用 nginx 中常见的配置合集

Linux下玩转nginx系列(七)---nginx如何实现限流功能

Nginx配置解决NetCore的跨域

无需CORS,用nginx解决跨域问题,轻松实现低代码开发的前后端分离

http协议之三次握手、四次挥手解析

前端应该会的nginx代理(windows)

Nginx越界读取缓存漏洞 CVE-2017-7529

极智开发 | ubuntu 安装 nginx

Nginx 简单入门指北

Nginx 实战核心知识点整理(上)

Linux-《Nginx-运维基础入门》实验报告

K8S 生态周报| Ingress NGINX 项目暂停接收新功能将专注于稳定性提升

nginx(二)服务器中配置超时时间的方法

nginx源码层面探究request_time、upstream_response_time、upstream_connect_time与upstream_header_time指标具体含义

Nginx原理性科普

nginx(一)nginx配置反向代理与负载均衡

【架构师(第四十四篇)】 服务端开发之 pm2 和 nginx 介绍

【Nginx】Windows平台下配置Nginx服务实现负载均衡

一次线上ngix的504 gateway timeout排查(真实案例)