WNMP(Window系统+Nginx服务器+Mysql数据库+PHP)


PHP环境安装配置:

从官网下载PHP压缩包后,解压到指定目录,在PHP文件目录下将php.ini-development改名为php.ini

然后修改php.ini中的配置如下:

指定扩展路径,根据自己实际目录配置

extension_dir = "C:\Program Files\PHP\php\php-7.3.10-Win32-VC15-x64\ext" 

配合nginx相关的CGI配置

enable_dl = On
cgi.force_redirect = 0
cgi.fix_pathinfo=1
fastcgi.impersonate = 1
cgi.rfc2616_headers = 1

其他相关扩展,根据自己需求开启

extension=bz2
extension=curl
extension=fileinfo
extension=gd2
extension=gettext
extension=mbstring
extension=exif 
extension=mysqli
extension=odbc
extension=openssl
extension=pdo_mysql
extension=pdo_odbc
extension=pdo_pgsql
extension=pdo_sqlite
extension=pgsql

extension=soap
extension=sockets
extension=sqlite3
extension=tidy
extension=xmlrpc
extension=xsl

设置时区

date.timezone = Asia/Shanghai

注意:需要用到的配置,如果前面有分号(;)一定要去掉。

Nginx相关配置:

从官网下载Nginx压缩包后,解压到指定目录,在Nginx文件目录下找到conf文件,然后里面有个nginx.conf

然后修改nginx.conf中的配置如下:

#端口号,没有被占用的端口号即可
listen 8080;
server_name localhost;
location / {
  #root是你项目的入口路径,如果不是用框架的话直接PHP项目目录即可   root C
:/Users/Dolts/phpProjects/mall/public;   index index.html index.htm index.php;
  #我是ThinkPHP框架,为了在地址栏省去index.php而设置的,你一开始可以不用配置
  #配置该项后,http://localhost:8080/index.php?=index/index/hello可以访问
  #也可以直接用http://localhost:8080/index/index/hello访问   
if (!-e $request_filename) {     rewrite ^/index.php(.*)$ /index.php?s=$1 last;     rewrite ^/(.*)$ /index.php?s=$1 last;   } }

 location ~ \.php$ {
   root C:/Users/Dolts/phpProjects/mall/public;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include fastcgi_params;
 }

这里的“$document_root”就是指前面“root”所指的站点路径

日志相关配置(根据自己需求配置,不需要可以不管)

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;
access_log logs
/host.access.log main;

在开启Nginx服务之前,必须要开启php-cgi

端口与nginx.conf配置下的fastcgi_pass保持一致

php-cgi -b 127.0.0.1:9000    不要关掉该窗口

然后开启另一个cmd窗口

start nginx     (然后任务管理器中,详细信息下就会出现php-cgi.exe、nginx.exe)

或者nginx -s reload

 

常见错误:

1.nginx -s reopen/nginx -s reload后 nginx: [error] invalid PID number "" in "/run/nginx.pid"

原因:nginx.pid文件内没有指定PID(Windows系统每一个进程都有一个PID)

解决方案:start nginx   该命令会自动给出PID

2.页面请求PHP文件后 An error occurred,HTML文件正常

解决方案:查看php.ini中相关cgi的配置是否正确配置了,是否开启php-cgi服务 php-cgi -b 127.0.0.1:9000

3.页面请求PHP文件后 No input file specified,HTML文件正常

解决方案:修改nginx.conf配置

fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 修改如下
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

4.执行开启nginx服务后 nginx: [error] CreateFile() 

原因:它无法创建相关文件,windows家庭版有很多目录需要管理员身份权限才能进行操作

解决方案:在指定目录下,自己手动创建相关文件。(如果该目录无法创建文件,可在桌面创建后copy过去)

扩展:

如果你有多个项目,nginx.conf可以进行拆分

在nginx.conf中加入include config_web/*.conf;  (include 文件名称/*.conf;)

然后在nginx.conf目录下创建config_web文件夹,在该文件下可以创建多个conf文件

在新建的conf文件中,只需要copy nginx.conf文件中的server部分,根据自己的需求进行相关配置,我的配置如下

server {
        listen       8081;
        server_name  localhost;

        #charset koi8-r;

      access_log logs/mall.access.log main;
      error_log logs/mall.error.log;

        location / {
            root   C:/Users/Dolts/phpProjects/mall/public;
            index  index.html index.htm index.php;
            if (!-e $request_filename) {
                rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                rewrite ^/(.*)$ /index.php?s=$1 last;
            }
        }

        #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           C:/Users/Dolts/phpProjects/mall/public;
            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;
        #}
    }

nginx.conf文件可以换成初始状态,如果要用到日志的话,日志相关配置要开启,日志文件无法自动创建的话,手动创建

 

参考链接:https://blog.csdn.net/hl648777580/article/details/79565494

https://www.cnblogs.com/Li-Cheng/p/4399149.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM