PHP, Apache, FreeBSD permissions issue - php

Persumably, this is a *nix permissions question. I'm a Java dev trying to write some simple PHP code. We have a production machine running FreeBSD+Apache.
I'm trying to create a directory called 'ccc' as in '/var/www/aaa/bbb/ccc'
Directories 'aaa' and 'bbb' already exist.
This is ls -ltrh output for directory 'aaa':
drwxr-xr-x 7 root root 12K Jun 10 05:27 aaa
This is ls -ltrh output for directory 'bbb':
drwxr-xr-x 3557 858 856 116K May 28 06:15 bbb
This PHP code does not create the directory '/var/www/aaa/bbb/ccc'. Says 'mkdir FAILED'.
<?php
$path = "/var/www/aaa/bbb/ccc";
if(!file_exists($path)) {
echo "Path does not exist, creating [".$path."]...";
if(mkdir($path, 0777, true)) {
echo "mkdir PASSED...";
}
else {
echo "mkdir FAILED...";
}
}
else {
echo "Path does exist[".$path."]...";
}
?>
This is ls -ltrh output for the php code:
-rw-r--r-- 1 root root 366 Jun 10 07:14 mdtest.php
How can I create the directory 'ccc'? Any pointers would be appreciated.

So the problem is that your apache web server has not write permission to /var/www/aaa/bbb. I dont know BSD, in debian is Apache running under www-data user. So you have to change owner of /var/www/aaa/bbb to apache user.
Run chown -hR apache /var/www

You may set all to write permission: chmod a+rw /var/www/aaa/bbb

Related

File Permissioning Error for Apache on Linux

I'm trying to get a simple PHP file to work on a linux Centos 7 server using apache. The problem is that the php code doesn't seem to have permission to write to the folder. The simple test php file below illustrates the problem
<?php
echo shell_exec('whoami');
echo "<br>";
$myfile = fopen("test.txt","w") or die("could not open test file");
fclose($myfile);
?>;
Just to try to get it to work I have done
sudo chmod -R a+rwx /var/www
and yet I keep getting the "could not open test file" error message. What am I doing wrong? Incidentally, the 'whoami' is coming back as 'apache'
**Edit*
In the light of the suggestion below I've done some changes and am now showing the full permissioning for the folders. I've created the group www-data and have added the user apache to it.
[prompt]$ groups apache
apache : apache www-data
For /var/www:
0 drwxrwsrwx. 4 root www-data 33 Jul 27 08:19 www
For /var/www/html:
0 drwxrwsrwx. 2 root www-data 137 Jul 27 12:43 html
The file I'm trying to load:
4 -rwxrwxrwx. 1 root www-data 182 Jul 27 12:40 test.php
It's still not working unfortunately. Might it be something in the apache configuration? Any suggestions would be much appreciated
Here are two options you can try
Option 1
Make sure the group is www-data on '/var/www'.
prompt> sudo chgrp www-data /var/www
Make '/var/www' writable for the group.
prompt> sudo chmod 775 /var/www
Set the GID for www-data for all sub-folders.
prompt> sudo chmod g+s /var/www
Your directory should look like this on an 'ls -l' output.
drwxrwsr-x
Last, add your user name to the www-data group (secondary group).
prompt> sudo useradd -aG www-data [USERNAME]
Option 2
Use the mod_userdir as described in https://httpd.apache.org/docs/2.4/mod/mod_userdir.html
I would recommend the first option as it suits your needs better.

Problems with php shell_exec permissions

I'm running as www-data and I'm trying to execute a shell script using shell_exec(/foobar/script/myscript.sh), but I'm getting the following error when the script attempts to write to a log file
cannot create /foobar/foo.log: Permission denied
However, I don't run into any problems if I try running the script directly from the terminal. ie
$ sudo su www-data
$ /foobar/script/myscript.sh
$
Any idea what could be going on here?
I should also add that I added www-data to the group mybar and that this is what's showing up when I list the directory
drwxrwxr-x 3 mybar mybar 4096 May 14 14:18 foobar # ls -l /
-rw-rw-r-- 1 mybar mybar 2824 May 15 09:57 foo.log # ls -l /foobar
I think there can be two options:
web-server and php-fpm are run under different users (by default that should not be). Try echo shell_exec('whoami');
You have added www-data to mybar after php-fpm process had been started so it still "doesn't know" that it is (then I think restart of fpm should help).
This works for me:
sudo chown www-data:www-data -R foobar/
The directory you are trying to create the file in must belong to whoever is executing the command.
You can type:
ls -la
to see who it belongs to.
You should see something like this:
drwxr-xr-x 8 jack jack 4096 Jul 22 11:36 application
When I am logged into my ubuntu machine I am logged in as jack#jack so I can create files in the following directory without issuing the sudo command:
drwxr-xr-x 8 jack jack 4096 Jul 22 11:36 application
because it is owned by the current user.
When you try running a script that is executed by visiting a webpage. www-data is the user that is executing the command so any directory or file that you are trying to create/modify/delete must be owned by www-data.

PHP File Upload filed to open stream

function uploadFile() {
global $attachments;
while(list($key,$value) = each($_FILES[images][name]))
{
if(!empty($value))
{
$filename = $value;
//the Array will be used later to attach the files and then remove them from server ! array_push($attachments, $filename);
$dir = "/home/blah/Music/$filename";
chmod("/home/blah/Music",0777);
$success = copy($_FILES[images][tmp_name][$key], $dir);
}
//
}
//
if ($success) {
echo " Files Uploaded Successfully<BR>";
//
}else {
exit("Sorry the server was unable to upload the files...");
}
//
}
Trying to upload a file and then send it as an attachment along mail using PHP Mailer
Errors :
Warning: copy(/home/blah/Music/Aerial_view_of_Yamuna_Expressway.jpeg): failed to open stream: Permission denied in /opt/lampp/htdocs/UI/user/joinmeeting.php on line 292
Updated :
blah#my001server:~$ ls -la for /home/blah/Music
ls: cannot access for: No such file or directory
/home/blah/Music:
total 8
drwxr-xr-x 2 blah blah 4096 Jul 4 10:20 .
drwxr-xr-x 67 blah blah 4096 Sep 21 10:18 ..
Why my linux system is not permitting to copy the file ?
Check permissions for target folder.
Set 777 and try again
$ chmod 777 folder
So as we see now you dont set write permission to Music folder.
Set it manualy from console, not from php script.
Try with move_uploaded_file (http://php.net/manual/en/function.move-uploaded-file.php) instead of copy :
move_uploaded_file($_FILES[images][tmp_name][$key], $dir);
Ok your edit is a bit weird.
blah#my001server:~$ ls -la for /home/blah/Music
ls: cannot access for: No such file or directory
/home/blah/Music:
total 8
drwxr-xr-x 2 blah blah 4096 Jul 4 10:20 .
drwxr-xr-x 67 blah blah 4096 Sep 21 10:18 ..
That command is erroranous since it is actually:
blah#my001server:~$ ls -la /home/blah/Music
You should be running.
But ok I see a problem. The file . which denotes the entire folder has no www-data permissions. This means that the default webuser for your Linux distro probably does not have access those files.
Since PHP runs under webuser www-data Linux will not permit it to cp or vi or gedit (or anything else for that matter) it anything it is not owning.
You can try:
sudo chown /home/blah/Music www-data
Instead. This should give some permission to www-data to take control of files within the directory.
Of course this raises even bigger problems. Ideally you would want to disconnect any upload dir or anything from your actual web server due to security needs.

PHP move_uploaded_file failing for unknown reason

I have an PHP website on a RHEL5/CentOS dedicated server. The website is located at /var/www/html/beta
I have a script:
/var/www/html/beta/scriptA.php
which calls a function in
/var/www/html/beta/code/inc/functions.php
The function uses move_uploaded_file() as follows:
$status = move_uploaded_file($imagetmp_name,$destinationPath);
Printing these values shows:
imagetmp_name=/tmp/phpiECxB6
destinationPath=in_upload/images/907770756_publicpage.jpg
status=false
Which I thought should have worked since 'in_upload/images' exists:
drwxr-xr-x 5 root root 4096 Oct 19 07:40 in_upload
and
drwxr-xr-x 2 root root 4096 Oct 19 07:40 images
What am I doing wrong?
You don't have writing permisions to in_upload neither images, only for root.
Use
chmod a+w in_upload
chmod a+w images
or change that directories' owner/group to the user, under which is apache running.
example:
chown apache:apache in_upload
chmod g+w in_upload

zend framework: can not access file that is already there

I am trying to read and post back to the browser a file uploaded with the zend framework mechanism.
The file has been uploaded correctly to the desired location and as I have checked by
su www-data
and after an ls and a cat, the web user can read it and modify it properly.
the problem is that inside a controller when I try to:
if(!file_exists($fileName)) {
die("File ($fileName) wasnt set or it didnt exist");
}
I am always getting to die(...), although the $fileName is a string and when I display it's location I can always (as stated before) read it from the command line.
ls output:
$ ls -lah
total 112K
drwxr-xr-x 2 www-data www-data 4.0K 2009-10-07 18:21 .
drwxr-xr-x 3 www-data www-data 4.0K 2009-10-07 13:57 ..
-rw-r--r-- 1 www-data www-data 70K 2009-10-07 17:33 Eclipse_Icon_by_TZR_observer.png
-rw-r--r-- 1 www-data www-data 27K 2009-10-07 18:24 eclipse_logo2.png
Stat output:
stat() [function.stat]: stat failed for .../eclipse_logo2.png
I saw a very similar question to the "try for 30 days" site, so it is not something that has happened to me...
Any ideas?
You have to chmod the newly created file because the file owner created from PHP side will be Apache (group: www-data, httpd, www, or something similar). So next time PHP cannot access the file because www-data owns it and it has wrong permissions.
Here's how you create new files so that you can access them later.
<?php
$path = '/path/to/new/file';
touch($path)
chmod($path, 0777);
// TRY to change group, this usually fails
#chgrp($path, filegroup(__FILE__));
// TRY to change owner, this usually fails
#chown($path, fileowner(__FILE__));

Categories