【问题标题】:Nginx not running php files after pear install梨安装后Nginx不运行php文件
【发布时间】:2017-09-29 08:15:39
【问题描述】:

我在这里使用本指南安装了 Nginx 和 PHP:

Nginx install guide

yum install php php-mysql php-fpm

已编辑 /etc/php-fpm.d/www.conf

listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
listen.owner = nobody
listen.group = nobody

跑:

systemctl start php-fpm

/etc/nginx/conf.d/default.conf

server {
listen       80;
server_name  <my servers IP here - removed>;

# note that these lines are originally from the "location /" block
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_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
}

然后我测试了

http://<my server ip>/phpinfo.php

成功了!

然后我安装了 pear

yum install php-pear

但现在 php 文件不起作用,浏览器会尝试下载它们而不是运行它们。

我试过了:

  1. 重启服务器
  2. 重启nginx
  3. 重启 php-fpm
  4. 检查所有配置文件以确保它们与上述相同。

我完全被困住了。我不知道要检查什么才能让 php 再次工作。这是我第一次安装 Nginx。我在网上四处寻找答案并在此处查看。

我正在运行 Centos 7

帮助:)

更新:

我尝试了一个更短的配置文件:

server {
listen       80;
server_name  <my servers IP here - removed>;

# note that these lines are originally from the "location /" block
root   /usr/share/nginx/html;
index index.php index.html index.htm;

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
}

但这也没有用。

我也试过了:

fastcgi_pass 127.0.0.1:9000;

代替:

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

但这也没有用。

更新 2

我发现如果我去:

http://my ip/phpinfo.php - 有效!

但如果我去:

http://domainname/phpinfo.php - 它尝试下载 PHP 文件而不是运行它。

如何让php文件在使用域名而不是IP的情况下运行?

【问题讨论】:

  • 我试图卸载 pear - yum uninstall php-pear 但它没有解决问题
  • 我尝试卸载重新安装 nginx & php 但它仍然不处理 php 文件
  • 日志是怎么说的?
  • 如果我没记错的话,我前段时间遇到过这个问题。我不记得我是如何解决的,但我认为日志对我有帮助。尝试在您的日志文件(nginx + php)上执行tail -f,以查看调用页面时发生的确切情况:这可能是来自 php-fpm 的问题。
  • 我发现使用服务器的IP而不是域名浏览到phpinfo文件时会运行php文件。关于如何使用域名而不是 IP 来实现这项工作的任何想法?

标签: php linux nginx centos pear


【解决方案1】:

您是否尝试过编辑

/etc/nginx/sites-enabled/default ?

那你需要编辑如下。

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

之后,你必须重新启动 php-fpm 和 nginx

service nginx restart && service php-fpm restart

【讨论】:

  • 我尝试了你的配置文件,但 Nginx 无法使用这个文件重新启动
  • cgi.fix_pathinfo 是否在 /etc/php5/fpm/php.ini 中设置为 0 ?
  • 在 /etc/php.ini 中是的
  • 是的,我已经清除了缓存,甚至使用了其他浏览器,我也尝试过使用 curl ip/phpinfo.php
【解决方案2】:

尝试使用以下配置获取位置,

location ~ .php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;
    include        fastcgi_params;
}

并事先清除缓存。

对于您的第二次更新,请尝试以下服务器块,

listen 127.0.0.1:8000; #your ip with port
server_name  example.com; #domain name

【讨论】:

    【解决方案3】:

    最近还需要搭建linux和php环境,所以我严格按照你的步骤来设置。

    我在设置 LEMP 环境时遇到了这两个问题,

    一个。 nginx 问题,postanswer

    b. php-fpm 问题,postanswer

    我没有遇到你面临的问题,所以我给你展示环境,你可以和你的比较。

    Centos 版本

    vi /etc/centos-release
    

    CentOS Linux 版本 7.3.1611(核心)

    Mysql版本

    mysql -u root -p
    //enter password
    

    服务器版本:5.5.52-MariaDB MariaDB 服务器

    PHP版本

    php -v
    

    PHP 5.4.16 (cli)(构建时间:2016 年 11 月 6 日 00:29:02)

    梨信息

    pear list // 参考这个post

    Installed packages, channel pear.php.net:
    ========================================= 
    Package          Version State
    Archive_Tar      1.3.11  stable
    Console_Getopt   1.3.1   stable
    PEAR             1.9.4   stable
    Structures_Graph 1.0.4   stable
    XML_Util         1.2.1   stable
    

    Nginx 配置文件 vi /etc/nginx/conf.d/default.conf

    server {
        listen       80;
        server_name  192.168.236.129;
    
        # note that these lines are originally from the "location /" block
        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_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    info.php vi /usr/share/nginx/html/info.php

    <?php phpinfo(); ?>
    

    【讨论】:

    • 谢谢你,除了我没有安装mysql之外,一切都一样。而且我的 php 版本稍旧。我用你的复制了我的 nginx 配置文件并重新启动了一切。我发现使用服务器的IP而不是域名浏览到phpinfo文件时会运行php文件。
    • 检查你的hosts文件看你的域名绑定到哪个ip,这可能是你的DNS问题。
    【解决方案4】:

    根据您的第二次更新,您的 Listen 指令有问题。 nginx 只监听 Listen 指令中提到的 IP 地址或域名。马克西米利安给了你正确的答案。如果你把listen 80 放在你的配置文件中,你会解决你的问题,但是只有一个站点,听这个。如果您想在您的域上配置 PHP,您可以配置 listen yourdomain.com:80

    【讨论】:

    • @user1398287 不客气。请标记正确答案。 :)
    • yourdomain.com 与 default_server 不同吗?
    • @MaximilianWeigand,嗯,这是不同的。 default_server 将标头中包含未知域的所有请求路由到此服务器部分,yourdomain.com 路由添加请求的标头中包含 yourdomain.com 到此服务器部分。
    猜你喜欢
    • 2020-03-19
    • 2013-02-19
    • 1970-01-01
    • 2011-07-22
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多