用php + nginx执行shell脚本 [英] execute shell script with php + nginx

查看:404
本文介绍了用php + nginx执行shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我想在php Web应用程序上使用exec_shell函数重新加载nginx,如下所示:

I want to reload my nginx after some scenarios on my php web application with exec_shell function in php like this :

shell_exec('sudo sh /var/www/camva/subdomain.sh');

此代码在每次请求重新加载我的route.php文件中的nginx之后运行.

This code run after every request for reloading nginx on my route.php file.

我的subdomain.sh shell文件还包含:

Also my subdomain.sh shell file contains :

#!/bin/bash
sudo /etc/init.d/nginx restart

但是我在每次请求后都给出此回复:

But I give this response after every request :

Reloading nginx ... fail !

我不知道为什么会发生这种情况,但是我可以使用与"sudo /etc/init.d/nginx restart"相同的终端命令在我的根目录中运行此命令,并给出有关重新加载nginx的正确响应!

I don't know why this scenario happened but I can run this command in my root with terminal command same as "sudo /etc/init.d/nginx restart" and give correct response about reloading nginx !

感谢您的帮助.

推荐答案

为了猜测,您正在尝试使用可能没有root访问权限的帐户以root用户身份执行Shell脚本. Nginx 应该以没有超级用户访问权限的用户身份运行.您可以尝试做的是相应地chmod脚本文件的权限,以便即使另一个用户激活它,它也可以以root用户身份运行.

To haphazard a guess, you're trying to execute a shell script as root using an account that probably doesn't have root access. Nginx should be running as a user that does not have root access. What you can try to do is to chmod the permissions on the script file accordingly so that it runs as root even when another user activates it.

首先让root用户成为您要运行的脚本的所有者(subdomain.sh). 在CLI中而不是在脚本中运行这些命令.

First make root the owner of the script you want to run (subdomain.sh). Run these commands in CLI, not in your script.

sudo chown root.root /var/www/camva/subdomain.sh

然后将脚本文件设置为所有人可执行文件,并且仅对root用户可写.

Then make the script file executable by all and writable only for root.

sudo chmod 4755 /var/www/camva/subdomain.sh

现在,nginx用户应该能够执行脚本,但不能更改文件内容.

Now the nginx user should be able to execute the script but not change the file content.

也就是说,我想知道为什么您需要重新加载服务器,并且也许有更好的解决方案来解决您要解决的总体问题.

That said, I'm wondering why you need to reload the server and if, perhaps, there's a better solution to the overarching problem you are trying to solve.

我将指出,将您的nginx用户添加到sudoers文件中绝对不是正确的答案,并且会带来安全隐患.

I will point out that adding your nginx user to the sudoers file is absolutely not the right answer and poses security concerns.

这篇关于用php + nginx执行shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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