+# stops apache from serving up the site.
+function disable_site()
+{
+ local sitename="$1"; shift
+ local site_config="/etc/apache2/sites-available/${sitename}.conf"
+
+ if [ ! -f "$site_config" ]; then
+ echo "The site config did not exist and could not be disabled: $site_config"
+ return 0
+ fi
+
+#hmmm: repeated pattern of hidden output file, very useful. abstract it...
+ local outfile="$TMP/apacheout.$RANDOM"
+ a2dissite "$(basename $site_config)" &>$outfile
+ if [ $? -ne 0 ]; then
+ # an error happened, so we show the command's output at least.
+ cat $outfile
+ echo
+ echo "There was a problem disabling the apache config file in:"
+ echo " $site_config"
+ echo "Please consult the apache error logs for more details."
+ exit 1
+ fi
+ \rm "$outfile"
+}
+
+# turns on the config file we create above for apache.
+function enable_site()
+{
+ local sitename="$1"; shift
+ local site_config="/etc/apache2/sites-available/${sitename}.conf"
+
+ local outfile="$TMP/apacheout.$RANDOM"
+ a2ensite "$(basename $site_config)" &>$outfile
+ if [ $? -ne 0 ]; then
+ # an error happened, so we show the command's output at least.
+ cat $outfile
+ echo
+ echo "There was a problem enabling the apache config file in:"
+ echo " $site_config"
+ echo "Please consult the apache error logs for more details."
+ exit 1
+ fi
+ \rm "$outfile"
+}
+
+# restarts the apache2 service.
+function restart_apache()
+{
+ service apache2 restart
+ if [ $? -ne 0 ]; then
+ echo "There was a problem restarting the apache2 service."
+ echo "Please consult the apache error logs for more details."
+ exit 1
+ fi
+ echo Apache2 service restarted.
+}
+
+# sets up the serverpilot storage location for a user hosted web site.
+function maybe_create_site_storage()
+{
+ local our_app="$1"; shift
+ # make sure the path for storage this app exists for the user.
+ local full_path="$BASE_APPLICATION_PATH/$our_app"
+ if [ ! -d "$full_path" ]; then
+ mkdir -p $full_path
+ test_or_die "The app storage path could not be created.\n Path in question is: $full_path"
+ fi
+
+ # now give the web server some access to the folder. this is crucial since the folders
+ # can be hosted in any user folder, and the group permissions will not necessarily be correct already.
+ local chow_path="$full_path"
+ # only the first chmod is recursive; the rest just apply to the specific folder of interest.
+ chmod -R g+rx "$chow_path"
+ # walk backwards up the path and fix perms.
+ while [[ $chow_path != $HOME ]]; do
+#echo chow path is now $chow_path
+ chmod g+rx "$chow_path"
+ test_or_die "Failed to add group permissions on the path: $chow_path"
+ # reassert the user's ownership of any directories we might have just created.
+ chown $(logname) "$chow_path"
+ test_or_die "changing ownership to user failed on the path: $chow_path"
+ chow_path="$(dirname "$chow_path")"
+ done
+}
+
+############################################################################
+
+