Solved

Restarting Apache from PHP

Posted on 2006-11-17
5
358 Views
Last Modified: 2010-03-04
Hello,

I'm looking for a way to restart my Apache server from PHP.  I run Apache 2 from Windows with PHP 5.
0
Comment
Question by:jtgraphic
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
rhickmott earned 500 total points
ID: 17977990
You should be able to use shell_exec() or system() or similar command call to execute the command

c:\www\apache2\bin\apache -k restart

You might need to give the Apache Service extra privileged see if it works first.
0
 

Author Comment

by:jtgraphic
ID: 17981716
I'm not where I can do that right now, but I figured it would be something like that.  I just couldn't find the command ANYWHERE.  Thanks for the heads up.  I'll let you know how it goes.
0
 

Author Comment

by:jtgraphic
ID: 17996959
I actually ended up restarting the services in a .bat file and executing the .bat file from the shell_exec command.  I think you've earned the points, but as a final question: do you think there is a graceful way to error handle/execute the script when apache goes down?
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 17997092
Good old batch files. I had to d something similar to remote restart a machine via the web.

Some untested ideas how any of them will work practically though.

Ideally you want the script to complete before the server shuts down. Shell Exec is a difficult one though as it will hold the server up until the batch has has executed ( Basically until the command it has run and the dos windows has been terminated )

What you need is some sort of delay maybe 3 seconds or so before the command is triggered to allow the script to complete. Maybe setting up another batch file and calling a CALL BATCH2.BAT which sleeps for a few minutes then runs the command might work. If this was me I would write maybe a VB application as normally when you run them from the command line they run in windows and then halt in the command line which will probably allow it to continue. This should to allow PHP to continue on its merry way and then execute the restart 10-15 seconds later. Thats a little more challenging though.

One thing you could try would be to look at the different switches that Apache supports when restarting.

http://httpd.apache.org/docs/2.0/stopping.html

apachectl -k graceful
The USR1 or graceful signal causes the parent process to advise the children to exit after their current request (or to exit immediately if they're not serving anything). The parent re-reads its configuration files and re-opens its log files. As each child dies off the parent replaces it with a child from the new generation of the configuration, which begins serving new requests immediately.

Running the graceful switch *may* force the server to flush the script and allow it to end before hand but without testing I couldn't be sure.
0
 

Author Comment

by:jtgraphic
ID: 17997135
Thank you very much!  I'll play around with those ideas.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question