If you want to run the command through the file url, example link and in that test.php has your shell_exec, you need make sure Apache is allowed to execute this command.
To give permission to Apache just know the user, this will depend on the distribution of Linux you are using. If you are using CentOS it will be Apache even if you are using Ubuntu will be www-data .
After you know that, check the file path where the files you want to run the sudo permission by Apache, then you edit the sudoers
vim /etc/sudoers
and add the following line.
apache ALL=(root) NOPASSWD: /caminho/da/sua/pasta
or
www-data ALL=(root) NOPASSWD: /caminho/da/sua/pasta
You will basically be telling the server that your Apache is allowed to run the files in this folder as root .