php搭配ubuntu/debian运行时出现404 error

ubuntu和debian可以安装php,而且更新了信源后可以升级到php7.0,但是有时候你可能遇到(本人遇到)明明php文件存在却是404错误

  1. 除过文件位置不对以外,还有一个原因,需要注意哦~(折腾两天的经验!)(这里只谈nginx)nginx配置文件conf没写对,导致fastcgi_param找不到文件。先贴一段标准的php配置语句
    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }

    这里的document_root是指网站根目录,root最好是在conf文件开始的地方,也就是server_name下面就写,写在server{}内,而不是location{}内
    例如:

    server {
    listen 80;
    server_name localhost;
    root /home/mydomain;
    index index.php index.html index.htm;
    }

    上面是对的!


    server {
    listen 80;
    server_name localhost;
    ...;
    }
    location / {
    try_files $uri $uri/ =404;
    root /home/mydomain;
    index index.php index.html index.htm;
    }

    则是错的!会导致404error! 不要问什么~
  2. 另外还要注意权限问题。nginx用户组是nginx,而php安装以后用户组是www-data,你需要修改/etc/php/7.0/fpm/pool.d/www.conf里面的www-data为nginx,否则会出现500错误。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据