At CODE TOT, we manage 50+ WordPress sites on RunCloud and often encounter issues related to file permissions breaking our WordPress installations. Incorrect permission settings can lead to a variety of problems, from failed logins to broken plugins.

Why WordPress Permissions Break

WordPress requires specific file permissions to run correctly. The default ownership on RunCloud is set to the runcloud user and group. When these permissions are not correctly set, it can lead to a myriad of errors. For instance, trying to update plugins or themes will result in a 500 error if PHP does not have write access to the files.

What Correct Permissions Look Like

For a WordPress site hosted on RunCloud, the correct file permissions should be as follows:

  • 755 for directories: This allows everyone to read and execute (list the contents), but only the owner can write (change files within the directory).
  • 644 for files: This gives the owner read and write permissions, while allowing others to read (run scripts or view files).

The ownership should be on the runcloud user and group for these permissions to work correctly.

Fix All Sites at Once

If you need to fix permissions for all WordPress sites managed on your RunCloud server, you can use our script, fix-permission.sh. This script will recursively apply the correct permissions to all directories and files within the /home/runcloud/webapps/ directory.

#!/bin/bash
WEBROOT="/home/runcloud/webapps"
for site in "$WEBROOT"/*; do
    if [ -d "$site" ]; then
        echo "Processing $site ..."
        sudo chown -R runcloud:runcloud "$site" || echo "Failed to chown $site"
        find "$site" -type d -exec sudo chmod 755 {} \;
        find "$site" -type f -exec sudo chmod 644 {} \;
        echo "Done with $site"
    fi
done

Fix a Single Site

If you need to fix permissions for a single WordPress site, it’s often more efficient than running the entire script. You can use our second script, fix-permission-site.sh, providing the name of your site as an argument with the --site flag.

#!/bin/bash
WEBROOT="/home/runcloud/webapps"
TARGET_SITE=""
for i in "$@"; do
    case $i in --site=*) TARGET_SITE="${i#*=}"; shift ;; esac
done
if [ -z "$TARGET_SITE" ]; then echo "No site specified. Usage: fix-permission-site.sh --site=your Site Name"; exit 1; fi
site="$WEBROOT/$TARGET_SITE"
echo "Processing $site ..."
sudo chown -R runcloud:runcloud "$site" || echo "Failed to chown $site"
find "$site" -type d -exec sudo chmod 755 {} \;
find "$site" -type f -exec sudo chmod 644 {} \;
echo "Done with $site"

Verification

After running the script(s), it’s essential to verify that the permissions have been applied correctly. You can do this by navigating to your site via FTP or SFTP and checking the file and directory permissions for each WordPress installation.

Alternatively, you can use the following commands on the terminal in RunCloud after running fix-permission.sh.

for i in /home/runcloud/webapps/*; do echo -n "$i: "; stat -c "%G %a" "$i"; done
find /home/runcloud/webapps/*/www -type d -exec stat -c "%C %N" {} +
find /home/runcloud/webapps/*/www -type f -exec stat -c "%a %N" {} +

This will output the ownership and permissions for each file and directory within your WordPress installations, allowing you to confirm that they have been set correctly.

Important Notes

  • Always ensure that you have proper backups before running scripts that modify file permissions.
  • Never run these scripts on a server that is not properly managed or maintained as modifying file permissions can potentially cause irreparable damage to your WordPress installations.
  • These scripts assume that you have the necessary privileges to change ownership and permissions on your RunCloud server. If you do not, please contact your hosting provider for assistance.

GitHub Repo

Both scripts are open-source and can be found at our GitHub repository for CODE TOT’s automation projects. If you find any issues or have suggestions for improvement, feel free to contribute!