#!/bin/bash # Set the base directory 
BASE_DIR="/var/www"

# Set ownership
echo "Setting ownership to www-data:www-data for all directories under $BASE_DIR..."
sudo chown -R www-data:www-data $BASE_DIR

# Set permissions for directories
echo "Setting permissions to 755 for all directories under $BASE_DIR..."
sudo find $BASE_DIR -type d -exec chmod 755 {} \;

# Set permissions for files
echo "Setting permissions to 644 for all files under $BASE_DIR..."
sudo find $BASE_DIR -type f -exec chmod 644 {} \;

# Set permissions for special Joomla directories
JOOMLA_DIRS=("administrator/cache" "cache" "logs" "tmp")

for dir in "${JOOMLA_DIRS[@]}";
do
echo "Setting permissions to 775 for $BASE_DIR/*/$dir..."

sudo find $BASE_DIR -type d -name $dir -exec chmod 775 {} \; done

# Set SELinux context if SELinux is enabled
if command -v getenforce &> /dev/null && [ "$(getenforce)" != "Disabled" ];
then

echo "Setting SELinux context for special Joomla directories..."

for dir in "${JOOMLA_DIRS[@]}"; do
sudo find $BASE_DIR -type d -name $dir -exec chcon -R -t httpd_sys_rw_content_t {} \; done
fi

# Restart Apache echo "Restarting Apache..."
sudo systemctl restart apache2

echo "Permissions and ownership setup completed successfully."