Saturday, February 19, 2011

Execute system commands using PHP under Root Priviledges.

Suppose you have set-up a webserver (apache2) with PHP enabled and you need to execute some system commands or your own scripts under super user / root priviledges, then the following method may help you to solve the problem, especially under Ubuntu.

Step 1:  Run sudo visudo in a Terminal window.

Step 2:  Add the below lines at the end of the file, depending on your requirement.
www-data ALL=NOPASSWD: /sbin/iptables, /usr/bin/du

Step 3:  Try an exec command under PHP script to test the settings.

e.g : exec ("sudo iptables -P FORWARD ACCEPT");