处理Yii2.0在lnmp环境下500错误
twocode

    1.设置nginx错误日志:

error_log  /home/wwwlogs/test.com.error.log;#记录错误日志

    2.查看错误日志,cat -n error.log:

FastCGI sent in stderr: "PHP message: PHP Warning:  require(): open_basedir restriction in effect. File(/home/document/test.com/common/third/PHPExcel/PHPExcel.php) is not within the allowed path(s): (/home/document/test.com/web/:/tmp/:/proc/) in /home/document/test.com/web/index.php on line 16
		
PHP message: PHP Warning:  require(/home/document/test.com/PHPExcel/PHPExcel.php): failed to open stream: Operation not permitted in

    3.对症下药:

        open_basedir【控制项目有效权限目录】设置的三种方式:

                1).php_ini文件[或ini_set]直接修改,默认为不生效 注释

                2).nginx设置:(优先级最高)

fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";

                3).项目根目录下,添加.user.ini文件中设置:

open_basedir=/home/wwwroot/local.rest.m2.com:/tmp/:/proc/

ps:设置路径是对应的目录下生效的话,需要 ‘/’,结尾,否则例如‘root/web’,真实生效的权限目录是‘root/’

附:nginx 配置

nginx.conf
	server
    {
        listen 80;
        server_name test.com ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/document/test.com/web;#项目目录入口index.php所在
		
        location ~ [^/]\.php(/|$)
        {
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include 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;

				#fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";
				fastcgi_param PHP_ADMIN_VALUE "open_basedir=/home/wwwroot/local.rest.m2.com/:/tmp/:/proc/";
			】
			fastcgi_param RUNTIME_ENVIROMENT 'dev';#设置环境变量 php调用方式$_SERVER['RUNTIME_ENVIROMENT']
			include pathinfo.conf;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/test.com.access.log;#
		error_log  /home/wwwlogs/test.com.error.log;#记录错误日志
    }
网友评论已关闭