Skip to content
Advertisement

Use PHP exec to execute Minecraft command

I’m working on creating my own little Website to manage a Minecraft server as fun project. Now what I would need to accomplish is being able to send commands to the screen in which the server is running.

My approach to this was the following:

<?php
if (isset($_POST['startbutton']))
    {
        exec('sudo screen -S 23971 -X stuff "say hello^M"');
    }
?>

    
<form method="post">
    <button type="submit" name="startbutton">Test</button>
</form>

Now that command line works just fine when i execute it in the terminal itself, but as soon as i try to run it over the Website nothing happens. If i just try to execute

if (isset($_POST['startbutton']))
    {
        echo exec('whoami');
    }
?>

it works just fine as well. I don’t know what I am doing wrong.

Advertisement

Answer

First of all: Thank you all for your support. I was doing a bit of further research and found a solution for my problem:

I created a new user which I then gave the ownership of /var/www. I then changed the apache2 user from www-data to the new user. Now i just needed to start the screen with the minecraft server as the new user so i can access this screen out of php and I was able to get it to work without having to give any user full root privileges or anything.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement