CHMOD in PHP not work arrgh HELP!

laughingbuddha
Forum Regular
Forum Regular
Posts: 512
Joined: Mon Mar 10, 2008 9:12 pm
Location: Southampton, UK

CHMOD in PHP not work arrgh HELP!

Unread post 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?
Matt

"Given that God is infinite, and that the universe is also infinite... would you like a toasted teacake?"

about.me/mattauckland
twitter.com/mattauckland
prupert
Forum Regular
Forum Regular
Posts: 573
Joined: Tue Aug 01, 2006 2:45 pm
Location: Netherlands

Re: CHMOD in PHP not work arrgh HELP!

Unread post by prupert »

Have you set the correct group owner for this file?
Have you set write permissions for the group on this file?
Lemonbit Internet Dedicated Server Management
laughingbuddha
Forum Regular
Forum Regular
Posts: 512
Joined: Mon Mar 10, 2008 9:12 pm
Location: Southampton, UK

Re: CHMOD in PHP not work arrgh HELP!

Unread post 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.
Matt

"Given that God is infinite, and that the universe is also infinite... would you like a toasted teacake?"

about.me/mattauckland
twitter.com/mattauckland
scott
Atomicorp Staff - Site Admin
Atomicorp Staff - Site Admin
Posts: 8355
Joined: Wed Dec 31, 1969 8:00 pm
Location: earth
Contact:

Re: CHMOD in PHP not work arrgh HELP!

Unread post 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.
laughingbuddha
Forum Regular
Forum Regular
Posts: 512
Joined: Mon Mar 10, 2008 9:12 pm
Location: Southampton, UK

Re: CHMOD in PHP not work arrgh HELP!

Unread post 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?
Matt

"Given that God is infinite, and that the universe is also infinite... would you like a toasted teacake?"

about.me/mattauckland
twitter.com/mattauckland
laughingbuddha
Forum Regular
Forum Regular
Posts: 512
Joined: Mon Mar 10, 2008 9:12 pm
Location: Southampton, UK

Re: CHMOD in PHP not work arrgh HELP!

Unread post 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 :)
Matt

"Given that God is infinite, and that the universe is also infinite... would you like a toasted teacake?"

about.me/mattauckland
twitter.com/mattauckland
Post Reply