whoami prints the effective user id.
When you start Apache, I guess you do it using the root user, or with some
sudo rule. This is required to let Apache bind ports below 1024 (so the default 80 HTTP or 443 HTTPS qualify).
In your Apache config, you put
User X (and probably
Group X). This tells Apache that once the first process is started (via root), all other children processes will run under the
X:X user and group.
So in your PHP code, when you run
whoami it returns the effective ID of the process PHP runs in. This is X, since the process is a child of the first process.
But when you run
env it runs the environment under which the child process was started. This environment was inherited from the first process, which is started by root. Hence you see the
env of root.
Your fix is ok.