Apache: Permission denied: exec of '/var/www/html/cgi-test/first.pl' failed
I see from your own answer that it was a SELinux permissions issue due to trying to run CGI scripts from within apache in a non-standard directory.
The proper way to solve the permissions issue while maintaining SELinux in 'enforcing' mode, and thus improving your server's security is to apply the proper context to the files in your custom CGI script directory. If it is to be a permanent directory, you should change the selinux policy to automatically create new files with the proper permissions.
You can check the selinux policy for the cgi-bin directory with the command:
$ semanage fcontext --list | grep cgi-bin
(...)
/var/www/[^/]*/cgi-bin(/.*)? all files system_u:object_r:httpd_sys_script_exec_t:s0
/var/www/html/[^/]*/cgi-bin(/.*)? all files system_u:object_r:httpd_sys_script_exec_t:s0
(...)
This means that every file created inside the standard cgi-bin directories for apache will be automatically given the SELinux type httpd_sys_script_exec_t
and be executable by httpd, so this is what your files in the cgi-test
directory should have as well.
NOTE: the examples shown below are based on CentOS/RHEL6, it should work just the same for RHEL7 with the eventual tweak.
Temporary solution
You can simply change your perl script's SELinux context with:
$ chcon -t httpd_sys_script_exec_t /var/www/html/cgi-test/first.pl
Check the file's SELinux attributes with ls -laZ
:
$ ls -laZ /var/www/html/cgi-test/first.pl
-rwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 /var/www/html/cgi-test/first.pl
However, if there's a SELinux relabel operation on this filesystem, the attributes will be reverted to the defaults and it will stop working again. It will also have to be done every time a new CGI script is added.
Definite solution
You can change the SELinux policy by adding a rule for your custom CGI directory and all contained subdirectories and files.
This is done via the semanage
command (available in the policycoreutils-python
RPM package):
$ semanage fcontext -a -t httpd_sys_script_exec_t "/var/www/html/cgi-test(/.*)?"
This will take a while to run. After changing the policy any new files created in your custom directory will have the new context. For the ones already there, you can apply the policy manually with:
$ restorecon -R -v /var/www/html/cgi-test
You can check your newly-added rule with:
$ semanage fcontext --list | grep cgi-test