Tuesday, November 2, 2021

[SOLVED] Php has its own /tmp in /tmp/systemd-private-nABCDE/tmp when accessed through nginx

Issue

I found strange behaviour concerning php and /tmp folder. Php uses another folder when it works with /tmp. Php 5.6.7, nginx, php-fpm.

I execute the same script in two ways: via browser and via shell. But when it is launched via browser, file is not in real /tmp folder:

<?php
$name = date("His");

echo "File /tmp/$name.txt\n";

shell_exec('echo "123" > /tmp/'.$name.'.txt');

var_dump(file_exists('/tmp/'.$name.'.txt'));

var_dump(shell_exec('cat /etc/*release | tail -n 1'));

php -f script.php

File /tmp/185617.txt
bool(true)
string(38) "CentOS Linux release 7.0.1406 (Core)

Where is the file? In /tmp

$ find / -name 185617.txt
/tmp/185617.txt

If access it via http://myserver.ru/script.php I get

File /tmp/185212.txt
bool(true)
string(38) "CentOS Linux release 7.0.1406 (Core)

But where is the file?

$ find / -name 185212.txt
/tmp/systemd-private-nABCDE/tmp/185212.txt

Why does php thinks that /tmp should be in /tmp/systemd-private-nABCDE/tmp?


Solution

Because systemd is configured to give nginx a private /tmp. If you must use the system /tmp instead for some reason then you will need to modify the .service file to read "PrivateTmp=no".



Answered By - Ignacio Vazquez-Abrams