Giving permissions to webserver without changing folder permissions to 777 - php

I've tried to use chmod function in php to change permissions to 777 temporarily, upload the file and then change it back to 755. But it didn't work, as it doesn't allow me to use the chmod function via php.
if(chmod($path, 0777) ) {
if(!move_uploaded_file($oldfile, $newfileloc)) {
return false;
}
chmod($path, 0755);
return true;
}
else
return false;
I had it working on my previous server with 755 permissions given to the folder.
I'm not sure how permissions work, so please help, thanks!
EDIT:
What permissions should my /var/www folder have so that web-server can write files?
EDIT 2:
Okay, I had this figured out. I just have to give permissions to www-data:www-data to make sure webserver has all the required permissions.
But, the issue I'm getting is that when I have /var/www has chown www-data:www-data, the php functions are working fine but I'm getting permissions denied error when using FileZilla. So right now I have to change permissions to root:www-data everytime I need to transfer something via FileZilla and then back to www-data:www-data to make sure my webserver's working fine. Anyone got a fix for this?

you can give 755 permission. But You have to change owner and group for /var/www/ folder. It should have www-data's ownership and group ownership. Check first which user has ownership and group ownership for this folder. run this below command.
ll /var/www/
if it has root access then it would look like this.
drwxr-xr-x. 2 root root 23 Mar 21 17:33 html
change the owner and group owner to www-data user using below command.
chown -R www-data:www.data /var/www
You can keep folder permission 755. -R option is use for giving permission recursively to its child folders and files.

Related

mkdir(): Permission problems

Im getting denied running the following function (which is located in
/var/www/mysite/public_html/app/Controllers/Script.php)
$structure = '/var/www/mysite/public_html/app/Controllers/folder1/newfolder';
if (!mkdir($structure, 0755, true)) {
die();
}
I dont want to give to the web server general root permissions and also I want to keep folder1 in 755.
What I did is to edit the sudoers file trying to give to apache root permisions just in that specific path, I added this line:
www-data ALL=NOPASSWD:
/var/www/mysite/public_html/app/Controllers/Script.php
However Im still getting the same error, what is missing here?
I do not know anything about php but must likely you is running them scripts with the wrong user
check who’s the owner of the dir.
$ ls -lash
change ownership.
$ sudo chown -R $USER:$USER /path/to/dir
chown changes owner
‘-R’ change the ownership of subdirs and files
$USER current user

change ownership of file in apache

I am working on apache sever and generating some tmpdir using follwoing code.
$tmpdatadir = "/home/user/tmpdata/".$id."/";
if (mkdir($tmpdatadir)) {
/* do something */
}
dir created:
drwxr-xr-x 2 www-data www-data 4096 Aug 30 17:16 147257020639481
but when i try to write some data using
exec ("cat file.txt >". $tmpdatadir."sample.txt")
i get following error message.
permission denied. As i copy file as user:user so how can i change permision of directory. I found chown does this but i am not sure how can i change ownership of whole directory.
First of all, please review the permissions for www-data folder with ls /lrt, is that way you can see if your user is able to write on the file.
Then, you can use the command: chmod 666 www-data to change the permission of the file to read and write for all users, in this link you can find the syntax for chmod command and a useful calculator if you want limit other users.
Also I share with you the specific functions for commands chown and chmod. See this site.

Not able to create folder on /home/ec2-user/MyDir/ on AWS

I wanted to create a folder on "/home/ec2-user/MyDir/test" on AWs but It is saying permission denied though I given enough permission to the directory.
<?php
$brideOrderFolder = '/home/ec2-user/MyDir/test';
if (!#mkdir($brideOrderFolder)) {
$error = error_get_last();
echo $error['message']; //Error: mkdir(): Permission denied
}
exit;
?>
Here is my folder permission.
[ec2-user#ip-xx-xxx-xxx-xxx MyDir]$ ls -ld
drwxrwxrwx 5 ec2-user ec2-user 4096 Aug 23 14:24 .
Can't I make dir at "/home/ec2-user" dir in AWS?
Change owner of parent directory to "apache" user.
$sudo chown apache:apache /var/www/html/PATHTOYOURDIR/
In order to make any changes to any folder or file on Linux, the application or user should be under that group.
Use ls -l for the folder ec2-user and see which user/ group has permissions to rw this folder and I am sure www-data do not have permission to r/w this folder. Ec2-user has right to his folder by default.
You can create fresh group and change owner group for this folder.
Alternately I ll suggest to
Step1: create folder under www/ec2-user-temp/
And now php can simply create folders under this folder.
Step2: schedule a copy Command under crontab to copy folder recursively to ec2-user. Crontab can execute it at intervals you define.
Or you can create sym link to replicate folders files automatically as soon as they are created under www folder. You can learn commands from here
https://blog.bartbania.com/raspberry_pi/create-symbolic-links-in-linux/
For security avoid any php or serverside application to make changes to folders or files outside your webroot or www folder.
I hope this helps.
i used $sudo chown ec2-user:ec2-user /var/www/html/ and it worked! thanks #pranjitsingh for the heads up.

Why can't remove the dir that have been chmoded 777?

I have assign anyone the right to read and write.
chmod 777 -R /home/test
An error occurred When to remove it with php function:
rmdir('/home/test');
Warning: rmdir(/home/test): Permission denied
Having 777 permission to /home/test isn't enough, the process trying to remove it also needs to have permission to alter /home (since removing /home/test constitute a change to /home).
The answer is likely that you have an ownership issue. The directory is probably owned by another user and group.
You can change this by using the chown command which stands for change ownership.
chown user:group directory/file.extension

Codeigniter – The upload destination folder does not appear to be writable

I'm using CodeIgniter's upload helper and received the above error when trying to upload an image. The permissions for the folder I'm trying to upload to are 755. When I changed it to 777, the error went away, but isn't 777 kind of a security risk?
I'm running on Apache. Is there a better way to allow users to upload files without setting the folder permissions to 777? How can I get 755 to work?
Thanks for the help!
If the folder is for loading files by users than permisision 777 is required.
It's up to you to validate what files are loaded through upload script.
Also you can use .htaccess to alow or not alow certain files to be executed from that directory.
The documentation for upload in codeigniter it's pretty simple and intuitive. Also here you can look at some ways to validate the type of files that are uploaded https://codeigniter.com/userguide3/libraries/file_uploading.html
I don't think so giving any folder on server 777 permission is good. Instead giving 777 permission i suggest make www-data user as owner of desired folder and give 755 permission like below
chown -R www-data:www-data /var/www/html/uploads/
For 755 permission
chmod 755 -R /var/www/html/uploads/
In my NGINX + PHP-FPM installation the issue was solved changing the SElinux parameters from enforcing to permissive:
edit and change options with vi /etc/selinux/config
apply options without restart with sudo setenforce 0
check the status with sestatus.
try this:
sudo chmod 777 -R /path/to/write/folder
I know this is not an active question and may not be an issue for most but because I came across this I wanted to clarify for anyone else that may see this.
You DO NOT need 777 permission on your upload directory. This is actually not a good idea. The last 7 means it is public writable which does not need to be in most cases. Typically 755 should be good enough
More than likely the issue is that the directory is not owned but the user running Apache which is typically www-data
Step by step:
Check owner of dir (i.e.)
ls -l /path/to/upload/
Output should show similar
drwxr-xr-x 4 www-data www-data 4096 Oct 26 20:41 uploads
If not then you should change to www-data if that is the user Apache is running under. To check what user apache is running under :
ps aux | egrep '(apache|httpd)'
This should list something similar:
www-data 419 0.0 0.9 556292 156656 ? S 18:46 0:00 /usr/sbin/apache2 -k start
Hope This Helps!

Categories