How to change the permissions on the system directory Linux using PHP (to execute commands as root)?

Hello !
Faced with such a task - you need using PHP to work with the filesystem of all Linux servers (a server for experiments is in a home LAN, behind a firewall and sharpened for specific needs). Installed CentOS 6.7 Final, Apache 2, php works as a module.

To solve the problem on PHP.

At the moment it is necessary to work with files in the /etc/ directory - you need to change the owner and rights of some directories, to copy, backup, and so on and then again to bring back the original law.

So - the problem is that Apache allows you to execute commands in php from Ruta:

shell_exec('chmod-R 777 /etc/catlib/'); - said not enough rights, music permission and so on.

Scripts are run as the Apache user (echo exec('whoami');)
Running the web server root, add Apache to the group root does not help.

I remembered the SUDO, do the following:

opened: /etc/sudoers
added: apache ALL=NOPASSWD: /path/to/script

and yet it is impossible to change the permissions with the command: shell_exec(' sudo chmod-R 777 /etc/catlib/');

Actually a question - how to solve ?
Don't want to do SCHOOL connection in the script itself and change the law. What to change in the system to give php root for everything ? Maybe Sudo is not the same manual prescribes ?

Thanks for the reply !
July 8th 19 at 16:14
3 answers
July 8th 19 at 16:16
And rightly so, because it's not just the crutch and the crutch of mega and cross to safety.
I can recommend you to raise a demon on a separate port that is already smoit to work from Ruta and Apache will have to manage it
July 8th 19 at 16:18
I'd have a php script as root run, and all business.
user apache/www-data in sudoers to add is very good.bad idea...
not suitable, already thought of that. - Destiny.Wintheis commented on July 8th 19 at 16:21
July 8th 19 at 16:20
1.
shell_exec('sudo /path/to/script');
cat /path/to/script:
#!/bin/bash
chmod-R 777 /etc/catlib/

2. to put incron to create the PHP file to write to create the file run the script

Find more questions by tags LinuxApachePHP