Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Ok I need to run my Apache web server as root. For this I typed echo whoami; in terminal. It gives me output: root. But when I check my apache server running as a root user or not by executing following php-script:

< ?php echo `whoami`;  ?>

It gives me output: nobody. So any suggestions to execute/login as a root user in apache??

share|improve this question
I have never seen a user case where apache needs to run as root. by default, on Ubuntu, apache runs as www-data. What are you trying to do exactly ? There is almost certainly a better solution then running apache or php scripts as root. –  bodhi.zazen Mar 26 '12 at 14:08
Can you provide the reason for using Apache as root? I mean what problem could there be with Apache that you need to run it as root to solve it? –  Luis Alvarado Mar 26 '12 at 14:28
Do you need to access to any folder that has only rights for root? –  husker Mar 26 '12 at 14:29
I am working on xen virtualization.I am using LIBVIRT-PHP to talk with hypervisor to start create destroy etc etc. For this I used this php-script: <?php $conn=libvirt_connect("xen:///"); $name=libvirt_domain_lookup_by_id($conn,3); $dest=libvirt_domain_destroy($name); echo $dest; ?> When I execute this it shows me: Warning: libvirt_domain_destroy() [function.libvirt-domain-destroy]: operation virDomainDestroy forbidden for read only access in /opt/lampp/htdocs/xampp/shutdown.php on line 5 –  Alee Mar 26 '12 at 14:49
"Ok I need to run my Apache web server as root." No you do not. –  Rinzwind Apr 19 '12 at 14:43

1 Answer 1

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/ and you should place it somewhere that makes sense on your systems (e.g. /home/www_data/bin).

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/

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/

but as Bodhi observed, apache should run as www-data in which case you should change nobody to 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:

< ?php
exec ("sudo /localstore/");

You can add parameters to the script inside the quotes.

That should get it so the 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.

share|improve this answer
This is the proper way to execute commands as root using PHP, which is risky enough as it is. –  Marco Ceppi Apr 19 '12 at 15:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.