0

How would I go about restarting Apache with a PHP script while keeping correct security measures on a production server? This is because restarting Apache requires the use of sudo or root privileges, both of which PHP don't normally have access to. So would it be possible to give said access, but also not open up a security whole in my server?

  • 1
    Why a PHP script needs to be able to restart Apache in the first place? This sounds like an XY problem. – Esa Jokinen Jul 30 at 5:05
  • The first problem is that you're trying to automate restarting Apache. That indicates you are having some other problem. You should solve the other problem instead. Also, deleting and reposting your question is not a good idea either. It will just confuse people. – Michael Hampton Jul 30 at 5:23
  • @EsaJokinen Im using PHP to change a config file in Apache, then i just need some way to restart Apache once file file change has happened. Whether thats PHP that restarts it, or PHP executing some other program that does the restart, idk. I'm just not sure on the direction I need to take. – Mr. Simmons Jul 30 at 6:42
  • @MichaelHampton I was hoping that phrasing the question better would help. I don't think I quite understood the explication of the XY problem. Sorry. – Mr. Simmons Jul 30 at 6:44
  • Maybe you could run a separate cron job (bash script or so) that looks for changes in the config file and then restarts Apache if necessary. It might be good to perform some syntax checks on the file, first. Still, giving a PHP script access to configuration files the web server uses may be a security risk in itself. Just be careful out there! – Esa Jokinen Jul 30 at 6:56
1

How would I go about restarting Apache with a PHP script while keeping correct security measures on a production server?

You can't or, even if you can, you really shouldn't.

... restarting Apache requires the use of sudo or root privileges, both of which PHP don't normally have access to.

Nor should it.
PHP Runs "within" the Apache environment and should not be mucking about with the configuration of that environment.

An analogy: You're a passenger on a commercial flight and you want to reconfigure the number of seats / pilots / engines / wings / tails on the plane while it is in flight.

So would it be possible to give said access, but also not open up a security whole in my server?

Short answer: No.

Question: What is the best way to prevent someone from shooting you in the foot? 
Answer:   Do not give them a gun. 

You mention in the comments that you want to reload the Apache process after making change to its configuration.

You should not be doing this in Production (except under strictly controlled circumstances).

You should not be doing this in Production manually (without a tried and tested way of restoring the previous, working configuration).

Any and all changes to your PHP / Apache / Operating System configuration(s) really should be thoroughly tested in a non-Production environment and applied through automated (and reversible) methods.

If you misconfigure Apache, your company's entire Web presence could go "Off the Air" and, if you did break things using this method, how would you fix it? No Apache means no PHP page with which to undo your change!

That's not a Good Place to find yourself.

| improve this answer | |
  • I wonder how on earth C-Panel does it then. Cause you can change the php.ini file on the fly and applying an update to the php.ini file requires an Apache restart. Cause that is what I'm trying to make. Some way to change the php.ini file in a web editor, then apply any changes made. – Mr. Simmons Jul 30 at 13:44
  • Does it allow you to, say, delete the entire contents of the php.ini file? Or to fill it with "stuff" that make no sense or settings that just can't work? Probably not, in which case it's applying some additional functionality to prevent you shooting yourself in the foot in this way. If you want to write your own version of this, then you'll need to do the same sort of thing. – Phill W. Jul 31 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.