如何通过shell_exec在php-apache docker容器中重新加载apache? [英] How to reload apache in php-apache docker container via shell_exec?

查看:122
本文介绍了如何通过shell_exec在php-apache docker容器中重新加载apache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了多个虚拟主机,需要重新加载apache以使虚拟主机可用,但是shell_exec('service apache2 reload')似乎在容器内不起作用.

I created multiple vhost and needed to reload the apache to make the vhost available, however shell_exec('service apache2 reload') didn't seem to work inside the container.

据我了解,php-apache(链接)容器在www-data用户下运行因此它没有触发sudo命令的权限.无论如何,shell_exec是否有一个sudo命令.

From my understanding is php-apache (link) container runs under www-data user therefore it doesn't have permission to trigger the sudo command. So is there anyway to shell_exec a sudo command.

仅供参考,这个问题与docker容器环境有关,而不是普通的Linux.基本上我可以在主机上的正常apache下执行所有这些命令,但是我想在docker容器中进行实验.最终,我会尝试所有其他sudo命令,例如a2ensite,a2dissite等...

FYI guys, this question is regarding to docker container environment not a normal Linux. Basically I can do all these commands under normal apache in the host machine, however I want to experiment it in docker container. Ultimately, I would try all the other sudo commands such as a2ensite, a2dissite, etc...

有什么想法吗?谢谢.

推荐答案

否,如果您的脚本在Apache下运行,则不能直接这样做.您的脚本没有足够的权限发出这样的命令.

No, you can't to this directly, if your script is running under Apache. Your script hasn't enough rights to make such a command.

无论如何,我认为通过Apache给脚本授予使用sudo的权限是一个[strong>非常危险的主意.

Anyway, I think that it's a very dangerous idea to give to your script the rights to use sudo, through Apache.

但是,您可以让数据库或服务器文件中包含信息.然后,让脚本通过超级用户的 crontab 重新加载Apache,例如,找到了.

But, you can let an information in your database or a server's file. And then, let a script to reload Apache via a superuser's crontab, by example, if the information is found.

示例代码:

<?php
if ($something_append) {
    // let an information in the server.
    touch('/srv/have_to_reload_apache') ;
}
?>

超级用户的cron :(可以是sh脚本或其他)

The superuser's cron : (could be a sh script or whatever)

sudo crontab -e

写:

*/5 * * * * php /path/to/sudo_script.php

这将每5分钟运行一次脚本. 该脚本可能是:

This will run the script every 5 minutes. The script could be :

<?php
if (file_exists('/srv/have_to_reload_apache')) {
    shell_exec('service apache2 reload'); // Reload apache
    unlink('/srv/have_to_reload_apache'); // Remove information
}
?>

这篇关于如何通过shell_exec在php-apache docker容器中重新加载apache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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