如何通过单击(Web)按钮来重新启动 Apache?
我正在摆弄我的虚拟机,我正在开发的代码要求我每次都重新启动 apache 以捕获更改。我认为如果有一个小书签、链接或按钮,我可以运行来执行此操作,那就太好了。关于如何在 CentOS 5 开发虚拟机上使用 PHP 完成此任务有什么想法吗?
I'm playing around with my VM and the code I'm developing requires that I restart apache every time to capture the changes. I thought it would be nice to just have a bookmarklet, or link or button click I could run to do this. Any thoughts on how to accomplish this with PHP on a CentOS 5 dev VM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Marc B 所说,您需要 root 权限才能重新启动 Apache。恕我直言,处理此问题的最佳方法是为 Apache 运行的用户提供通过
sudo命令
。
您需要编辑
/etc/sudoers
文件并添加类似于以下内容的行:您可能需要
nobody
而不是www-data
,这取决于 Apache 运行的用户。在 Debian 上,Apache 通常在用户www-data
下运行,而在 Red Hat 下,Apache 通常在用户nobody
下运行。另外,/sbin/service apache2 restart
可能需要是/sbin/service apache restart
或/sbin/service httpd restart
。一切都取决于您的系统配置。完成后,您可以在 PHP 中使用以下代码:(
如果重新启动 Apache 的命令在您的服务器上不同,则显然需要更改该代码。)
请注意:这很可能被视为安全风险!如果您这样做,您就完全信任
sudo
二进制文件、service
二进制文件以及您的系统遵守规则,不会让 Apache/PHP 进程获取 root shell。我强烈建议您在 http://serverfault.com 上询问您在这里所做的事情的影响。As Marc B said, you need root privs to be able to restart Apache. The best way to handle this, IMHO, is to give the user that Apache runs under access to restart Apache via the
sudo
command.You'll want to edit your
/etc/sudoers
file and add lines similar to the following:You may need
nobody
instead ofwww-data
, it depends on the user which Apache runs under. On Debian, Apache typically runs under userwww-data
, whereas under Red Hat, often Apache runs under usernobody
. Also, the/sbin/service apache2 restart
may need to be/sbin/service apache restart
or maybe/sbin/service httpd restart
. All depends on your system's configuration.Once that's done, in PHP you can use the code:
(Obviously changing that if the command to restart Apache differs on your server.)
Please note: this could very well be considered a security risk! If you do this, you fully trust the
sudo
binary, theservice
binary, and your system to obey the rules and not let an Apache/PHP process get a root shell. I highly recommend asking on http://serverfault.com for the implications of what you're doing here.不过,尝试执行此操作的此类脚本可能会遇到权限问题。不过,听起来您正处于一个全面的开发环境中,因此您授予它更高的权限应该不重要。
You might have permissions problems with such a script attempting to do this though. It sounds like you're in a full-on dev environment though, so it shouldn't matter for you to give elevated privileges to it.
文件包含 apache 的进程 ID。只需向其发送一个 HUP 即可。可以编写一个简单的脚本 (sh) 来实现此目的 - 并将其放置在 cgi-bin 目录中。
A file contains the process ID for apache. Just send a HUP to it. A simple script (sh) could be written to achieve this - and place it in the cgi-bin directory.