Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Why would you give Apache root permissions anyway? –  slybloty Aug 6 '12 at 14:06
    
I dont want to give root permission to apache but necessary permissions for apache to change the date. –  user1259132 Aug 6 '12 at 14:12
1  
While that may be possible, I wouldn't recommend it. Setting the date manually on a network-connected computer is highly unusual: normally it is set automatically by NTP. –  Gilles Aug 7 '12 at 1:08
add comment

1 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.

share|improve this answer
    
If you're running a system with SELinux (and if you're running a web server that has PHP on it, you really should be using SELinux), I bet you'll get an SELinux denial if php tries to exec sudo. –  jsbillings Aug 6 '12 at 17:30
1  
if you are using SELinux, you may need to use audit2allow to parse selinux audit logs and system logs to create the appropriate rules for SELinux. see thedumbterminal.co.uk/php/knowledgebase/article/131 for an example. –  Tim Kennedy Aug 6 '12 at 17:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.