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;
    #    }
    #}

}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值