Centos7下apache使用的tmp目录问题

问题描述

今天在做web开发时碰到一个很奇怪的问题,使用PHP执行了一些shell脚本,因为时间较长,所以使用了nohup来后台运行,并将输出重定向到/tmp目录下的一个临时文件来调试。
一个奇怪的问题出现了,我的命令都有正常执行,但是输出的文件就是没有出现在/tmp目录下,这是为什么?

code:

$command = 'echo Hello World > /tmp/aaa';
shell_exec($command);
ll -a /tmp:

[root@sjc tmp]# ll
total 2048
drwxrwxrwx  11 root root     240 Apr 16 02:29 .
dr-xr-xr-x. 17 root root     244 Oct  6  2017 ..
drwx------ 3 root root      60 Apr 16 01:23 systemd-private-bf21bbefdh-httpd.service-cdMgBH

这时候只需要进到这个tmp目录下,就会发现我们刚才创建的aaa文件已经在这里面了

问题解决

这时候会发现这里出现一个奇怪的目录

systemd-private-bf21bbefdh-httpd.service-cdMgBH

这里有出现httpd.service这不就是我们的Apache服务吗?进去看下内容

[root@sjc tmp]# ll -a /tmp/systemd-private-bf21bbefdh-httpd.service-cdMgBH/
total 0
drwxr-xr-x  3 root root  60 Apr 16 01:23 .
drwxrwxrwx 11 root root 240 Apr 16 02:29 ..
drwxrwxrwt  2 root root  60 Apr 16 02:14 tmp

这时候就知道了,原来系统给这个服务单独创建了一个自己的private tmp 目录,这时候只需要把这个目录也改成777,代码就能顺利执行了。

补充内容

这个是linux系统中关于private tmp的一个配置,比如查看关于apache的配置如下:

[root@sjc tmp]# cat /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意到上面的PrivateTmp=true这说明这个服务使用到tmp目录是默认会创建一个私有的文件夹来使用,如果不想要使用,只需要把这个true设置为false就可以了。

参考资料:http://blog.oddbit.com/2012/11/05/fedora-private-tmp/

猜你喜欢

转载自blog.csdn.net/cckooo/article/details/79957748