There are some huge security headaches here, so you will need to be extremely careful. You can place the www-data user in the sudoers file and give the explicit path to the script you wish to run as root. It is imperative that you state only the script you wish to run as root, otherwise some miscreant could break out into your entire system.
and then add the following:
www-data ALL = NOPASSWD: /var/www/html/script/script.sh
You should be able to call the script like this:
If I'm honest, I'm not particularly keen on this. The www-data user should have as least permissions as possible. Although we have reduced the attack vector considerably by explicitly stating exactly what we want to execute as root, it may be worth shifting those
chown operations into root's crontab to run periodically, rather than be called by a script (if you actually need to chown those directories in such a way, which seems like another massive security hole too).