nginx+php配置

nginx+php配置

一、下载文档

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

nginx+php配置

 2.下载nginx: nginx: downloadnginx+php配置http://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窗口,让服务在后台运行,可进行第五步骤

nginx+php配置

五:后台启动服务

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

点击阅读全文

上一篇 2023年 5月 27日 am11:00
下一篇 2023年 5月 27日 am11:01