A) It is not advisable to give any form of access to the world, even if it is just read access.
B) To give the owner of the file just a read access leads to complicated maintenance process (eg: most recommended, that Settings.php should be readonly to all), this will only increase your tasks whenever you want to modify the settings.
In nutshell:
- World need 0 access - not even to public folder.
- Your web server needs read only access for all files, except the public folder and tmp folder - these will be both read and write.
- Your file owner needs full access to all files - to keep maintenance simple
This however, will work best when file owner and webserver owner are 2 separate users, and you have ssh control over server and are able to modify the file ownership.
The below script will work when you have following directory structure:
Site Folder
Site Folder/conf (containing apache virtual host configuration files for this site)
Site Folder/htdocs (containing the site)
In this scenario: kalpesh is the file owner and daemon is the webservice owner - it may be www-data for your site.
I normally save such script in a .sh file and then add it to cron, so that whenever my team members upload new content on the site or update a module, the sites permission doesn't get compromised by their mistakes. Cron will execute the scripts and repair permissions every 24 hours.
cd ToSiteFolder
sudo chown kalpesh:daemon .
sudo chmod 750 .
sudo chown -R kalpesh_popat:daemon ./conf
sudo find ./conf -type d -exec chmod 750 {} +
sudo find ./conf -type f -exec chmod 640 {} +
sudo chown -R kalpesh_popat:daemon ./htdocs
sudo find ./htdocs -type d -exec chmod 750 {} +
sudo find ./htdocs -type f -exec chmod 640 {} +
sudo find ./htdocs/sites/default/files -type d -exec chmod 770 {} +
sudo find ./htdocs/sites/default/files -type f -exec chmod 660 {} +
sudo find ./htdocs/tmp -type d -exec chmod 770 {} +
sudo chmod 640 ./htdocs/sites/default/settings.php
sudo chmod 750 ./htdocs/sites/default
There is a blog that explains this beautifully and breaks many myths. https://technologymythbuster.blogspot.com/2018/06/misconception-about-file-ownerships-and.html