Skip to content

Instantly share code, notes, and snippets.

@annedorko
Created April 8, 2018 08:33
Show Gist options
  • Save annedorko/e023e548f2381c122fd2dd5dc640ab75 to your computer and use it in GitHub Desktop.
Save annedorko/e023e548f2381c122fd2dd5dc640ab75 to your computer and use it in GitHub Desktop.
Set WordPress file permissions on Ubuntu 16.04
#!/bin/bash
# Automatically sets appropriate permissions on WordPress files
# ===== Expects multiple sites stored under /var/www/{sitename}/html
if [[ $* == *--all* ]]; then
echo "Reset permissions for all WordPress sites? (Y/n)"
read accept
find /var/www -name 'wp-config.php' -exec dirname {} \; > ~/wp_list.txt
else
if [ -z $1 ]; then
echo "Please enter a domain name."
exit
fi
pub_html="/var/www/${1}/html"
echo "Does this look correct? ${pub_html} (Y/n)"
read accept
echo ${pub_html} > ~/wp_list.txt
fi
if [ $accept == 'Y' ]; then
while read pub_html; do
sudo chown -R www-data:www-data ${pub_html}
sudo find ${pub_html} -type f -exec chmod 644 {} +
sudo find ${pub_html} -type d -exec chmod 755 {} +
sudo find ${pub_html} -type d -exec chmod g+s {} \;
sudo chmod g+w ${pub_html}/wp-content/
sudo chmod -R g+w ${pub_html}/wp-content/themes
sudo chmod -R g+w ${pub_html}/wp-content/plugins
if [ -d "${pub_html}/wp-content/cache" ]; then
sudo chmod -R g+w ${pub_html}/wp-content/cache
fi
if [ -d "${pub_html}/wp-content/upgrade" ]; then
sudo chmod -R g+w ${pub_html}/wp-content/upgrade
fi
if [ -d "${pub_html}/wp-content/db-backups" ]; then
sudo chmod -R g+w ${pub_html}/db-backups
fi
if [ -d "${pub_html}/wp-content/wflogs" ]; then
sudo chmod -R g+w ${pub_html}/wp-content/wflogs
fi
if [ -f "${pub_html}/wp-content/wp-cache-config.php" ]; then
sudo chmod g+w ${pub_html}/wp-content/wp-cache-config.php
fi
echo "Permissions reset for ${pub_html}"
done < ~/wp_list.txt
fi
rm ~/wp_list.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment