apache PHP可以重启Apache吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/425717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:06:56  来源:igfitidea点击:

Can PHP restart Apache?

phpapache

提问by Chris Burgess

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.

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

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?

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

Thanks for any help!!

谢谢你的帮助!!

回答by Chris Burgess

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

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

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.

最简单的方法是拥有一个文件 /tmp/RESTART_APACHE,PHP 可以创建该文件,并且 cron 脚本会检查该文件。如果 cron 脚本看到文件 /tmp/RESTART_APACHE,那么它会正确地重新启动 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.

使用 cron 脚本会引入延迟(如果您每分钟运行一次,则最多 60 秒),但除此之外应该可以按您的意愿工作。

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

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

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.

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

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.

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

回答by mark

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.

我的方法是在系统上创建一个新用户,赋予该用户 sudo 重新加载 apache 的权限,并从 Apache->PHP 我使用 SSH 到 localhost,并使用一个没有密码的授权密钥给该用户,发出命令。

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.

这样做的原因是我不想给 apache 用户(通常是 www-data)重新加载自己的能力。我将新用户命名为 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.

我不得不以 wwwctrl 用户身份手动执行一次此命令,以便将本地主机密钥添加到 ~wwwctrl/.ssh/known_hosts。

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

我使用 proc_open() 来观察命令的执行。

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@".

事实上,我正在为不同系统上的不同 Apache 安装生成一批虚拟主机,所以在每个系统上我都有这个 wwwctrl 用户重新加载 Apache,基本上是在“foreach hosts as host do ... wwwctrl@host@”中执行此操作。

回答by Christian Davén

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.

我会创建一个守护进程来监视启用站点的目录并在添加或修改文件时重新启动 Apache。然后您不必像 cron 作业那样等待 60 秒。

回答by Andrew Williams

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

at将能够做到这一点,不确定您是否可以安排到第二个,但我想这取决于实施

回答by Nick Stinemates

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

您不想通过“重新加载”而不是“重新启动”吗?

回答by BobbyShaftoe

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.

为此,您需要编辑 sudo 文件,然后执行系统上使用的重启命令,当然使用 sudo。如果您提供详细信息,我可以告诉您,但您甚至可以这样做吗?是托管的吗?不过,Cron 在这里可能是更好的选择。

回答by lewsid

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.

这有点违反了标准的命令链,因为 apache 调用 php,而不是相反。我支持 cron 建议。只需设置一个具有足够权限的 cron 作业来检查主机文件的更改,如果发现任何更改,则重新启动 apache。