On a development server (Ubuntu 14.04) we have this shell script running every minute on cron:
for dir in /home/*; do
username=$(basename $dir)
echo "changing ownership to $username in $dir/public_html"
chown -R $username:$username $dir/public_html
chmod 755 $dir
chmod -R 775 $dir/public_html
done
Which has the aim of changing ownership and chmod of any file to the virtualhost's username. This is because when a new file is created, it's created by 'root' as our local machines are SMB to the server using root user. The files need to be owned by virtualhost username in order for them to run.
This script works perfectly well, BUT will start to slow down the server when we get a lot of virutalhosts / files as it chowns everything regardless of if it needs it. Most of the time it won't need it as it's only new files which do.
My thoughts are to change it to foreach public_html directory look for any files which are not owned by that virtualhost username and chown that only.
But I fear that having to roll through every file to know which ones to chown might be just as (or more) intensive than the way it works at the moment (just looking at directory level then running recursively).
So was wondering if anyone has a better idea for this? (happy with sh or php script solution)
Aim: any newly created file, chown and chmod it to virtualhost username and 755. Skip all other files. Would also be good to have ability to skip certain filetypes regardless.