restarting apache within php script on linux

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
samtay
Forum Newbie
Posts: 9
Joined: Sun Aug 20, 2006 8:44 am
Location: Manchester, UK

restarting apache within php script on linux

Post by samtay »

I need to restart Apache from an php script. I have been searching google for days now and can't find any information. Can anyone help?

Thanks
Sam Taylor
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Try using the exec function.

Code: Select all

exec ("/etc/init.d/apache restart");
I'm not sure about the command to restart apache, you'll have to make sure that the path and command are right, but that should work.
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

blackbeard wrote:Try using the exec function.

Code: Select all

exec ("/etc/init.d/apache restart");
I'm not sure about the command to restart apache, you'll have to make sure that the path and command are right, but that should work.
Won't work. First off, it is /etc/init.d/httpd. Second, /var/run/httpd.pid is owned and grouped by root (-rw-r--r--), meaning that the service can only be stopped/started by root. All apache child threads run as the user apache, so effectively no php script can restart apache.

To possible solution is to create a cron job run by root every minute that checks for the existance of a particular file and if it does exist it restarts httpd and deletes the file. Then you just create that file from php if you need apache restarted and wait at the most 1 minute.
User avatar
samtay
Forum Newbie
Posts: 9
Joined: Sun Aug 20, 2006 8:44 am
Location: Manchester, UK

Post by samtay »

Does anyone know of any other better way because of permissions?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

system() perhaps?
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

Everah wrote:system() perhaps?
system() and exec() are almost exactly the same except in the manner they handle program output (system puts it in the buffer and trys to flush the buffer, exec puts it in an optional 2nd parameter passed byref).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I was just throwing ideas out there. It seems (in my logic anyway) that using PHP to restart apache might have some undesired results. When Apache stops to restart, PHP will no longer be able to process because the server is not on for that small amount of time. Am I wrong about this?
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

Maybe this wiil help. You have the httpd process, running as user 'root'. This process serves no pages, but rather spawns child processes as needed/configured running as the user 'apache'. These child threads are responsible for serving pages and running modules/CGIs. Therefore, when a php script makes a call to exec() or system(), the apache child process runs the command (probably through the C function system()). As an example, if I called exec('ls .'), the ls program would be started with the uid 'apache', therefore being able to do anything the 'apache' user can. To answer your question, Everah, it won't matter that all php scripts stop when the server is restarting because it is already doing the desired function, ie restarting.

I am not too familiar with php-cgi, but I think that in that case php runs as the owner of the file. I could be wrong, but this might be a solution as you could change the user of the php file that restarts apache to 'root', but I would never recommend this.
User avatar
samtay
Forum Newbie
Posts: 9
Joined: Sun Aug 20, 2006 8:44 am
Location: Manchester, UK

Post by samtay »

mibocote wrote:To possible solution is to create a cron job run by root every minute that checks for the existance of a particular file and if it does exist it restarts httpd and deletes the file. Then you just create that file from php if you need apache restarted and wait at the most 1 minute.
The cron job idea sound good, does anyone have any examples?

Thanks
Sam
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Permissions will be the biggest issue here. The cron job would look like:

Code: Select all

#initialization goes here
*/1 * * * * root /home/user/checkApache.php
Although I'd recommend checking every ten minutes (*/10 * * * *), one minute seems like a lot.

The script would look like:

Code: Select all

<?php
if (!file_exists('restart.txt')) exit;
unlink('restart.txt');
shell_exec('/etc/init.d/httpd restart');
?>
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

The cronjob script:

Code: Select all

if [ -f testfile ]
then 
  /etc/init.d/httpd restart
 rm testfile
fi
Replace testfile with the absolute location of the file you will be generating from php.

Edit: I prefer Ambush Commander's solution, though I think he meant

Code: Select all

*/1 * * * * root php /home/user/checkApache.php
Last edited by mibocote on Sun Aug 20, 2006 12:15 pm, edited 3 times in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hey, don't forget to delete the file! ;-)
User avatar
samtay
Forum Newbie
Posts: 9
Joined: Sun Aug 20, 2006 8:44 am
Location: Manchester, UK

Post by samtay »

would it be better by using an MySQL db instead of a file?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

In my opinion it's overkill.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

exec('sudo /etc/init.d/httpd restart');
access issue solved..
Post Reply