What you are asking is very dangerous because any new vulnerabilities found in Apache can be exploited as root. One of the first things webmasters do when securing their web server is to not run the server application as root. That said, I offer the following alternative that should meet the need as you have stated it.
1- create a shell script that does what you want. This is not a PHP script as PHP is run within the web server, you need a script that is run by Ubuntu. Here is a sample:
# script that runs as root
I called this file /localstore/root.sh and you should place it somewhere that makes sense on your systems (e.g.
2- In a terminal window, execute the command
sudo vi /etc/sudoers and insert the following line at the end of the file (important to be at end so the other commands do not override):
ALL ALL=NOPASSWD: /localstore/root.sh
That will allow anyone to run the script as root, once you test and confirm it work, I STRONGLY recommend (again for security purposes) that you change the line to
nobody ALL=NOPASSWD: /localstore/root.sh
but as Bodhi observed, apache should run as
www-data in which case you should change
www-data. NOTE: I have never tried sudo with "nobody" as the user and am not sure that will work.
3- In your Web server script, have the shell script run in PHP as follows:
exec ("sudo /localstore/root.sh");
You can add parameters to the script inside the quotes.
That should get it so the root.sh script executes as root when invoked from your web server. If you have other scripts, they can be a comma separated list on that same entry in the sudoers file. Make sure the permissions on the script allow execution by the web server.