[英]Restart apache2 via PHP script
I want to restart apache2 when I load a page with the following code: 当我使用以下代码加载页面时,我想重新启动apache2:
exec('/etc/init.d/apache2 reload', $output, $return);
if(!$return) {
$result = "<script>console.log('can not restart apache2');</script>";
echo $result;
echo $output;
} else {
$result = "<script>console.log('restart apache2 successfuly');</script>";
echo $result;
}
And in file etc/sudoers
I add this lines: 在文件
etc/sudoers
添加以下行:
Cmnd_Alias RESTART_APACHE = /etc/service apache2 restart
www-data ALL=NOPASSWD: RESTART_APACHE
But the result return can not restart apache2
. 但是返回结果
can not restart apache2
。
Am I do something wrong? 我做错什么了吗?
Apache服务可能没有权限自行重启
@tuanptit @tuanptit
<?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.
听起来您好像处在一个完整的开发环境中,因此您不必为此赋予特权。
But the best way the best way to handle this, IMHO, is to give the user that Apache runs under access to restart Apache via the sudo command . 但是,解决此问题的最佳方法的最好方法是恕我直言,这是通过sudo命令为用户提供Apache运行权限以重启Apache。
You'll want to edit your /etc/sudoers file and add lines similar to the following: 您将需要编辑/ etc / sudoers文件并添加类似于以下内容的行:
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. 您可能不需要
nobody
代替www-data
,这取决于运行Apache的用户。 On Debian, Apache typically runs under user www-data
, whereas under Red Hat, often Apache runs under user nobody. 在Debian上,Apache通常在用户
www-data
下运行,而在Red Hat下,Apache通常在nobody用户下运行。 Also, the /sbin/service apache2 restart
may need to be /sbin/service apache restart
or maybe /sbin/service httpd restart
. 另外,
/sbin/service apache2 restart
可能需要/sbin/service apache restart
或/sbin/service httpd restart
。 All depends on your system's configuration. 全部取决于您系统的配置。
Once that's done, in PHP you can use the code: 完成后,在PHP中可以使用以下代码:
exec('/sbin/service apache2 restart');
(Obviously changing that if the command to restart Apache differs on your server.) (显然,如果重新启动Apache的命令在您的服务器上不同,则进行更改。)
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. 如果执行此操作,则完全信任
sudo
二进制文件, service
二进制文件以及您的系统可以遵守规则,并且不会让Apache / PHP进程获取根shell。 I highly recommend asking on http://serverfault.com for the implications of what you're doing here. 我强烈建议您在http://serverfault.com上询问您在这里做什么的含义。
The solution already discussed here. 解决方案已在此处讨论。
How do you restart Apache with a (web) button click? 如何通过单击(Web)按钮重新启动Apache?
In the sudoers files you have "restart", in php file you have "reload" 在sudoers文件中,您具有“重新启动”,在php文件中,您具有“重新加载”
Check if you need to use /sbin/service instead of /etc/service 检查是否需要使用/ sbin / service而不是/ etc / service
Make sure the commands match in PHP and sudoer file 确保命令在PHP和sudoer文件中匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.