为什么我的PHP无法呈现?为什么我的php页面空白?

时间:2019-05-03 23:23:19

标签: php nginx configuration centos centos7

我正在尝试设置wordpress服务器,但是在此之前,我需要先启动PHP。目前,我已经安装并配置了nginx和php 7.3,但是php并未呈现,它只是显示文本。

主要问题:

[root@a-knapsack-sav ~]# cat /usr/share/nginx/html/info.php
<?php
phpinfo();
?>
[root@a-knapsack-sav ~]# curl http://localhost/info.php
<?php
phpinfo();
?>

很明显,我应该期望从上面看到一些内容,但是我没有。以下是我完成的所有设置。希望有人能看到这一点并指出我可能的遗漏。

您正在使用什么操作系统?

[root@a-knapsack-sav nginx]# rpm -q centos-release
centos-release-7-6.1810.2.el7.centos.x86_64

是否还安装了Nginx?

[root@a-knapsack-sav nginx]# nginx -v
nginx version: nginx/1.15.12

是否还安装了php?

[root@a-knapsack-sav nginx]# php -v
PHP 7.3.5 (cli) (built: Apr 30 2019 08:37:17) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.5, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.5, Copyright (c) 1999-2018, by Zend Technologies

在哪里安装了Nginx?

[root@a-knapsack-sav nginx]# pwd
/etc/nginx
[root@a-knapsack-sav nginx]# ls
conf.d  fastcgi_params  koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params  sites-available  sites-enabled  uwsgi_params  win-utf

php-fpm是否还在运行?

[root@a-knapsack-sav nginx]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 10:42:25 CDT; 7h ago
 Main PID: 2772 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php-fpm.service
           ├─2772 php-fpm: master process (/etc/php-fpm.conf)
           ├─2773 php-fpm: pool www
           ├─2774 php-fpm: pool www
           ├─2775 php-fpm: pool www
           ├─2776 php-fpm: pool www
           └─2777 php-fpm: pool www

Nginx是否还在运行?

[root@a-knapsack-sav nginx]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 17:19:55 CDT; 31min ago
     Docs: http://nginx.org/en/docs/
  Process: 4299 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 4302 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 4303 (nginx)
   CGroup: /system.slice/nginx.service
           ├─4303 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─4304 nginx: worker process

您是否编辑了php.ini?cgi.fix_pathinfo出于安全性考虑。

[root@a-knapsack-sav nginx]# cat /etc/php.ini | grep cgi.fix
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

您是否将ngs的conf设置更新为nginx?
我想是这样。

[root@a-knapsack-sav ~]# cat /etc/php-fpm.d/www.conf | grep listen
; - 'listen' (unixsocket)
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;   'port'                 - to listen on a TCP socket to all addresses
;   '/path/to/unix/socket' - to listen on a unix socket.
listen = /var/run/php-fpm/php-fpm.sock
; Set listen(2) backlog.
;listen.backlog = 511
listen.owner = nobody
listen.group = nobody
;listen.mode = 0660
; When set, listen.owner and listen.group are ignored
;listen.acl_users = apache,nginx
;listen.acl_groups =
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
listen.allowed_clients = 127.0.0.1
;   listen queue         - the number of request in the queue of pending
;                          connections (see backlog in listen(2));
;   max listen queue     - the maximum number of requests in the queue
;   listen queue len     - the size of the socket queue of pending connections;
;   listen queue:         0
;   max listen queue:     1
;   listen queue len:     42
[root@a-knapsack-sav ~]# cat /etc/php-fpm.d/www.conf | grep nginx
user = nginx
group = nginx
;listen.acl_users = apache,nginx

您是否正确更新了Nginx的conf? 据我所知....

[root@a-knapsack-sav ~]# cat /etc/nginx/conf.d/default.conf
server {
    listen       80;
#   listen       [::]:80;
    server_name  A.B.C.D;
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    error_page   404 /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~* \.php$ {
         try_files $uri =404;
         fastcgi_index index.php;
         include fastcgi_params; 
         fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#        fastcgi_param SCRIPT_NAME     $fastcgi_script_name;
     }
#    location ~ /.ht {
#        deny  all;
#    }
}

您的info.php文件是否放置在正确的位置?
耶齐尔。

[root@a-knapsack-sav ~]# ls /usr/share/nginx/html
50x.html  index.html  info.php

您的nginx.conf怎么样?

[root@a-knapsack-sav ~]# cat /etc/nginx/nginx.conf

    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       /etc/nginx/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  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*.conf;
        server_names_hash_bucket_size 64;
    }

您的网站可用的配置如何? 我不知道是否需要这样做,但我还是尝试了。

[root@a-knapsack-sav ~]# cat /etc/nginx/sites-available/default.conf
server {
    listen       80;
    server_name A.B.C.D;
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    error_page   404 /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ .php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }
#    location ~ /.ht {
#        deny  all;
#    }
}

您是否忘记了链接启用网站和可用网站? 不:)

[root@a-knapsack-sav ~]# ls -lrt /etc/nginx/sites-enabled
total 0
lrwxrwxrwx. 1 root root 39 May  3 12:40 default.conf -> /etc/nginx/sites-available/default.conf

我希望这就是一切。我认为这就是我所做的一切。当我curl http://localhost加载index.html时,但是如上所示,info.php在调用时未呈现。我认为,我已经将其设置为几乎正确的,但是我很确定我错过了一些事情。请让我知道是否出现问题。我尝试遵循https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7(mysql在另一台服务器上)。另外,如果需要更改任何配置以使curl http://A.B.C.D不挂起并像curl http://localhost一样返回,我将不胜感激。谢谢:)

编辑:

您的整个配置是什么?

[root@a-knapsack-sav ~]# nginx -T
nginx: [warn] conflicting server name "A.B.C.D" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
    server_names_hash_bucket_size 64;
}

# configuration file /etc/nginx/mime.types:

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

# configuration file /etc/nginx/conf.d/default.conf:
server {
    listen       80;
#   listen       [::]:80;
    server_name  A.B.C.D;
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    error_page   404 /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~* \.php$ {
         try_files $uri =404;
         fastcgi_index index.php;
         include fastcgi_params; 
         fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#        fastcgi_param SCRIPT_NAME     $fastcgi_script_name;
     }
#    location ~ /.ht {
#        deny  all;
#    }
}

# configuration file /etc/nginx/fastcgi_params:

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;

# configuration file /etc/nginx/conf.d/default_original.conf:
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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   /usr/share/nginx/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  /scripts$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;
    #}
}


# configuration file /etc/nginx/sites-enabled/default.conf:
server {
    listen       80;
    server_name A.B.C.D;
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    error_page   404 /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ .php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }
#    location ~ /.ht {
#        deny  all;
#    }
}

1 个答案:

答案 0 :(得分:0)

在理查德·史密斯(Richard Smith)的问题之后,nginx -T表明我有另一个配置项正在干扰我的配置/etc/nginx/conf.d/default_original.conf。因此,我做了mv /etc/nginx/conf.d/default_original.conf /etc/nginx/conf.d/default.conf.original使其不再加载,并在include /etc/nginx/fastcgi_params;中将include fastcgi_params;更改为/etc/nginx/sites-available/default.conf。完成此操作后,我重新启动了nginx(systemctl restart nginx),curl http://localhost返回了html表的预期输出。