Managing access via inotifywait for Vhosts

47 Views Asked by At

I have a few vhosts on apache server and a few developers working on them separately. I want to give them access to their site only. Since daemon overtakes access on any file that is created within site, I created a script with inotifywait to grant permission on a file that is changed/created, but it only works for a single site and duplicating script for other vhosts doesn't look like an elegant solution.

#!/bin/sh
monitorDir="/opt/bitnami/apps/wordpress/htdocs"

inotifywait -m -r --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -q -e create,modify,move "${monitorDir}" | while read date time dir file; do
        FILECHANGE=${dir}${file}
        #Change ownership of file
        chown wpdev1:wpdev1 "$FILECHANGE"
        #Change permissions of file
        chmod 755 "$FILECHANGE"
done

Does anyone have an idea how this can be solved for multiple folders and developers? (for instance I have 3 websites: wordpress, wordpress2 and wordpress3). Thank you.

1

There are 1 best solutions below

0
On

The following code works. It checks for certain folder for changes and does relevant actions (chown), if changes occur:

#!/bin/bash
wordpressDir="/opt/bitnami/apps/wordpress/htdocs"
wordpress="copyme"
phpDir="/opt/bitnami/apps/phpmyadmin"
phpuser="phpme"

inotifywait -m -r --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -s -q -e create,modify,move "${wordpressDir}" "${phpDir}"  |
while read date time dir file; do
        FILECHANGE=${dir}${file}
        if [[ "$dir" == *"$wordpressDir"* ]]; then
                #Change ownership of file
                chown $wordpress:$wordpress "$FILECHANGE"
        fi
        if [[ "$dir" == *"$phpDir"* ]]; then
                #Change ownership of file
                chown $phpuser:$phpuser "$FILECHANGE"
        fi
        #Change permissions of file
        chmod 755 "$FILECHANGE"
done