tags:

views:

1920

answers:

15

I have a local server which needs to make changes to a virtual hosts apache config file and then restart apache so the new config takes effect.

Can PHP do this? I tried passthru and exec but they didn't work. Maybe the problem is that I'm trying to restart PHP's parent process?

Thanks for any help!!

A: 

Wouldn't you want to pass a 'reload' instead of a 'restart?'

Nick Stinemates
A: 

To do this you would need to edit the sudo file and then execute the restart command that is used on your system, using sudo of course. If you give details, I could tell you but do you even have access to do that? Is it hosted? Cron would probably be a better choice here though.

BobbyShaftoe
A: 

Probably :-)

Seriously, I'm not sure how to do this... I need the config changes to take effect. Can reload do that? If so, can you tell me how to do it through PHP?

Please use the comments to each answer for chat. Since this reply isn't actually an answer.
Ólafur Waage
You need a reputation of 50 before the site permits you to add comments.
Peter Boughton
He can comment to his own post instead
Click Upvote
+7  A: 

I've used a cron script (written in PHP, not executed from the webserver) to check a server is up and restart the server.

However, I wouldn't do this from a server-created process, because you know you're about to kill the parent process, which has bad implications for the child.

The simplest method would be to have a file /tmp/RESTART_APACHE which PHP can create, and which the cron script checks for. If the cron script sees the file /tmp/RESTART_APACHE then it does a proper restart of Apache.

Using a cron script will introduce a delay (up to 60s if you run it each minute), but apart from that should work as you want.

Depending on how you intend using this, that may do the trick.

(You probably want to use a different directory than /tmp/ to set permissions and prevent anyone on the server being able to create the file.)

EDIT: Please see Aaron H's comment to this post. I agree with what he says: you really do want to be careful that the ability to restart your webserver is not a service generally available to the public.

Restrict access to the system which can trigger the restart; ensure that the file which triggers the restart has restrictive permissions so only the web process can create that file, and generally be smart.

Chris Burgess
It's important to add that this is a very bad idea if the service (PHP) is at all available on the outside... Something that restarts your web services should only be used by administrators, otherwise you open the door to a whole host of oportunities to have your server compromised.
Aaron H.
You didn't say how exactly this restart would be done? what code needs to be executed
Click Upvote
@Java PHP - if you're just trying to reload the config, you only need to run "apachectl graceful" (or "apache2ctl graceful")
Chris Burgess
A: 

I have full control over the server and can make any changes needed.

A: 

This sorta thing violates the standard chain of command since apache invokes php, not the other way around. I second the cron suggestion. Just set a cron job with sufficient privileges to check for changes to the host file, and restart apache if any are found.

lewsid
A: 

It would be awesome if it could be an instant reload...but I guess I could wait a minute.

A: 

Is there a way to schedule a job for a few seconds in the future?

You can use at, which could schedule a job for the next minute.But it usually gets run by cron anyway :)
Chris Burgess
+1  A: 

at will be able to do that, not sure if you can schedule down to the second but I guess that depends on the implementation

Andrew Williams
at is run from cron usually, so this won't be any faster than running cron will it?
Chris Burgess
A: 

re at

  1. it runs each minute, so has the same 60s delay
  2. is triggered by cron on most servers, so won't be any faster than using cron
  3. but it would avoid your script having to fire up at all and check for the existence of the lockfile
Chris Burgess
A: 

I'd assume that if your script has got shell access, you could use the exec() function to run a command which restarts apache or the server.

Click Upvote
he already said he'd tried that in the question
Chris Burgess
A: 

you can try editing /etc/sudoers and give whoever is running the apache process privelages to your apache init script... but this question is totally blowing my mind right now. i am curious to how and why..... if you are trying to restart apache through php.. something tells me that whatever goal you are trying to achieve, you are probably doing it the wrong way. please post back and tell us what happened, im really curious about this now.

+6  A: 

I've done this for the very exactly thing. However it was solely for a development environment, to quickly create virtual host for our developers on demand. Worked very pleasing well so far.

My approach was to create a new user on the system, give this user sudo rights to reload apache and from Apache->PHP I used SSH to localhost with an authorized key without passphrase to that user, issuing the command.

The reason for this was that I didn't wanted to give the apache user (usually www-data) the power in general to reload itself. I named the new user wwwctrl.

The command I used was:

ssh -i /path/to/key-file wwwctrl@localhost sudo /etc/init.d/apache2 reload

I had to execute this command manually one time as wwwctrl user to have the local host key being added to ~wwwctrl/.ssh/known_hosts.

I used proc_open() to watch the execution of the command.

In fact I was generating a batch of virtual hosts for different Apache installations on different systems so on every system I had this wwwctrl user to reload Apache, basically doing this in a "foreach hosts as host do ... wwwctrl@host@".

mark
A: 

with shell_exec you can( but php would have to be running as super user, something that can be acomplished with suphp )

DFectuoso
again, i'm pretty sure he said he'd tried that approach in the question abovethis probably doesn't work because it either kills the parent process midway thru (apachectl restart) or waits for all current requests to finish - which never happens, because it's waiting for itself (apachectl graceful)
Chris Burgess
A: 

I would create a daemon to monitor the sites-enabled directory and restart Apache when files are added or modified. Then you don't have to wait up to 60 seconds as with a cron job.

Christian Davén