How can I invoke aplay from nginx/php

I am trying to make aplay play a wav file on my Beaglebone running Debian (trixie).

I can do it from an ordinary login user no problem.

aplay -D dmix:CARD=Device,DEV=0 somefile.wav

works perfectly and plays the sound on a USB speaker, so I believe the hardware is all just fine. The problem arises when I try and invoke aplay from a PHP script running under nginx. That is running under user www-data so I added www-data to the audio group. Then I was able to do this:

sudo -u www-data aplay -l

which shows me the active audio devices and I can also tell it to play the audio file, and it does successfully. It only fails when it is running in the real environment ie under nginx. In that environment aplay returns a status 1 and I can’t tell why.

More specifically, PHP runs a shell_exec to invoke a small bash script that invokes aplay and logs a few things. I can run this bash script from my login user and it works, and under nginx/php it logs its output just fine so I know it is running and that it is invoking the wav file I want it to.

The target file name is definitely correct. I use the full path and I log it. I can copy/paste from the log and invoke aplay with that file and it works fine. Permissions on the file are -rw-rw-r-- so it is readable by anyone. It is owned by a different user, though.

User www-data has no ~/.asoundrc file and neither does my login user.

I know this can work because I had it running on this machine up until I rebuilt it. I’m fairly sure there is some, hopefully obvious, trick I have forgotten from the first time I set it up, though that was pre-trixie.