LittleBizzy

Dominate technical SEO with a SlickStack cloud server for just $39/month!  Order Now

Owners, Groups, & Permissions Reset Script

Oh, file permissions! Perhaps one of the most annoying things for both web hosts and webmasters across the globe are those pesky file permissions that get in the way of software updates, media uploads, and other maintenance tasks. For better or worse, it tends to be a knee-jerk scapegoat for many developers: “hmm, could it be a permissions issue?” is all too often the otherwise hopeless response of technical support agents the world over. If you are accustomed to years of cPanel/Apache web hosting, you may not have noticed such issues too often as the (bulky) software manages its own users and server permissions. However, as the trend toward slimmed down VPS servers continues to grow, permissions issues are here to stay.

In order to manage inconsistent owners/groups/permissions, some managed WordPress hosting companies offer a “permissions reset” button in their user control panel. Others don’t even have a solution, and only address the problem when asked. In response to this growing request from our users, we decided to put together a shell script for Ubuntu servers that can be loaded into crontab and run automatically on a scheduled basis. Feel free to try it out if you run servers of your own :)

Note: you must have root access to your server in order to properly configure this cron job script.

First, save variables in separate config file below:

cd /home/example
sudo touch /home/example/config
sudo nano /home/example/config

Copy and paste the below code into your new config file (change username):

#!/bin/bash
user="example"

Next, let’s create a new file i.e. perms-cron in which our cron script is going to reside:

cd /home/example
sudo touch /home/example/perms-cron
sudo nano /home/example/perms-cron

…and copy and paste the below code into your cron script created above:

#!/bin/bash
source config
source /home/$user/perms

Lastly, we need the actual permissions settings in a third file:

cd /home/example
sudo touch /home/example/perms
sudo nano /home/example/perms

Copy and paste the below script into your new perms file (don’t forget to change any sources):

Important: notice that sudo is not necessary as we will be adding this shell script to the root user’s crontab. Including sudo in a shell script requires a user’s password to be stored in plaintext = very bad idea!

Code last updated 5 May, 2016

#!/bin/bash
chown root:root /home/$user/config
chown root:root /home/$user/muplugs-cron
chown root:root /home/$user/perms-cron
chown root:root /home/$user/ups-cron
chown root:root /home/$user/perms
chown root:root /home/$user/check-cron
chown root:root /home/$user/worker-cron
chmod +x /home/$user/config
chmod +x /home/$user/muplugs-cron
chmod +x /home/$user/perms-cron
chmod +x /home/$user/ups-cron
chmod +x /home/$user/perms
chmod +x /home/$user/check-cron
chmod +x /home/$user/worker-cron
chown -R $user:wordpress /home/$user/www
chown -R www-data:wordpress /home/$user/www/wp-content/mu-plugins
chown -R www-data:www-data /var/log/nginx;
chmod -R 755 /var/log/nginx;
find /home/$user/www/ -type d -exec chmod 775 {} \;
find /home/$user/www/ -type f -exec chmod 664 {} \;
find /home/$user/www/wp-content/cache/ -type d -exec chmod 777 {} \;
find /home/$user/www/wp-content/cache/ -type f -exec chmod 777 {} \;
chmod 660 /home/$user/www/wp-config.php
chmod -R g+s /home/$user/www/

Finish up the script by making sure all files executable and owned by the root user (our cron will do this too):

sudo chmod +x /home/example/config
sudo chmod +x /home/example/perms-cron
sudo chmod +x /home/example/perms
sudo chown root:root /home/example/config
sudo chown root:root /home/example/perms-cron
sudo chown root:root /home/example/perms

You will notice that our script file is merely named “permissions” without any file extension, such as .sh or otherwise. This is because we’ve already made the script executable and included a “shebang” line at the top of our script to clarify to the root user which type of script this is (bash), so there is no need for a file extension type.

Lastly, we need to add this script to the root’s crontab file for scheduling:

sudo crontab -e

At the bottom of the crontab file, paste the below code (change username). /dev/null 2>&1 will prevent any cron reporting attempts being sent via email, which is best in the case that your VPS does not have an active mail server. The cron below is set to run once every hour, which should be more than enough (too often can stress server resources):

0 * * * * /home/example/perms-cron > /dev/null 2>&1

For good measure, restart the Nginx service and then test that the script is working (see below video):

sudo service nginx restart
About the Author

Jesse

Leave a Reply

Your email address will not be published. Required fields are marked *