Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
Is it a shell script or a PHP file? Your last paragraph isn't clear about that. Also, did you set execute permissions (x) on the file? Did you specify the script interpreter in a shebang line? –  Daniel Beck Jul 31 '12 at 18:14
It's a bash script to be ran from PHP. Yes I did make it an executable and I did specify the script interpreter. It works correctly when I execute the PHP script from putty and the bash script gets called and runs correctly. But if I run the php script from the webbrowser instead it fails to run the bash script and it will do this error since it's running as the apache user and not the user I use in putty. –  Robin Presto Jul 31 '12 at 19:54
Try chmod 775 yourscript.sh . That will give r-x (read and execute) permissions to "Other" users on that file. –  Rhyuk Jul 31 '12 at 20:00
I tried it. No luck.. I can't know the exact reason until tomorrow though. I don't have access to the logs from my location. I will get back to you guys. Thank you for your help. :) –  Robin Presto Jul 31 '12 at 20:19

2 Answers 2

up vote 3 down vote accepted

Try the following things:

  • try to run test command, if it works:
    • php -r "echo exec('whoami');"
  • all parent directories and the file have r-x:
    • chmod 755 dir; chmod 755 file,
  • make sure that owner of the file is your apache user
    • try also to add +s flag to the file:
      • chmod u+s file,
  • your PHP is not running in safe_mode
  • make sure that the script is inside your Apache root,
    • if not - move the script inside it,
    • or add that directory to your Apache configuration,
    • or add this directory to your include_path:
      • php.ini: include_path ".:/usr/local/lib/php:/your/dir"
      • or .htaccess: php_value include_path ".:/usr/local/lib/php:/your/dir"
  • check as well if giving proper shell (/bin/sh) to your apache user make any difference (check with: finger),
  • make sure that your php.ini doesn't use: disable_functions for exec function


  • if you change your php.ini or httpd.conf, don't forget to restart it,
  • check your Apache error log for additional details
  • enable in your php.ini all kind of errors (display_error, error_reporting, etc)
share|improve this answer
That was my problem.. the parent directory didn't have execution rights... it works now! Thank you! :) –  Robin Presto Aug 1 '12 at 7:11

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 
root@ls:~# /usr/sbin/setenforce Permissive
root@ls:~# /usr/sbin/getenforce 

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

share|improve this answer
Thank you for the detailed reply! Alas, like I mentioned I can't have root access until tomorrow. So I will get back to you too! :) And yes, I am using CentOS. –  Robin Presto Jul 31 '12 at 20:33
I loved your answer, very informative! Unfortunately I didn't pick yours because the enforce was disabled and wasn't the problem. Though I did learn a lot from your answer, so thank you. I will vote you up when I get enough reputation :) –  Robin Presto Aug 1 '12 at 7:13
No worries, glad to know you learned from my post! –  Tonin Aug 1 '12 at 12:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.