PHP exec find doesnt work - php

I'm trying to recursively chmod all directories with php, and using:
find /path/to/dir -type d -exec chmod 777 {} \;
Works great from the command line, but using it from the server:
exec('find /path/to/dir -type d -exec chmod 777 {} \;');
Does nothing. Any ideas? Is it permission related? The path is the real path server level.

When you run it on the command line, you're running it as your user which probably is the owner of the path. When you exec() from PHP it is the Apache user that is doing it. Only the owner a a file/dir can chmod it. To confirm this is the case, try to run your exec() script from the PHP CLI Interactive Shell. Within the Interactive Shell, you can run PHP code but it will execute as your user.
If that turns out to be the cause, you must first chown the dir and it's sub files/dirs to be owned by the Apache user.

Related

Magento cron changing folder permissions

We have a Magento website hosted on EC2 and for some reason var folder permissions keep on changing after some time. I think its happening because of magento default cron which runs every couple of hours and it seems it flushes the cache and at that time folder permission gets changed. Because of this website stops working.
Here is the screenshot of the error which we get once permissions are changed:
I am solving this by running chmod command through terminal but this is happening every couple of hours, so we need some permanent solution so that this problem is not repeated.
The probable answer is that the cron job executes under your user - and the directory is owned by apache (or www-data or nobody or whatever user your webserver runs as).
To get it to work, you could set up the cron job to run as the webserver user. Something like this:
su -l www-data -c 'crontab -e'
Alternatively, you could change the permissions to 775 (read-write-execute for the owner and group, and read-execute for others) and set the group ownership of the folder to the user running the cron job.
I think that you have some permission restriction in your folder, try these commands:
Magento File permissions:
chmod -R 644 ./*
find . -type d -exec chmod 755 {} ;
find . -type f -exec chmod 644 {} ;
chmod 550 ./mage

Raspberry Pi 3, browser permissions on apache2 php script

Having a nightmare with permissions on RP3, apache2, php
I can run my php api script from terminal with no problems. I use the browser for debugging and get the 'permission denied' when trying to edit text files from the browser.
I have temporarily set permissions to 777 on /var/www but this is insecure, and i can no longer remember the original permissions that were on /var/www
Can i somehow give the broswer (chromium) permission to edit files in /var/www?
I have tried setting inheritance, but to no avail, as i am copying files over all the time, i have to keep resetting the permissions to 777 (which is not ideal), so i can debug the code in a browser.
Any help would be greatly appreciated
Rgds
Mike
Usually, the permissions for folders are 0755 and for folders are 0644.
To set the permissions for folders:
sudo find /var/www -type d -exec chmod 0755 {} \;
To set the permissions for files:
sudo find /var/www -type f -exec chmod 0644 {} \;
Now about the user/group, Apache is probably using the user www-data with group www-data.
To set the user/group for write permissions:
sudo chown www-data:www-data /var/www -R
A note about security: opening up the option to edit the files from the browser is insecure without any login, allowing anyone to edit your files. Also, the write permissions should only be allowed in a directory where the users upload files and NOT for all your /var/www files.

I can't delete the folder generated/code in Magento 2.2.6

I use Docker for my project. After installing the 2.2.6 package with sample data, it gives me problems like the following when I execute php bin/magento cache:clean
The file "/var/www/html/src/generated/code/Magento/Backend/App/Request/PathInfoProcessor/Proxy.php" cannot be deleted Warning!unlink(/var/www/html/src/generated/code/Magento/Backend/App/Request/PathInfoProcessor/Proxy.php):
No such file or directory
The generated/code folder can not be deleted and I have to restart my computer then it does let me delete the folder
I use php 7.1.22 , percona 5.6.39 and docker Version 18.06.1-ce-win73 (19507)
This happens due to insufficient permissions on the project folder and files.Also www-data must be owner of the project if using Apache as web-server.
Please execute commands given below:--
sudo chown -R www-data:www-data [path to magento directory]
navigate to root of your magento project
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
find ./var -type d -exec chmod 777 {} \;
find ./pub/media -type d -exec chmod 777 {} \;
find ./pub/static -type d -exec chmod 777 {} \;
chmod 777 ./app/etc
chmod 644 ./app/etc/*.xml
chmod u+x bin/magento
An important mania for people who offer their help is to think that all systems are the same.
www-data is the user as long as there are conditions, which many will not meet, because they prefer to use nginx + php-fpm instead of the generalist model.
Other times, included in that model is not the user running Appache, nginx or in the end, php, is www-data.
So the most correct answer is not:
Change the owner of the entire magento folder to www-data
Neither give permissions 777, this is the most common of all the aberrations in the network.
Lo mas correcto serĂ­a
Who executes the process you write in the magento directory?
PHP writes as user or globally?
I have consulted the configuration of my php (php, php-fpm, etc.) to verify which user is the one that executes the operations?
Once we have that data then if we can execute
sudo chown -R <user_correct>: <group_correct>
And please, stop paying attention to those who always write chmod -R 777
Or one day they will wake up without their site in a normal way but full of infections that are hardly curable.
Installing magento 2 from the command line of the container in Docker solves the problem of writing permissions in Windows for me
You may need to adjust the parameters:
bin/magento setup:install --base-url=http://local.shop.magento2.com \
--db-host=db_magento2 --db-name=magento --db-user=magento --db-password=magento \
--admin-firstname=magento --admin-lastname=magento --admin-email=user#example.com \
--admin-user=admin --admin-password=admin123 --language=en_US \
--currency=USD --timezone=Europe/Madrid --use-rewrites=1
Just in case anyone stumbles upon this:
Add the following to your docker file
RUN echo "root:root" | chpasswd
This will allow you to switch to root and run chown
su root
password:root
chown -R user:group /folder

Uploading files with PHP on a linux machine

I'm trying to make some sort of cloud application on a headless raspbian machine.
The testing happens on a wamp-server running on windows8.1. PHP version is 5.6
The code works fine on my testing windows, but I run into issues when running the same code on my linux server. I've narrowed down the issue to the fact that (the linux user behind) my PHP code doesn't have write permissions on the folder where it should store files.
I can think of two ways to solve the issue: either give that php-user permissions, or change the user behind the php code. The last one would be quite interesting if it was possible to change per-script.
So, for my concrete question:
1) Where can I find what user is being used by my PHP code?
2) Can I change what user my PHP code uses, preferably on a per-project basis?
EDIT:
My script was running as root, as is shown by echo get_current_user();. I changed ownership of all files to root:webhosting, which was previously ftpuser:ftpgroup. However, when setting permissions to 770 I get access denied
EDIT2:
When using var_dump(posix_geteuid()); instead of get_current_user();, i get UID 33, which matches user www-data.
SOLUTION:
By looking at the EUID with var_dump(posix_geteuid(); I was able to validate the actual user, which does NOT match the get_current_user();. Changing the directroy and all files in it with sudo chown -R www-data:www-data <root of site> I managed to set the correct permissions.
I am not sure what the specifics are but normally when I have issues like that I change the ownership to www-data:www-data and if necessary for upload folders I change the folder permissions to 0755 and inside files permissions to 0644.
EDIT
I changed the permissions setting for security purposes.
You're PHP/Apache user is probably www-data. You probably want to run something like this.
sudo find path/to/project/ -exec chown www-data:root {} \;
File permissions may also be a problem. If so, run something like this.
sudo find path/to/project/ -type d -exec chmod 775 {} \;
sudo find path/to/project/ -type f -exec chmod 664 {} \;

Enabling write permissions Ubuntu Server in var/www/image directory

I'm trying to get my php test upload script to work and was wondering what the command would be to allow files to be uploaded in ubuntu server in the var/www/image directory
What username will be uploading the files? Usually on Ubuntu the Apache web server username is www-data. You can check for sure by finding the web server process in a process list command and seeing which username under which it is running.
ps aux | grep apache or ps aux | grep httpd should give you that answer.
Then you will usually want to make that Apache username the owner of the directory and all files and directories within it:
cd /var/www/image
# recursively (all subdirs & files) set owner to www-data for current directory
chown -R www-data .
Ordinarily, the above should be enough, but if for some reason the directory, files or subdirectories do not have write permission for the owner username, that's easily fixed by changing the permissions to add that write access, like this:
cd /var/www/image
# recursively add "w"rite permissions for the "u"ser (owner) to current directory
chmod -R u+w .
cd /var/www/image
For file like image you don't need execution permission :
sudo chmod 664 *
If you have directories inside image and you want to apply permission :
sudo find . -type d -exec chmod 755 "{}" \;
This will recursively search your directory and chmod 755 all directories only.
Similarly, the following will chmod all files only (and ignore the directories):
sudo find . -type f -exec chmod 644 "{}" \;
File name with space case (thanks to Nicklas B)
find . -type f -print0 | xargs -0 chmod 644
Change edit group to www-data
chown -R (owner):www-data (folder)
And folder permission to 775
cd /var/www
sudo chmod 775 image
You'll be prompted to enter the admin password - do so, and hit [return].
Change edit group may solve most of the problems of permissions.
Changing do www-data and set permission to 775.

Categories