Assuming your Apache user can run sudo
commands without entering a password (which, by the way, would be a huge security vulnerability, so please don't do it) you could do this:
exec("sudo hostname " . $hostname);
In order to make sure your www-user
can do this, edit /etc/sudoers
with the following line:
www-user ALL=NOPASSWD: /bin/hostname
Whatever you're trying to accomplish by changing the hostname, you'll probably find that you can do it another, safer, way. To anyone reading this, I thoroughly do not recommend you follow these instructions, you are opening your server up to potential harm by escalating the priveleges of the www-user
account.