如何通过单击(Web)按钮来重新启动 Apache?

发布于 2024-12-01 04:58:34 字数 126 浏览 1 评论 0原文

我正在摆弄我的虚拟机,我正在开发的代码要求我每次都重新启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸵鸟症 2024-12-08 04:58:34

正如 Marc B 所说,您需要 root 权限才能重新启动 Apache。恕我直言,处理此问题的最佳方法是为 Apache 运行的用户提供通过 sudo命令

您需要编辑 /etc/sudoers 文件并添加类似于以下内容的行:

Cmnd_Alias      RESTART_APACHE = /sbin/service apache2 restart

www-data ALL=NOPASSWD: RESTART_APACHE

您可能需要 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 中使用以下代码:(

exec('/sbin/service apache2 restart');

如果重新启动 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:

Cmnd_Alias      RESTART_APACHE = /sbin/service apache2 restart

www-data ALL=NOPASSWD: RESTART_APACHE

You may need nobody instead of www-data, it depends on the user which Apache runs under. On Debian, Apache typically runs under user www-data, whereas under Red Hat, often Apache runs under user nobody. 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:

exec('/sbin/service apache2 restart');

(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, the service 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.

夏见 2024-12-08 04:58:34
<?php echo shell_exec('service httpd restart &'); ?>

不过,尝试执行此操作的此类脚本可能会遇到权限问题。不过,听起来您正处于一个全面的开发环境中,因此您授予它更高的权限应该不重要。

<?php echo shell_exec('service httpd restart &'); ?>

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.

渡你暖光 2024-12-08 04:58:34

文件包含 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文