如何使用PHP执行centos命令行srcipt? [英] How to execute centos command line srcipt using PHP?

查看:299
本文介绍了如何使用PHP执行centos命令行srcipt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用诸如"shutdown -h now"之类的命令在centos中执行一些命令行脚本,...
我具有root特权的用户名和密码,但是当我使用浏览器运行php文件时,它不起作用.
这是我的PHP代码:

I want to excute some command line script in centos using, for example "shutdown -h now",...
I have username and password for root privilege but when I use browsers to run php file, it not work.
Here is my PHP code:

<?php
   shell_exec('/sbin/shutdown -r now');
?>



我还找到了一些有关此的指南:
php-使用shell_exec()重新启动服务器吗? -堆栈溢出 [ ^ ]
但是我想为什么要在拥有root用户名并通过的情况下使用它?而且我找不到/etc/sudoer目录.
请帮我.谢谢

我尝试过的事情:

使用PHP执行Centos命令行,例如重新启动计算机,...



I also find some guide, about like this:
php - Use shell_exec() to restart server? - Stack Overflow[^]
but I think why use this while I have root username and pass? And I not found /etc/sudoer directory.
Please help me. Thanks

What I have tried:

Execute Centos command line srcipt using PHP, such as, reboot machine,...

推荐答案

Quote:

,但我想为什么在我具有root用户名并通过的情况下使用它?

but I think why use this while I have root username and pass?

您具有密码,但必须在需要时输入密码.当Web服务器执行脚本时,该怎么做?

You have the password but it has to be entered when required. How do want to do that when the web server executes a script?

报价:

我找不到/etc/sudoer目录

And I not found /etc/sudoer directory

它不是目录,而是文件.并且您应该使用visudo来编辑该文件,而不是使用编辑器.


Web服务器将不会以 root 身份运行脚本.在大多数情况下,PHP脚本以运行服务器的用户身份运行(例如 wwwrun apache ).

当使用shell_exec执行运行脚本的用户所不允许的命令时,您必须以另一个用户(在本例中为 root )执行该命令.

可以使用sudo( 43.1.4.3来完成.2.sudo命令 [ ^ ]).

但是sudo会提示您输入一个密码,当从Web服务器启动的脚本中调用该密码时,密码将不起作用.解决方案是允许特定用户使用/etc/sudoers 文件(使用visudo编辑该文件)执行spefic命令.

一个示例条目可能看起来像

It is not a directory but a file. And you should use visudo to edit that file instead of using an editor.


The web server will not run the script as root. In most cases the PHP script is run as the user that runs the server (e.g. wwwrun or apache).

When using shell_exec to execute commands that are not allowed for the user running the script, you have to execute that command as another user (root in your case).

This can be done using sudo (43.1.4.3.2. The sudo Command[^]).

But sudo will prompt for a password which will not work when called from a script started by the web server. The solution is to allow specific users executing spefic commands using the /etc/sudoers file (use visudo to edit that file).

An example entry might look like

wwwrun ALL=(root) NOPASSWD: /sbin/shutdown



但是更好的解决方案是创建一个专用脚本.这将确保只能将此脚本作为root用户执行,因为给Web服务器帐户各种命令的root用户权限是一个坏主意.

移至要存储脚本的文件夹(例如/usr/local/bin ),然后使用喜欢的编辑器创建脚本(在此处命名为 reboot.sh ).根目录:



But a better solution would be creating a dedicated script. This will ensure that only this script can be executed as root because it is a bad idea to give the web server account root privileges for various commands.

Move to a folder where you want to store the script (e.g. /usr/local/bin) and create the script (named reboot.sh here) using your favorite editor as root:

#!/bin/bash
/sbin/shutdown -r now


使脚本可执行:


Make the script executable:

sudo chmod u+x reboot.sh


现在编辑sudoers文件并允许执行脚本:


Now edit the sudoers file and allow the script to be executed:

sudo visudo


在底部添加此行(假设PHP脚本以用户 wwwrun 的身份执行)


Add this line at the bottom (assuming the PHP script is executed as user wwwrun)

wwwrun ALL=(root) NOPASSWD: /usr/local/bin/reboot.sh


然后使用sudo
调用此脚本


Then call this script using sudo

shell_exec('sudo /usr/local/bin/reboot.sh');


这篇关于如何使用PHP执行centos命令行srcipt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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