Ubuntu with Webmin and Virtualmin, public_html owners

On a clasical Linux Ubuntu Server LAMP (Linux, Apache, Mysql, PHP) with Webmin and Virtualmin installed, when creating subdomains, sometimes files and folders ownership issues appear. In WordPress, this is translated into trouble while updating plugins, wordpress itself or even when running the install script after uploading the files via FTP.

A quick remedy for this problem is:

chown -R www-data:www-data /home/<user>/domains/<subdomain>/public_html
chmod -R 0755 /home/<user>/domains/<subdomain>/public_html

A more elaborate way is using a bash script. Here is an example:

#!/bin/bash
  if [ $# -ne 2 ]
 	then { echo "Required parameters: <user> <domain>"; }
 	else
 	{
 		target="/home/$1/domains/$2/public_html"
  		if [ -d "$target" ]
 			then {
 				chown -R www-data:www-data $target
 				chmod -R 0755 $target
                                echo "Bash script run for $target";
 			}
 			else { echo Directory missing: $target; }
 		fi
 	}
 fiCode language: PHP (php)

Save the above code into a file and then run it using the first command of the following to make it executable and the second to execute it every time you need:

chmod +x /path/to/file.sh
./path/to/file.sh user subdomain.domain.com