Skip to content

Instantly share code, notes, and snippets.

@willfull
Created January 19, 2017 16:23
Show Gist options
  • Select an option

  • Save willfull/3fe400a3ec0abc8e6ea3fba56a1bfd59 to your computer and use it in GitHub Desktop.

Select an option

Save willfull/3fe400a3ec0abc8e6ea3fba56a1bfd59 to your computer and use it in GitHub Desktop.
Script to automate ownership and permissions reset for WordPress sites.
#!/bin/bash
#
# This script configures WordPress file permissions based on recommendations
# from http://codex.wordpress.org/Hardening_WordPress#File_permissions
#
# Original author: Michael Conigliaro (https://gist.github.com/macbleser/9136424)
# Modified by willfull on 17 January 2017 (https://gist.github.com/willfull/3fe400a3ec0abc8e6ea3fba56a1bfd59)
#
WP_ROOT=${1:-.} # <-- wordpress root directory, current directory by default
[ -e "$WP_ROOT/wp-config.php" ] || { echo "Usage: $0 /path/to/wordpress"; exit; } # <-- detect that the directory is a wordpress root
WP_OWNER=$(id -u $(logname)) # <-- wordpress owner (This assumes the wordpress owner is the logged in user)
WP_GROUP=$(id -g $(logname)) # <-- wordpress group (This assumes the wordpress owner is the logged in user)
WS_GROUP=$(
source /etc/apache2/envvars 2>/dev/null && # This works on debian-based systems at least
echo "$APACHE_RUN_GROUP" ||
echo nobody
) # <-- webserver group
# Miscellaneous ANSI codes/colors
ansiOK="\033[0;93mOK\033[0m" # <-- completed step "OK" macro
ansiOff="\033[0m" # <-- reset ANSI color & format
ansiCyan="\033[0;36m" # <-- Cyan
ansiICyn="\033[0;96m" # <-- Cyan (Intense)
ansiGren="\033[0;32m" # <-- Green
ansiBIGn="\033[1;92m" # <-- Green (Bold + Intense)
ansiIRed="\033[0;91m" # <-- Red (Intense)
ansiBWht="\033[1;37m" # <-- White (Bold)
ansiIWht="\033[0;97m" # <-- White (Intense)
ansiIYel="\033[0;93m" # <-- Yellow (Intense)
# Intro
echo "+----------------------------------------------------------------------+"
echo -e "| ${ansiBWht}\033[44mWordPress Fix script by willfull - v1.0 \033[0;37m\033[44m(ownership and permissions)${ansiOff} |"
echo "+----------------------------------------------------------------------+"
echo -e " WordPress site directory: ${ansiIYel}$WP_ROOT${ansiOff}"
echo " WordPress site owner IDs:"
echo -e " Apache = ${ansiICyn}$WS_GROUP${ansiOff} (web server)"
echo -e " User = ${ansiICyn}$WP_OWNER${ansiOff}"
echo -e " Group = ${ansiICyn}$WP_GROUP${ansiOff}"
echo
# 1: Ownership reset
echo -en "[Step ${ansiIWht}#1${ansiOff}] Reset ${ansiIRed}ownership${ansiOff} of all dirs and files ... "
chown -R ${WP_OWNER}:${WP_GROUP} ${WP_ROOT}
echo -e ${ansiOK}
# 2: Permissions reset
echo -en "[Step ${ansiIWht}#2${ansiOff}] Reset ${ansiIRed}permissions${ansiOff} of all dirs and files ... "
find ${WP_ROOT} -type d -exec chmod 2775 {} +
find ${WP_ROOT} -type f -exec chmod 0664 {} +
echo -e ${ansiOK}
# 3: Security for .htaccess
echo -en "[Step ${ansiIWht}#3${ansiOff}] Secure ${ansiGren}$WP_ROOT/${ansiBIGn}.htaccess${ansiOff} ... "
touch ${WP_ROOT}/.htaccess
chmod 0660 ${WP_ROOT}/.htaccess
echo -e ${ansiOK}
# 4: Security for wp-config.php
echo -en "[Step ${ansiIWht}#4${ansiOff}] Secure ${ansiGren}$WP_ROOT/${ansiBIGn}wp-config.php${ansiOff} ... "
chmod 0660 ${WP_ROOT}/wp-config.php
echo -e ${ansiOK}
# Fini!
echo -e "\nCompleted at: ${ansiIYel}$(date)${ansiOff}"
@willfull
Copy link
Author

Original script located here:
https://gist.github.com/macbleser/9136424

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment