Tuesday, 7 May 2019

PHP script can't run bash script. sh: Permission denied


I'm trying to exec a .sh script from PHP, however it is not executing.


I checked the error logs, and I'm getting the 'sh: Permission denied' error. I checked under which user php is being run, and it's done under the apache user.


I tried changing the .sh's ownership to the apache user, but there is no result.


I thought at first this was because the script was outside the www/ dir, however even when I put the script in the same directory, the error is still being given.


Are there any solutions to this other than adding the apache user to the SUDOers list?


The sh script runs fine if I launch it from putty using the 'php filename.php' command.



Answer



Try the following suggestions:



  • Try to run below test command, and check whether it worked:

    • php -r "echo exec('whoami');"



  • Make sure that all parent directories and the files have at least r-x flag permissions:

    • chmod 755 dir; chmod 755 file



  • Make sure that the owner of the file is your Apache user.

    • Try also to add a +s flag (sudo) to the file (not recommended):

      • chmod u+s file,





  • Make sure that your PHP is not running in a safe_mode.

  • Make sure that the script is inside your Apache root:

    • Otherwise, move the script inside it,

    • or add that directory to your Apache configuration,

    • or add this directory to your include_path, e.g.:

      • php.ini file: include_path ".:/usr/local/lib/php:/your/dir"

      • or .htaccess file: php_value include_path ".:/usr/local/lib/php:/your/dir"





  • Check whether your shell is set to valid (e.g. /bin/sh) to your Apache user (e.g. check with: finger).

  • Make sure that your php.ini doesn't use: disable_functions for exec function

  • If using SELinux or having selinux-utils installed (a Security-enhanced Linux system), check getenforce/setenforce configuration as described in @Tonin answer.


Troubleshooting:



  • If you changed your php.ini or httpd.conf file, don't forget to restart the web server,

  • Check your Apache error log for additional details.

  • Enable in your php.ini all kind of errors (display_error, error_reporting, etc.).


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...