1

I am on Debian, with no sudo commands. I try to write a php script to create virtual hosts: ex:

file_put_contents("/etc/apache2/sites-available/{$newDomain}.conf", $content);

php runs under apache mod as www-data user and of course I am not enable to write in /etc/apache2/sites-available

failed to open stream: Permission denied 

I have many other commands that I can execute through php-cli when loggedon as root and that I cannot execute through the browser.

$cmd = "service apache2 restart";
shell_exec($cmd);

Any idea on how to execute them as root ?

Regards

yarek
  • 11,278
  • 30
  • 120
  • 219
  • 2
    `/etc/apache2/sites-available...` is owned by root, you need root to write the file (and also to restart the server) so without really making open is not possible. What I do is make a tasking system and root runs the jobs via a daemon. i.e put the domains and config in a database, then request root to build the file and restart the server (could also be achieved with by dropping a json file in a folder then using inotifywait, roots watching the folder to run and build) – Lawrence Cherone Jan 05 '21 at 21:48
  • See also: https://stackoverflow.com/questions/8532304/execute-root-commands-via-php. Make sure you don't just read the accepted answer since there are varying opinions on it. Anything to do with root commands mixed with user input should be treated with extreme caution. – Mike Jan 05 '21 at 21:53

0 Answers0