Skip to content

Instantly share code, notes, and snippets.

@sebastian13
Last active July 30, 2024 08:40
Show Gist options
  • Save sebastian13/c57fb8f62863112825b7b1b912b9a2d8 to your computer and use it in GitHub Desktop.
Save sebastian13/c57fb8f62863112825b7b1b912b9a2d8 to your computer and use it in GitHub Desktop.
Set WordPress' file permissions when using docker compose
#!/bin/bash
# https://gist.github.com/sebastian13/c57fb8f62863112825b7b1b912b9a2d8
#
# This script sets WordPress' file permissions based on recommendations
# from http://codex.wordpress.org/Hardening_WordPress#File_permissions
# and https://stackoverflow.com/a/23755604/8940679
#
# Idea from https://github.com/mconigliaro/notes/blob/master/wordpress/permissions.md
#
# To use this script run:
# ./fix-wordpress-permissions.sh [CONTAINER]
#
WP_OWNER=www-data # <-- wordpress owner
WP_GROUP=www-data # <-- wordpress group
WP_CONTAINER=${1:-'wordpress'} # <-- container running wordpress
docker compose exec ${WP_CONTAINER} /bin/bash -c " \
chown --changes ${WP_OWNER}:${WP_GROUP} wp-config.php && \
chmod 600 wp-config.php && \
chown --recursive --changes ${WP_OWNER}:${WP_GROUP} wp-content && \
find wp-content -printf '' && \
find wp-content -type f ! -perm 644 -print0 | xargs --no-run-if-empty --null chmod --changes 644 && \
find wp-content -type d ! -perm 755 -print0 | xargs --no-run-if-empty --null chmod --changes 755 \
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment