As part of the signup process on our site, VirtualHost directives get created an added to a config file via PHP, after each VirtualHost is created apache is restarted gracefully so that the new site starts working. The user PHP which runs under (www) was given permission to restart apache in /etc/sudoer and then a php script called exec('/usr/local/bin/sudo /usr/local/sbin/apachectl graceful');

This works perfectly. However we're in the process of moving to a new server and I cant get this working at all on the new setup

Ive added the following line to /etc/sudoers

apache ALL= NOPASSWD: /usr/sbin/apachectl graceful

and then our PHP script calls:

exec('/usr/bin/sudo /usr/sbin/apachectl graceful');

If I enable shell access for the apache user and login I can run 'sudo /usr/sbin/apachectl graceful' perfectly fine, its just when we call it from PHP it doesnt work.

Has anybody got any ideas? Thanks.

On our old FreeBSD server we were running Apache 1.3.x and PHP 4.4

Our new server is CentOS 5.4 w/ Apache 2.2.x and PHP 5.3.2

2 Answers 2


In the sudoers file (use visudo), below the extant defaults line, add:

defaults:www !requiretty

Actually, are you sure it's running as the "www" user? The default for CentOS is "apache". A little script to try:

<?php print system("whoami");?>

If it's not "www", just replace the "www" bit in sudoers, above.

  • This did the trick, Adding: Defaults:apache !requiretty to /etc/sudoers. Thank you very much!
    – James
    Jul 14, 2010 at 15:10
  • As I recall, figuring out something very similar to this took about four hours and culminated in some work in gdb. So... yeah. The more people I can help solve this problem, the more I amortize that effort. ;)
    – BMDan
    Jul 15, 2010 at 21:41

Can you try with shell_exec ?

  • I've just tried shell_exec() exactly the same, no output returned either if that makes any difference. PHP is definitely running under the apache user, ive just tried shell_exec('whoami').
    – James
    Jul 14, 2010 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .