Python script through php on Amazon EC2

后端 未结 2 1659
小鲜肉
小鲜肉 2021-01-21 20:57

I am trying to run a simple python script through php on Amazon EC2 instance.

The file is working fine on terminal i.e am getting the output. But when am trying the same

相关标签:
2条回答
  • 2021-01-21 20:58

    I guess first place one should look is the apache error log at. You don't want to enable sudo. Please consider checking the error log of apache to understand what is the underlying problem at:

    /var/log/apache or similar directory

    Please review these posts for a better solution and review of the problem:

    How to execute shell_exec on EC2 Linux

    https://forums.aws.amazon.com/thread.jspa?messageID=692394

    php shell_exec() command is not working

    0 讨论(0)
  • 2021-01-21 21:16

    After a lot of "scratching my head", I finally figured it out.

    First of all you one need to figure out the current user who is executing the php. One can either check out php.info file or use

    $processUser = posix_getpwuid(posix_geteuid());
    print $processUser['name'];
    

    This will give you the user who is executing the code. In my case it was apache rather than www-data (I shouldn't have assumed so in first place).

    After that you will need to edit the sudoers file (etc/sudoers)

    Add the lines over there.

    apache ALL=NOPASSWD:/var/www/myfile.py
    apache ALL=NOPASSWD:/usr/bin/pythondestination
    

    or you can simply add

    apache ALL=(ALL)        NOPASSWD:ALL
    

    (You probably should just specify the path).

    Then execute the script through php.

    0 讨论(0)
提交回复
热议问题