Launching games from web over Nginx
-
Hey, a million thanks in advance to anyone who can help out
I've set up Nginx and PHP to act as a control panel for RetroPie (adjusting volume, various settings, keeping logs etc) and I'm trying to integrate a feature which would allow a user to start a game from this control panel (eg over their phone)
Here's what I've done:
- Given the user www-data permissions to execute commands as the user pi in visudo
www-data ALL=(pi) NOPASSWD:ALL
- Run this command in the terminal (locally and over SSH):
sudo -u pi DISPLAY=:0 /opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ snes '/home/pi/RetroPie/roms/snes/an-example-rom-obviously-not-the-real-one-but-lets-say-mario-or-something.sfc'
- It worked perfectly both locally and over SSH (started the game on the Pi)
- Placed the exact same command in a PHP file like this and hit it from a browser over HTTP:
<?php $command = shell_exec("sudo -u pi DISPLAY=:0 /opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ snes '/home/pi/RetroPie/roms/snes/an-example-rom-obviously-not-the-real-one-but-lets-say-mario-or-something.sfc'"); echo $command; // redundant but you never know ?>
- The game does not start. The page takes a minute to load, htop reveals the process starts, and then just quits
- Replaced that command with a different one:
<?php $command = shell_exec("sudo -u pi espeak 'hello one two three' --stdout | aplay"); echo $command; // cant help myself ?>
- The command executed properly and the TTS was heard from the speaker connected to the Pi
- (same results over SSH and locally through the terminal as well)
That's where I've gotten myself, again a million thanks to anyone who might point me in the right direction
Edit: To clarify, when I executed that command
sudo -u pi DISPLAY=:0 /opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ snes '/home/pi/RetroPie/roms/snes/an-example-rom-obviously-not-the-real-one-but-lets-say-mario-or-something.sfc'
in the terminal over SSH and locally, I did it only after:
su - www-data -s /bin/bash
So that it would be running as the same user as it would be when over PHP
- I then SSH'd into the pi and ran the following:
su - www-data -s /bin/bash php ./that-file-with-the-command-that-didnt-work-on-the-web-server.php
(ran the php file over the terminal as the www-data user)
- And it worked, loaded the game, so I'm pretty desperately confused
-
This post is deleted! -
Solved it
In case anyone else ever tries to do something like this and finds this post, the problem was this:
Somewhere along the line, something required to start up the ROM expected an interactive terminal, which is why it worked over SSH and locally, and even worked with running the PHP script from the terminal, but Nginx/Apache/another web server won't provide any interactive terminal.It doesn't actually need any interactive terminal, so you can wrap the startup command inside a "script" command like this, to lie to it and trick it into believing it has an interactive terminal
<?php $command = '/opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ snes "/home/pi/RetroPie/roms/snes/an-example-rom-obviously-not-the-real-one-but-lets-say-mario-or-something.sfc"'; $result = shell_exec("sudo -u pi script --return --quiet -c '".$command."' /dev/null"); ?>
And that does the trick and starts the game up from a PHP web panel
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.