Solved

Restarting Apache from PHP

Posted on 2006-11-17
5
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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