restarting apache within php script on linux
Moderator: General Moderators
restarting apache within php script on linux
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
Thanks
Sam Taylor
-
blackbeard
- Forum Contributor
- Posts: 123
- Joined: Thu Aug 03, 2006 6:20 pm
Try using the exec function.
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.
Code: Select all
exec ("/etc/init.d/apache restart");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.blackbeard wrote:Try using the exec function.
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.Code: Select all
exec ("/etc/init.d/apache restart");
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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).Everah wrote:system() perhaps?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
The cron job idea sound good, does anyone have any examples?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.
Thanks
Sam
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Permissions will be the biggest issue here. The cron job would look like:
Although I'd recommend checking every ten minutes (*/10 * * * *), one minute seems like a lot.
The script would look like:
Code: Select all
#initialization goes here
*/1 * * * * root /home/user/checkApache.phpThe script would look like:
Code: Select all
<?php
if (!file_exists('restart.txt')) exit;
unlink('restart.txt');
shell_exec('/etc/init.d/httpd restart');
?>The cronjob script:
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
if [ -f testfile ]
then
/etc/init.d/httpd restart
rm testfile
fiEdit: 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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Code: Select all
exec('sudo /etc/init.d/httpd restart');