Skip to content

Instantly share code, notes, and snippets.

@heyalexej
Last active September 12, 2018 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save heyalexej/11351829 to your computer and use it in GitHub Desktop.
Save heyalexej/11351829 to your computer and use it in GitHub Desktop.
Fix WordPress File Permission
#!/bin/bash -ex
#
# configures wordpress file permissions based on recommendations
# from http://codex.wordpress.org/Hardening_WordPress#File_permissions
#
# script is aware of .git directories by default. edit if you need to consider
# other folders as well.
#
# you will find a log file in /tmp/ in case you fucked up.
WP_OWNER=www-data # <-- wordpress owner
WP_GROUP=www-data # <-- wordpress group
WP_ROOT=$(pwd) # <-- wordpress root directory / run from root or edit
WS_GROUP=www-data # <-- webserver group
LOG=`mktemp "${TMPDIR:-/tmp}"/permissions.XXXXXXXXXX` # create log
# reset to safe defaults
find ${WP_ROOT} -not -iwholename '*.git*' -exec chown -v ${WP_OWNER}:${WP_GROUP} {} \; >> $LOG
find ${WP_ROOT} -not -iwholename '*.git*' -type d -exec chmod -v 755 {} \; >> $LOG
find ${WP_ROOT} -not -iwholename '*.git*' -type f -exec chmod -v 644 {} \; >> $LOG
# allow wordpress to manage wp-config.php (but prevent world access)
chgrp -v ${WS_GROUP} ${WP_ROOT}/wp-config.php >> $LOG
chmod -v 660 ${WP_ROOT}/wp-config.php >> $LOG
# allow wordpress to manage .htaccess
touch ${WP_ROOT}/.htaccess
chgrp -v ${WS_GROUP} ${WP_ROOT}/.htaccess >> $LOG
chmod -v 664 ${WP_ROOT}/.htaccess >> $LOG
# allow wordpress to manage wp-content
find ${WP_ROOT}/wp-content -exec chgrp -v ${WS_GROUP} {} \; >> $LOG
find ${WP_ROOT}/wp-content -type d -exec chmod -v 775 {} \; >> $LOG
find ${WP_ROOT}/wp-content -type f -exec chmod -v 664 {} \; >> $LOG
@heyalexej
Copy link
Author

there are many reasons why file permissions on a wordpress install can be completely messed up.

example:

 curl -sS wordpress.org/latest.tar.gz | tar -xvz --strip-components=1

now, if you're in :rage4: mode, you can pipe it to the shell.

wget -O - https://gist.githubusercontent.com/heyalexej/11351829/raw/permissions.sh | sh

@mxlje
Copy link

mxlje commented Apr 28, 2014

This looks nice, but I’m feeling sorry for you that you’re working with WordPress ;)

@heyalexej
Copy link
Author

Sometimes a man's gotta do what a man's gotta do.

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