refcodes-batch.1.0.2.source-code.create-apache-website.job Maven / Gradle / Ivy
#!/bin/bash
. $SCRIPT_DIR/lib-filesystem.inc
if [[ $HELP = y ]] ; then
echo "The switch \"-j $JOB\" uses a given configuration to " >&2
echo "create a configured Apache2 website. The website's folder will be created and an" >&2
echo "Apache vhost is created. " >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
fi
. $SCRIPT_DIR/validate-apache2-config.job
. $SCRIPT_DIR/validate-website-config.job
if [[ $HELP = y ]] ; then
exit $EXIT_SUCCESS
fi
exitOnConfigOff "APACHE2"
exitOnConfigOff "WEBSITE"
APACHE_TEMPLATE_FILE=$SCRIPT_DIR/apache-vhost.template
if [[ $APACHE2_SSL = y ]] ; then
APACHE_TEMPLATE_FILE=$SCRIPT_DIR/apache-vhost-ssl.template
fi
TEMPLATE_SERVER_NAME="{SERVER_NAME}"
TEMPLATE_DOCUMENT_ROOT="{DOCUMENT_ROOT}"
TEMPLATE_LOGS_DIR="{LOGS_DIR}"
TEMPLATE_SERVER_ADMIN="{SERVER_ADMIN}"
APACHE_SITES_AVAILABLE_DIR=$APACHE2_DIR/sites-available
APACHE_SITES_ENABLED_DIR=$APACHE2_DIR/sites-enabled
APACHE_CONFIG_FILE=$APACHE_SITES_AVAILABLE_DIR/$WEBSITE_DOMAIN_NAME.config
APACHE_ENABLED_FILE=$APACHE_SITES_ENABLED_DIR/$WEBSITE_DOMAIN_NAME.config
if [[ $VERBOSE = y ]] ; then
echo "Assuming the path \"$APACHE_SITES_AVAILABLE_DIR\" to exist ..." >&2
echo "Assuming the path \"$APACHE_SITES_ENABLED_DIR\" to exist ..." >&2
echo "Assuming the path \"$APACHE_TEMPLATE_FILE\" to exist ..." >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
fi
exitOnMissingVariablePath "APACHE_SITES_AVAILABLE_DIR"
exitOnMissingVariablePath "APACHE_SITES_ENABLED_DIR"
exitOnMissingVariablePath "APACHE_TEMPLATE_FILE"
if [[ $VERBOSE = y ]] ; then
echo "Setting up website's HTDOCS folder \"$WEBSITE_DIR\" ..." >&2
fi
makePath "$WEBSITE_DIR" "$WEBSITE_FS_USER" "$WEBSITE_FS_GROUP" "o-rwx,ug+rwx"
exitOnError "Unable to create directory \"$WEBSITE_DIR\" !!!"
if [[ $VERBOSE = y ]] ; then
echo "Site folder \"$WEBSITE_DIR\" created/updated ..." >&2
echo "Setting up Apache virtual host configuration for \"$WEBSITE_DOMAIN_NAME\" ..." >&2
fi
cp $APACHE_TEMPLATE_FILE $APACHE_CONFIG_FILE
exitOnError "Unable to copy \"$APACHE_TEMPLATE_FILE\" to \"$APACHE_CONFIG_FILE\" !!!"
sed -i 's/'$TEMPLATE_SERVER_NAME'/'$WEBSITE_DOMAIN_NAME'/g' $APACHE_CONFIG_FILE
sed -i 's#'$TEMPLATE_LOGS_DIR'#'$WEBSITE_LOGS_DIR'#g' $APACHE_CONFIG_FILE
sed -i 's#'$TEMPLATE_DOCUMENT_ROOT'#'$WEBSITE_DIR'#g' $APACHE_CONFIG_FILE
sed -i 's/'$TEMPLATE_SERVER_ADMIN'/'$WEBSITE_SERVER_ADMIN_EMAIL'/g' $APACHE_CONFIG_FILE
exitOnError "Unable to apply changes to \"$APACHE_CONFIG_FILE\" !!!"
chown root:root $APACHE_CONFIG_FILE
exitOnError "Unable to change owner to \"roor:root\" for \"$APACHE_CONFIG_FILE\" !!!"
chmod u+rw,go+r,go-w,gou-x $APACHE_CONFIG_FILE
exitOnError "Unable to change access rights for \"$APACHE_CONFIG_FILE\" !!!"
if [[ $VERBOSE = y ]] ; then
echo "Apache virtual host configuration \"$APACHE_CONFIG_FILE\" created ..." >&2
fi
ln -f -s $APACHE_CONFIG_FILE $APACHE_ENABLED_FILE
exitOnError "Unable link \"$APACHE_CONFIG_FILE\" to \"$APACHE_ENABLED_FILE\" !!!"
if [[ $VERBOSE = y ]] ; then
echo "Apache virtual host configuration linked to \"$APACHE_ENABLED_FILE\" ..." >&2
fi
if [[ $VERBOSE = y ]] ; then
echo "Setting up Apache logs folder for \"$WEBSITE_DOMAIN_NAME\" ..." >&2
fi
makePath "$WEBSITE_LOGS_DIR" "$WEBSITE_FS_USER" "$WEBSITE_FS_GROUP" "o-rwx,ug+rwx"
exitOnError "Unable to create directory \"$WEBSITE_LOGS_DIR\" !!!"
if [[ $VERBOSE = y ]] ; then
echo "Apache logs folder \"$WEBSITE_LOGS_DIR\" created/updated ..." >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
fi
echo "Website for \"$WEBSITE_DOMAIN_NAME\" was created:" >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
echo "+ Place your HTDOCS in: \"$WEBSITE_DIR\"" >&2
echo "+ Logs are written to: \"$WEBSITE_LOGS_DIR\"" >&2
echo "+ Apache configuration is: \"$APACHE_CONFIG_FILE\"" >&2
echo "+ Configuration enabled is: \"$APACHE_ENABLED_FILE\"" >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
echo "Change owner, group and access rights of directories inbetween \"$WEBSITE_DIR\" as needed !" >&2
echo "Change owner, group and access rights of directories inbetween \"$WEBSITE_LOGS_DIR\" as needed !" >&2
echo "You may need to apply the job \"configure-folder-over-ssh-server.job\" to enable SSH operations !" >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
showSuccess "Created \"$WEBSITE_DOMAIN_NAME\" website !"