try to change the permission of the logs directory to 777, as the php script is writing a file that means apache user is writing the file because php scripts run under apache user if suexec is not enabled.
the php script is running correctly on shell because from shell it runs under root user which can write any file any where.
ServerManagementAdmins -- Expert in Server Management, Server Hardening & Server Optimization. https://www.servermanagementadmins.com
Server Management Company
IM (skype): servermanagementadmins Email: sales[at]servermanagementadmins[dot]com