• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 在 centOS9 上配置 Nginx 支持 PHP8(本地环境)

    使用状况:本机 Nginx 配置 localhost 根目录是/var/web/www。在这个目录下又新建设了多个 Laravel 项目。每个单独的项目,还区分前台后台。

    • http://localhost:访问根目录下,index.php
    • 前台:http://localhost/lanmper/public,访问lanmper项目。后台:http://127.0.0.1/lanmper/public
    • 前台:http://localhost/seocher/public,访问seocher项目。后台:http://127.0.0.1/seocher/public
    • 前台:http://localhost/chandaoweb/public,访问chandaoweb项目。后台:http://127.0.0.1/chandaoweb/public


    vim /usr/local/nginx/conf/nginx.conf
    

    nginx.conf内容如下:

    user  www;
    worker_processes  2;
    
    error_log  /var/log/nginx/error.log  notice;
    pid  /usr/local/nginx/logs/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        client_max_body_size 20M;
    
        gzip  on;
            gzip_http_version 1.1;
            gzip_vary on;
            gzip_comp_level 3;
            gzip_proxied any;
            gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png;
            gzip_min_length 1;
            gzip_buffers 16 8k;
            gzip_disable "MSIE [1-6].(?!.*SV1)";
    
        server {
            listen       80;
            server_name  localhost;
            charset utf-8;
            root /var/web/www;
            index index.php index.html index.htm;
    
            error_page  404              /404.html;
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
               root html;
            }
    
            location ~ \.php(.*)$ {
                include fastcgi.conf;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
            }
    
            location /lanmper/public
            {
                index index.php;
                if (!-e $request_filename)
                {
                    rewrite ^/lanmper/public/(.*)$ /lanmper/public/index.php?$1 last;
                    break;	
                }
            }
    
            location /seocher/public
            {
                index index.php;
                if (!-e $request_filename)
                {
                    rewrite ^/seocher/public/(.*)$ /seocher/public/index.php?$1 last;
                    break;	
                }
            }
    
            location /chandaoweb/public
    	    {
                index index.php;
                if (!-e $request_filename)
                {
                    rewrite ^/chandaoweb/public/(.*)$ /chandaoweb/public/index.php?$1 last;
                    break;	
                }
            }
    
    		location ~ /\.env {
                deny  all;
            }
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            location ~ /\.ht {
                deny  all;
            }
        }
    
    }
    


    fastcgi.conf内容如下:

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;
    
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  REQUEST_SCHEME     $scheme;
    fastcgi_param  HTTPS              $https if_not_empty;
    
    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    
    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;
    
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;