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

Such an issue might depend on the OS you use and how it is configured. Some linux distros (mainly those based on RHEL like CentOS or Fedora) come with SELinux activated by default. This can be checked, and temporarily changed, with the following commands:

root@ls:~# /usr/sbin/getenforce 
Enforcing
root@ls:~# /usr/sbin/setenforce Permissive
root@ls:~# /usr/sbin/getenforce 
Permissive

You can also have a more complete view on the current configuration with:

root@ls:~# /usr/sbin/sestatus 
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   permissive
Mode from config file:          enforcing
Policy version:                 21
Policy from config file:        targeted

This change can be made permanent by editing the /etc/selinux/config file and set the SELINUX variable to permissive or disabled.

But, the correct way to solve this kind of issue, if you are indeed in this situation, is to check the /var/log/audit/audit.log log file. It will contain all the events related to SELinux rules. You'll then probably should give your script the correct context, i.e. being authorized to be run by the apache/php user. Checking SELinux security context is done with ls -Z:

root@ls:~# ls -alZ /var/www/cgi-bin/
drwxr-xr-x  root root system_u:object_r:httpd_sys_script_exec_t .
drwxr-xr-x  root root system_u:object_r:httpd_sys_content_t ..

This list the User, the Role and the Type of each file/directory. Here the httpd_sys_script_exec_t type gives the files in the cgi directory the permission to be executed by httpd. Your shell script should probably have the same type.

You can also feed the audit.log lines to the audit2allow command. It will output you the changes needed to make SELinux happy. But usually the changes suggested need to be done on the SELinux policy itself which is not what you should do in your case (still, this output can give some clue at what is going on).

The following page describe a similar issue and different ways to solve it: http://sheltren.com/stop-disabling-selinux


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.).