php提示File not found.

server {
    listen       80;
    server_name  localhost;

    access_log  /etc/nginx/conf.d/access.log  main;
    error_log   /etc/nginx/conf.d/error.log;

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        root           /home/php;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

当nginx匹配到扩展名是php后,交由php-fpm处理,fpm会去 root 定义的目录下执行相应的php脚本。

fastcgi_param 默认配置:

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 
默认是去 /scripts 目录下找对应的请求文件,就会报错提示:File not found.

改为:

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

就会去读 root 参数定义的目录

nginx中多个location段中可以配置不同的 root 参数。

另外一次遇到这个错误是nginx运行用户和php-fpm运行用户不一致导致的,但是在后来搭建测试环境没有再复现这个问题,若遇到修改上面路径不生效,可以看一下两者用户是否一致。

注意浏览器报的错误信息很模糊,需要结合访问日志去排查问题。

注意:nginx是告诉php-fpm请求文件的路径,而不是把代码文件读出来交给php-fpm。

当nginx和php-fpm不在同一台机器(比如docker中),就需要同时建立相同的路径。

awk 不打印某一列的输出

比如一个a文件,内容如下,现在要实现不打印最后一列。

[root@localhost ~]# cat a
a b c

# 通过设置最后一列为空

[root@localhost ~]# cat a | awk '{$NF="";print$0}'
a b 

# $0 是打印所有列  (数字零)

[root@localhost ~]# cat a | awk '{print $0}'
a b c

# 排除多列

[root@localhost ~]# cat a | awk '{$2="";$NF="";print$0}'
a  
[root@localhost ~]# cat a | awk '{$2="";print$0}'
a  c

# 排除多列另一种写法 (等于多次变量赋值)

[root@localhost ~]# cat a | awk '{$2=$NF="";print$0}'
a  

上面可以看到排除的那列变为了一列空列,再用tr(tr是translate缩写)去除空列:

[root@localhost ~]# cat a | awk '{$2="";print$0}'
a  c # 中间有空列
[root@localhost ~]# cat a | awk '{$2="";print$0}' | tr -s ' ' ' '
a c # 排除了空列

tr -s参数意思是如果有多个连续相的同字符就合并为1个。

Failed to get D-Bus connection: Operation not permitted

版本信息:

root@deepin:~# docker --version
Docker version 18.09.6, build 481bc77
root@deepin:~# docker run -dit --name test centos /usr/sbin/init
在容器内启动apache
[root@8065435580b6 ~]# systemctl start httpd
Failed to get D-Bus connection: Operation not permitted

以前遇到这个报错,是通过docker run创建容器时执行 /usr/sbin/init 这个命令解决,现在发现也不好用了,还需要加上–privileged=true

root@deepin:~# docker run -dit --name test --privileged=true centos /usr/sbin/init

安装apache再用systemctl启动测试:

[root@7e43e6387f93 /]# systemctl start httpd
[root@7e43e6387f93 /]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2421/httpd