Page 1 of 1

CHMOD in PHP not work arrgh HELP!

Posted: Thu Nov 07, 2013 1:03 am
by laughingbuddha
Hi all,

I'm having real head busting issues with chmod command running in a PHP script.

I've just installed a fresh VPS, no Plesk, just an LAMP install using CentOS 6 64bit. I'm using SFTP instead of an FTP server, and create a user for the SFTP and added it to the apache group.

Site uploaded and works fine, but when I use a PHP script with a chmod command in it like so:

Code: Select all

define('ABS_PATH', rtrim( dirname(__FILE__), '/' ) . '/');
chmod(ABS_PATH . 'media/cache/', 0777);
The command fails with the following error:

Code: Select all

Warning: chmod(): Operation not permitted in /var/www/html/test.php on line 2
BUT...if I change the owner of the folder in question to apache, the chmod command works.

Now my frustration is, on a Plesk server I run this works without a problem. So I'm scratching what's left of the hair trying to work out why it runs fine on a Plesk server, yet not on my LAMP server even though the apache user and my SFTP user are both in the same group.

Can anyone shed some light on this?

Re: CHMOD in PHP not work arrgh HELP!

Posted: Thu Nov 07, 2013 6:02 am
by prupert
Have you set the correct group owner for this file?
Have you set write permissions for the group on this file?

Re: CHMOD in PHP not work arrgh HELP!

Posted: Thu Nov 07, 2013 10:52 am
by laughingbuddha
Thanks for the reply, I followed this guide http://www.rackspace.com/knowledge_center/article/how-to...yet it still has the same problem.

I did the following commands as root:-

Code: Select all

sudo usermod -a -G apache myuser
sudo usermod -d /var/www/html myuser
sudo chgrp -R apache /var/www/html
sudo chmod -R g+w /var/www/html
sudo chmod g+s /var/www/html
Yet if I connect via SFTP as myuser, upload a folder and then run the PHP script above to change that folders permissions via chmod, it fails on that error. But If I do the same on my Plesk server which uses standard FTP, it works fine.

Re: CHMOD in PHP not work arrgh HELP!

Posted: Thu Nov 07, 2013 10:59 am
by scott
Probably because you're using two different user id's. What you could do here is run the domain as the sftp user with either mod_ruid2, or fcgi.

Re: CHMOD in PHP not work arrgh HELP!

Posted: Thu Nov 07, 2013 11:08 am
by laughingbuddha
I did forget to mention I'm not running vhosts in Apache, as there's only one domain on the server.

So would it be better to change the apache user to the SFTP username in the apache config?

Re: CHMOD in PHP not work arrgh HELP!

Posted: Thu Nov 07, 2013 2:39 pm
by laughingbuddha
I got it working in the end by changing the Apache config so that it runs as my SSH/SFTP user, but is still part of the apache group.

My ssh user doesn't have sudo root permissions, so it should be safe enough option.

Thanks for your help guys :)