遇到"sh:php:找不到命令"在Lambda中运行os.system模块时出错

问题描述:

我正在尝试使用python运行时在aws lambda中运行php脚本.我尝试使用python os.system模块调用php页面执行脚本.代码在下面给出

I'm trying to run a php script in aws lambda using python runtime. I tried using python os.system module to call the php page to execute the script. The code is given below

import os
def lambda_handler(event, context):
    os.system("php /home/admin/web/####/###.php")

在运行上述代码时,它显示错误:"sh:php:命令未找到".我对python脚本或shell脚本都不了解.因此,我们将不胜感激.

On running the above code, it displays error: "sh: php: command not found". I do not have much knowledge either in python scripting or shell scripting. So any help would be much appreciated.

更新:新代码

def lambda_handler(event, context):
    stream = os.popen("/usr/bin/php /home/admin/web/path/")
    output = stream.read()
    print(output) 

使用了新代码,但现在显示此错误:/bin/sh:/usr/bin/php74:没有这样的文件或目录.

Used the new code but its displaying this error now: /bin/sh: /usr/bin/php74: No such file or directory.

我不明白,因为php已安装到系统中,但它却表明不存在此类文件.

I don't understand, because php is installed into the system , but yet it is telling no such file exists.

您可以在默认的 Python Lambda运行时中运行PHP脚本.没有安装PHP.

You can not run PHP scripts in the default Python Lambda runtime. There is no PHP installed.

因此,您有三个选择:

  1. 将您的PHP代码迁移到Python.
  2. 使用Python和PHP创建Docker映像并将其用作Lambda运行时.此功能是最近添加的,您可以在文档中的描述,创建自定义Lambda运行时.

您的原始问题包含PHP脚本,你想跑步.它只包含一个非常简单的MySQL查询.将那一行PHP代码迁移到Python应该很容易.

Your original question contained the PHP script, that you want to run. It only contained a very trivial MySQL query. It should be very easy to migrate that one line of PHP code to Python.

PHP代码:

<?php
$con1 = mysqli_connect("endpoint","###","###","database");
if(mysqli_query($con1,"insert into ex_inbox(time) values ('Done')") or die("the eror ".mysqli_error($con1)));
?>

因此,选项#1是我建议的选项.您可以阅读如何使用Python

Therefore, option #1 is the one I'd recommend. You can read how to do insert into a MySQL database using Python in this guide.