Skip to content

Instantly share code, notes, and snippets.

@efann
Last active November 7, 2022 20:56
Show Gist options
  • Save efann/af72166d0226d42c2de966d22df7ffaa to your computer and use it in GitHub Desktop.
Save efann/af72166d0226d42c2de966d22df7ffaa to your computer and use it in GitHub Desktop.
#!/bin/bash
# License: Eclipse Public License - v 2.0 (https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html)
# Updated on November 7, 2022
if [ -z "$1" ]
then
echo -e "\nOne must pass the folder name under /var/www to be set. Exiting. . . .\n\n"
exit 1
fi
if [[ $EUID -eq 0 ]]
then
echo -e "\nThis script may not be run as root. Exiting. . . .\n\n"
exit 1
fi
# From https://stackoverflow.com/questions/3976362/bash-scripts-requiring-sudo-password
# Force asking for the password, so you don't have to wait till running the below external scripts.
if [[ ! $(sudo echo 0) ]]; then
echo -e "sudo password was not correct. Exiting. . . .\n\n"
exit;
fi
WPCLI="$(which wp-cli)"
if [ -z "$WPCLI" ]
then
echo -e "\nThis script needs wp-cli installed. You can find more information here: http://wp-cli.org/. Exiting. . . .\n\n"
exit 1
fi
# Remove the trailing backslash, if exists, from the first parameter.
lcWebRoot="/var/www/${1%/}"
lcWordpress="$lcWebRoot/public_html"
lcConfig="$lcWordpress/wp-config.php"
if [ ! -d "$lcWordpress" ]
then
echo -e "\n$lcWordpress does NOT exists. Exiting. . . .\n"
exit 1
fi
if [ ! -f "$lcConfig" ]
then
echo -e "\n$lcConfig does NOT exists. Exiting. . . .\n"
exit 1
fi
# ------------------------------------
# Ensure that all files match the owner:group of the WordPress root folder, the one that contains
# public_html and log.
# ------------------------------------
lcOwner=`stat -c '%U' $lcWebRoot`
lcGroup=`stat -c '%G' $lcWebRoot`
# I found that even if I change the value of folder group
# it gets changed back to nobody at a later time . . . probably by cPanel / WHM
# and their bookkeeping.
if [ "$lcGroup" == "nobody" ]
then
lcGroup="$lcOwner"
fi
pushd "$lcWordpress"
lcCheck="$PWD"
if [ "$lcWordpress" != "$lcCheck" ]
then
echo -e "\n$lcCheck is not the correct current folder. Exiting. . . .\n"
exit 1
fi
echo -e "\nDisplay information for wp-cli.\n"
"$WPCLI" --info
echo -e "\nUpdate WordPress. . . .\n"
"$WPCLI" core update
echo -e "\nUpdate all themes. . . .\n"
"$WPCLI" theme update --all
echo -e "\nUpdate all plugins. . . .\n"
"$WPCLI" plugin update --all
echo -e "\nRegenerate thumbnails. . . .\n"
"$WPCLI" media regenerate --yes
echo -e "\nFlush the cache. . . .\n"
"$WPCLI" cache flush
echo -e "\nChanging ownership & group to $lcOwner:$lcGroup for $lcWordpress.\n"
sudo chown -R "$lcOwner":"$lcGroup" "$lcWordpress"
echo -e "\nChanging permissions for $lcWordpress. . . .\n"
lcUploads="./wp-content/uploads"
if [ ! -d "$lcUploads" ]
then
echo -e "\nCreating $lcUploads folder. . . .\n"
mkdir -p "$lcUploads"
fi
echo -e "\nChanging all file permissions. . . .\n"
find . -type f -exec chmod 644 '{}' \;
echo -e "\nChanging $lcUploads file permissions. . . .\n"
find "$lcUploads" -type f -exec chmod 664 '{}' \;
echo -e "\nChanging all directory permissions. . . .\n"
find . -type d -exec chmod 755 '{}' \;
echo -e "\nChanging $lcUploads directory permissions. . . .\n"
find "$lcUploads" -type d -exec chmod 775 '{}' \;
echo -e "Changing wp-config permissions. . . .\n"
chmod 440 "$lcConfig"
popd
echo -e "\nDone. . . ."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment