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
Solved

Restarting Apache from PHP

Posted on 2006-11-17
5
361 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
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). #…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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