PHP可以重启Apache吗? [英] Can PHP restart Apache?

查看:40
本文介绍了PHP可以重启Apache吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地服务器,需要对虚拟主机的 apache 配置文件进行更改,然后重新启动 apache 以使新配置生效.

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.

PHP 能做到这一点吗?我试过 passthru 和 exec 但它们没有用.也许问题是我正在尝试重新启动 PHP 的父进程?

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?

感谢您的帮助!!

推荐答案

我使用了一个 cron 脚本(用 PHP 编写,不是从网络服务器执行)来检查服务器是否启动并重新启动服务器.

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.

最简单的方法是拥有一个文件/tmp/RESTART_APACHE,PHP 可以创建该文件,并且 cron 脚本会检查该文件.如果 cron 脚本看到文件/tmp/RESTART_APACHE,那么它会正确重启 Apache.

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.

使用 cron 脚本会导致延迟(如果您每分钟运行一次,则最多 60 秒),但除此之外,您应该可以正常工作.

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.

(您可能希望使用与/tmp/不同的目录来设置权限并防止服务器上的任何人能够创建该文件.)

(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.)

请参阅 Aaron H 对这篇文章的评论.我同意他所说的:您确实要小心,重新启动网络服务器的能力不是公众普遍可用的服务.

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.

限制对可以触发重启的系统的访问;确保触发重启的文件具有限制性权限,以便只有 Web 进程可以创建该文件,并且通常是智能的.

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.

这篇关于PHP可以重启Apache吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆