Change server date using PHP

date

Is this possible to change Linux server date using php code?

Is there any way to give necessary permissions to Apache to do this?

What is the procedure to give root permissions to Apache?

Best Answer

You can do that with sudo, without giving "root" permissions to apache.

apache <hostname> = (root) NOPASSWD: /bin/date

Which in English translates roughly to:

Let the apache user run the /bin/date command as the root user without requiring a password on server <hostname>.

Using sudo, (or doing the equivalent in any RBAC model) allows you to expose only the privileges you need to, to specific users, to accomplish your goals.

Then in PHP, just pass the sudo in along with the command:

exec("sudo -su /bin/date <newdate>");

Also, if that's not working for you, check your /etc/sudoers file for requiretty:

Defaults        requiretty

You'll need to comment that out, for sudo to work with apache.

Related Question