Using bindfs to solve permission issues for Nginx on Fedora
Edit 17th December, 2019: Modified fstab command to allow npm run dev on laravel installations
This article is based on http://blog.netgusto.com/solving-web-file-permissions-problem-once-and-for-all/ The solution mentioned there is for Debain based systems and apache. I modified it for Nginx on Fedora and altered permissions for 755 on folders and 644 on files instead of 770 on all as mentioned in that article.
bindfs is a simple utility that mounts directory as a disk. The description itself felt like a solution for an old problem. Getting permissions to work correctly is a pain in the a** for web developers coming from a Windows background. We can kinda solve this with User-Group but once we create a new file with our user then nginx user has no access to it. The Linux geniuses can solve it easily but I want a quick solution to this rather than working on permissions for every new project installed via Composer.
Install bindfs using dnf
sudo dnf install bindfs
Create a directory in your home directory where your nginx directory will be mounted.
Note: Replace amarnath with your username.
mkdir -p /home/amarnath/websites chown -Rf amarnath:amarnath /home/amarnath/websites chmod -Rf 770 /home/amarnath/websites
Then edit /etc/fstab and add the following. I used gedit and as I’m opening as root, saving will not be an issue.
bindfs#/usr/share/nginx/html /home/amarnath/websites fuse force-user=amarnath,force-group=amarnath,create-for-user=nginx,create-for-group=nginx,create-with-perms=god=rx:ud=rwx:gof=r:uf=rw,chgrp-ignore,chown-ignore,chmod-ignore 0 0
Note: If you are intending to compile or build node assets from the mounted location please use the following instead of the previous one.
bindfs#/usr/share/nginx/html /home/amarnath/websites fuse force-user=amarnath,force-group=amarnath,create-for-user=nginx,create-for-group=nginx,create-with-perms=god=rx:ud=rwx:gof=rx:uf=rwx,chgrp-ignore,chown-ignore,chmod-ignore 0 0
Once done run the following command
Now you should see all your websites in your home directory. The permissions for you on these files will be amarnath:amarnath where as the ones you create will be looked in the /usr/share/nginx/html as nginx:nginx
Millions of thanks to the developers at bindfs. My biggest nightmare of web development on linux is solved.