1

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.

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    This probably means PHP isn't installed in your runtime environment – robinsax Dec 25 '20 at 05:11
  • it is installed in my ec2 environment – husaindevelop Dec 25 '20 at 05:17
  • That error raises when `php` is not installed. Have you tried to run a `php` file without using python script ? – Brad Figueroa Dec 25 '20 at 05:20
  • I cannot run php script in aws lambda as there is no php runtime, i was trying to bypass it by either using python subprocess or os.system. Regarding php file, yes i have run it on browser and its working. – husaindevelop Dec 25 '20 at 05:27
  • 2
    This seems very similar to your [other question](https://stackoverflow.com/questions/65437332/) (to the point where it may be considered a duplicate). We had made some progress there already, please consider following up there instead of creating a very similar question. The core of the problem seems to be that you want to call an executable that is not present in AWS Lambda. You can't work around that with Python - you need to either supply a custom runtime with PHP and Python or build your Lambda from a Docker Image. – Maurice Dec 25 '20 at 11:51

2 Answers2

1

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

So you have three options:

  1. Migrate your PHP code to Python.
  2. 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.
  3. 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.

Jens
  • 20,533
  • 11
  • 60
  • 86
  • It is a very bad idea to use `die(mysqli_error($$conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Dec 29 '20 at 18:03
0

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()
Davis
  • 96
  • 5
  • Add the php path to the system environment variable, you do not need to specify the full path – Davis Dec 25 '20 at 07:07
  • Tried the code with paths changed to mine, but its displaying error "/bin/sh: /usr/bin/php: No such file or directory" in aws lambda. – husaindevelop Dec 25 '20 at 08:37
  • You need to determine whether php is successfully installed and locate the php installation path. How did you install php? – Davis Dec 26 '20 at 05:50
  • Ok, php was installed using ssh > aws cli via putty client. Whether it is successfully installed, yes its working, whole of my applications are currently online executing php commands. Regarding php installation path, may be if i'm right, it is in /usr/bin/php74/ – husaindevelop Dec 29 '20 at 05:16
  • Sorry, I am not very familiar with aws lambda, I hope the following article can help you, if you can't solve the problem in the end, can you consider the way of http request api and php interaction [https://forums.aws.amazon.com/thread.jspa?threadID=326841](https://forums.aws.amazon.com/thread.jspa?threadID=326841) [https://www.saltycrane.com/blog/2011/04/how-use-bash-shell-python-subprocess-instead-binsh/](https://www.saltycrane.com/blog/2011/04/how-use-bash-shell-python-subprocess-instead-binsh/) – Davis Dec 30 '20 at 02:10