Created
October 23, 2024 07:54
-
-
Save numpde/147b8fbc1e191c95557564eb9196a328 to your computer and use it in GitHub Desktop.
WP/WC: Set file permissions on the host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script updates file ownership and permissions for a WordPress installation | |
# located in the specified base directory. It ensures that the directories and files | |
# are owned by 'www-data', making them accessible for a WordPress instance running | |
# as 'www-data' inside a Docker container, while allowing the 'wc' user to edit them. | |
# | |
# The script performs the following actions: | |
# 1. Changes ownership of plugins, themes, uploads, wp-config.php, and .htaccess to 'www-data'. | |
# 2. Adds the 'wc' user to the 'www-data' group for edit permissions. | |
# 3. Sets directory permissions to 775 (read, write, execute for owner/group). | |
# 4. Sets file permissions to 664 (read, write for owner/group, read for others). | |
# 5. Handles errors if the specified directory does not exist or changing directories fails. | |
# Define variables | |
USER_NAME="wc" | |
BASE_DIR="." | |
# Verify that the script is run as root or with sudo | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root or with sudo" | |
exit 1 | |
fi | |
# Change to the base directory | |
# If changing the directory fails, exit the script with an error message. | |
cd "$BASE_DIR" || { echo "Failed to change directory to $BASE_DIR. Exiting."; exit 1; } | |
# Change ownership of specific directories and files to www-data:www-data | |
echo "Changing ownership to www-data:www-data in $BASE_DIR..." | |
chown -R www-data:www-data ./plugins ./themes ./uploads | |
chown www-data:www-data ./wp-config.php ./htaccess | |
# Add the user to the www-data group for permission access | |
echo "Adding '$USER_NAME' to 'www-data' group..." | |
usermod -aG www-data "$USER_NAME" | |
# Set permissions for directories (rwx for owner and group, rx for others) | |
echo "Setting directory permissions to 775..." | |
find ./plugins ./themes ./uploads -type d -exec chmod 775 {} \; | |
# Set permissions for files (rw for owner and group, r for others) | |
echo "Setting file permissions to 664..." | |
find ./plugins ./themes ./uploads -type f -exec chmod 664 {} \; | |
chmod 664 ./wp-config.php | |
chmod 664 ./htaccess | |
# Feedback to the user | |
echo "Ownership and permissions updated successfully in $BASE_DIR. '$USER_NAME' can now edit the specified files and directories." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment