Solved

Restarting Apache from PHP

Posted on 2006-11-17
5
352 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much!  I'll play around with those ideas.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now