问题
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")
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.
By the way merry christmas to all of you.
Update: New code
def lambda_handler(event, context):
stream = os.popen("/usr/bin/php /home/admin/web/path/")
output = stream.read()
print(output)
Used the new code but its displaying this error now: /bin/sh: /usr/bin/php74: No such file or directory.
I don't understand, because php is installed into the system , but yet it is telling no such file exists.
回答1:
You can not run PHP scripts in the default Python Lambda runtime. There is no PHP installed.
So you have three options:
- Migrate your PHP code to Python.
- Create a Docker image with Python and PHP and use it as Lambda runtime. This feature was added recently, you can read about it this blog post.
- Create a custom Lambda runtime, as described in the documentation.
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.
The PHP code:
<?php
$con1 = mysqli_connect("endpoint","###","###","database");
if(mysqli_query($con1,"insert into ex_inbox(time) values ('Done')") or die("the eror ".mysqli_error($con1)));
?>
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.
回答2:
Please specify the full path!!!
for example:
The actual PHP path should be based on your own
/####/####/php/72/bin/php /home/admin/web/####/###.php
My Python code
import os
def main():
stream = os.popen("C:/Users/Administrator/Desktop/php/xxx/xxx/php/php7.2.9nts/php C:/xxx/xxx/WWW/test/test1.php")
output = stream.read()
print(output) # Successful and effective execution
if __name__ == '__main__':
main()
来源:https://stackoverflow.com/questions/65445455/encountered-sh-php-command-not-found-error-while-running-os-system-module-in