简体   繁体   English

使用php exec()执行Linux命令并运行Shell脚本

[英]Executing a linux command with php exec() and running a shell script

i am trying to run this piece of php code on my server: 我试图在服务器上运行这段php代码:

<?php
$cmd = 'echo "this is a test" > /home/ubuntu/scripts/test_file';
echo exec($cmd);
?>

From my understanding it should add the piece of text to the file test_file . 据我了解,它应该将一段文本添加到文件test_file The file exists in the appropriate location and i have tried chmod 755 and chmod 777 on the php file. 该文件存在于适当的位置,我已经尝试了php文件上的chmod 755和chmod 777。 But i dont see the text being added to the text_file . 但是我看不到要添加到text_file的文本。 I tried running the linux command directly on the server and it works. 我尝试直接在服务器上运行linux命令,它可以工作。 Could some one tell me what i am doing wrong? 有人可以告诉我我在做什么错吗?

Also, i am trying to create a virtual host file on the server through a php script. 另外,我试图通过php脚本在服务器上创建虚拟主机文件。 Rather than running the commands through php exec() , i thought it would be better to run a shell script, with the shell script reading the required parameters from a text file and setting the directory path in the virtual host file. 我认为最好运行一个shell脚本,而不是通过php exec()运行命令,该shell脚本从文本文件中读取所需的参数并在虚拟主机文件中设置目录路径。 I am new to linux, is this a good approach or is there a better way in going about this? 我是linux新手,这是个好方法还是有更好的方法呢? All this is being done to setup a magento based site programatically. 所有这些都是通过编程来设置基于magento的站点。 Thanks. 谢谢。

Your code is OK. 您的代码正常。 The problem probably either lies with your php being in safe mode (though it's deprecated, see link) or with file/directory permissions. 问题可能出在您的php处于安全模式 (尽管已弃用,请参见链接)或文件/目录权限。

No need to give the file permissions 0777 since that makes the file executable, 0666 should suffice. 无需授予文件权限0777,因为它使文件可执行,因此0666就足够了。 It is not enough however for the file to have the right permissions, each directory on the path must be traversable. 但是,仅文件具有正确的权限是不够的,路径上的每个目录都必须是可遍历的。 Try a different directory to which the user with whose privileges the php code runs has access, /tmp is a good start. 尝试使用运行php代码特权的用户可以访问的其他目录, /tmp是一个好的开始。

General way to debug problems like this is to execute a different command which gives you extra information about the context in which echo is executed, eg 调试此类问题的一般方法是执行一条不同的命令,该命令为您提供有关执行echo的上下文的更多信息,例如

<?php
echo exec("id");
echo "<br/>";
echo exec("ls -l /home/ubuntu/scripts/test_file");
?>

(remember exec() only returns the last line of command's output, these display just one line though). (记住exec()仅返回命令输出的最后一行,尽管它们仅显示一行)。 These commands will tell you the user which runs the code and whether they can see the file at all. 这些命令将告诉您运行代码的用户,以及他们是否完全可以看到该文件。

As the comment already said: this is actually bad way to accomplish what you're trying to do, as writing Apache configuration based on user input through web could open you up to multiple issues. 正如评论已经说过的那样:这实际上是完成您要尝试的操作的不好方法,因为基于通过Web的用户输入编写Apache配置可能会使您面临多个问题。

What you might consider, is to have the PHP side write the required information to a file, or a database, which is then polled every now and then via a cron script or similar by a different process that does the actual configuration changes. 您可能考虑的是让PHP端将所需的信息写入文件或数据库,然后不时地通过cron脚本或类似程序通过实际配置更改的其他过程对其进行轮询。 This eliminates the need to exec() from PHP (which is always bad). 这消除了从PHP中执行exec()的需要(这总是很糟糕的)。 With this, your process that runs PHP wouldn't need to have write permissions to important system files. 这样,您运行PHP的进程就无需具有对重要系统文件的写许可权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM