Wednesday, April 30, 2014

How to run any command on php as root

Running sudo root commands in php

edit the sudoers file (vi /etc/sudoers) and then under "#User priviligie specification" (where it should have root    ALL=(ALL:ALL) ALL) add:


www-data ALL=NOPASSWD: path_to_command


example for chown:


www-data ALL=NOPASSWD: /bin/chown

then inside php run using:


exec("sudo -u root command);

example for chown:

exec("sudo -u root chown -R www-data /directory");

This is particular usefull if you are trying to edit/delete a file/folder that is owned by root through php. Meaning solution for:

unlink Permission denied