diff --git a/bin/vhost-add.sh b/bin/vhost-add.sh index 1e1a25c..e480ca6 100755 --- a/bin/vhost-add.sh +++ b/bin/vhost-add.sh @@ -12,84 +12,64 @@ help() thisfilename=$(basename -- "$0") echo "Add virtualhost to this server." echo "" - echo "usage: $thisfilename virtualhost username [OPTIONS]" + echo "usage: $thisfilename -d -u [-h]" echo "" echo " -h Print this help." - echo "" - echo " Username must already exist. If need be run vhost-user-add.sh first." + echo " -d Domain name to add as a VirtualHost. www. subdomain is automatically aliased." + echo " -u Username to install VirtualHost for. Username must already exist." + echo " If need be run vhost-user-add.sh first." echo " Or use vhost-deploy.sh instead to automatically generate username." - exit } -# check for and set virtualhost & username -if [ -n "$1" ]; then - if [ $1 == "-h" ]; then - help - else - # virtualhost - if vhost::validate_domain $1; then - virtualhost="${1,,}" - else - echo "ERROR: $1 is not a valid domain name." - exit 1 - fi - # username - if [ -n "$2" ]; then - if [ $2 == "-h" ]; then - help - else - username="${2,,}" - fi - else - echo "username not set" - exit 1 - fi - # last check for -h - if [ -n "$3" ]; then - if [ $3 == "-h" ]; then - help - fi - fi - fi -else - help +vhost:getoptions "$@" + +# check for domain (virtualhost) +if [[ -z $domain ]]; then + echo "domain is required" + exit fi -if [ ! -d /home/$username ]; then +# check for username +if [[ -z $username ]]; then + echo "username is required" + exit +fi + +if [[ ! -d /home/$username ]]; then echo "home dir for $username does not exist" exit 1 fi -if [ -d /srv/www/$virtualhost ]; then - chown $username:$username /srv/www/$virtualhost - chmod 755 /srv/www/$virtualhost +if [[ -d /srv/www/$domain ]]; then + chown $username:$username /srv/www/$domain + chmod 755 /srv/www/$domain else - install -d -o $username -g $username -m 755 /srv/www/$virtualhost + install -d -o $username -g $username -m 755 /srv/www/$domain fi -if [ -d /srv/www/$virtualhost/html ]; then - chown $username:$username /srv/www/$virtualhost/html - chmod 755 /srv/www/$virtualhost/html +if [[ -d /srv/www/$domain/html ]]; then + chown $username:$username /srv/www/$domain/html + chmod 755 /srv/www/$domain/html else - install -d -o $username -g $username -m 755 /srv/www/$virtualhost/html + install -d -o $username -g $username -m 755 /srv/www/$domain/html fi -if [ ! -e /home/$username/$virtualhost ]; then - ln -s /srv/www/$virtualhost /home/$username/$virtualhost - chown -h $username:$username /home/$username/$virtualhost +if [[ ! -e /home/$username/$domain ]]; then + ln -s /srv/www/$domain /home/$username/$domain + chown -h $username:$username /home/$username/$domain fi -if [ -d /usr/jails/$username ]; then - if [ ! -d /usr/jails/$username/srv/www/$virtualhost ]; then - install -d -o $username -g $username -m 755 /usr/jails/$username/srv/www/$virtualhost - mount --bind /srv/www/$virtualhost /usr/jails/$username/srv/www/$virtualhost - echo "/srv/www/$virtualhost /usr/jails/$username/srv/www/$virtualhost none bind 0 0" >> /etc/fstab.jails +if [[ -d /usr/jails/$username ]]; then + if [[ ! -d /usr/jails/$username/srv/www/$domain ]]; then + install -d -o $username -g $username -m 755 /usr/jails/$username/srv/www/$domain + mount --bind /srv/www/$domain /usr/jails/$username/srv/www/$domain + echo "/srv/www/$domain /usr/jails/$username/srv/www/$domain none bind 0 0" >> /etc/fstab.jails fi fi # php-fpm pool vhost::set-phpVersion -if [ ! -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]; then +if [[ ! -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]]; then # create /etc/php/$phpVersion/fpm/pool.d/$username.conf echo "[$username]" > /etc/php/$phpVersion/fpm/pool.d/$username.conf echo "user = $username" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf @@ -103,7 +83,7 @@ if [ ! -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]; then echo "pm = ondemand" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf echo "pm.max_children = 12" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf echo "pm.process_idle_timeout = 3s;" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf - echo "php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -fwebmaster@$virtualhost" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf + echo "php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -fwebmaster@$domain" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf # restart php$phpVersion-fpm if systemctl is-active --quiet php$phpVersion-fpm ; then if /usr/sbin/php-fpm$phpVersion -t >/dev/null 2>&1 ; then @@ -115,4 +95,4 @@ if [ ! -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]; then fi # create & enable apache config -/usr/local/bin/vhost-enable.sh VHostHTTP $virtualhost +/usr/local/bin/vhost-enable.sh VHostHTTP $domain diff --git a/bin/vhost-del.sh b/bin/vhost-del.sh index 3268bff..a8d77ce 100755 --- a/bin/vhost-del.sh +++ b/bin/vhost-del.sh @@ -12,76 +12,74 @@ help() thisfilename=$(basename -- "$0") echo "Removes virtualhost from server." echo "" - echo "usage: $thisfilename virtualhost [OPTIONS]" + echo "usage: $thisfilename -d [-h]" echo "" + echo " -d Domain name of VirtualHost to remove." echo " -h Print this help." exit } -# check for and set virtualhost -if [ -n "$1" ]; then - if [ $1 == "-h" ]; then - help - else - virtualhost="${1,,}" - fi -else - help +vhost:getoptions "$@" + +# check for domain (virtualhost) +if [[ -z $domain ]]; then + echo "domain is required" + exit fi -# remove virtualhost dir -if [ ! -d /srv/www/$virtualhost ]; then +# check for virtualhost dir +if [[ ! -d /srv/www/$domain ]]; then echo "virtualhost dir does not exist" exit 1 fi -username=$(stat -c '%U' /srv/www/$virtualhost) +username=$(stat -c '%U' /srv/www/$domain) # disable the apache conf and reload apache -if [ -h /etc/apache2/sites-enabled/$virtualhost.conf ]; then - a2dissite --quiet $virtualhost +if [[ -h /etc/apache2/sites-enabled/$domain.conf ]]; then + a2dissite --quiet $domain systemctl --quiet is-active apache2 && systemctl --quiet reload apache2 fi # remove the apache config -if [ -f /etc/apache2/sites-available/$virtualhost.conf ]; then - rm /etc/apache2/sites-available/$virtualhost.conf +if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then + rm /etc/apache2/sites-available/$domain.conf fi # remove varnish config -if [ -f /etc/varnish/sites.d/$virtualhost.vcl ]; then - rm /etc/varnish/sites.d/$virtualhost.vcl +if [[ -f /etc/varnish/sites.d/$domain.vcl ]]; then + rm /etc/varnish/sites.d/$domain.vcl /usr/local/bin/vhost-varnish-update-sites.sh # don't bother to restart varnish as it will clear cache unnecessarily fi # if virtualhost is mounted in a jail, unmount it -if grep -q "^/dev/sda /usr/jails/$username/srv/www/$virtualhost " /etc/mtab; then - umount /usr/jails/$username/srv/www/$virtualhost +if grep -q "^/dev/sda /usr/jails/$username/srv/www/$domain " /etc/mtab; then + umount /usr/jails/$username/srv/www/$domain fi # if virtualhost mount in fstab.jails exists remove it -if grep -q "/usr/jails/$username/srv/www/$virtualhost" /etc/fstab.jails; then - sed -i "\|/usr/jails/$username/srv/www/$virtualhost|d" /etc/fstab.jails +if grep -q "/usr/jails/$username/srv/www/$domain" /etc/fstab.jails; then + sed -i "\|/usr/jails/$username/srv/www/$domain|d" /etc/fstab.jails fi # if virtualhost symlink exists in jail remove it -if [ -h /usr/jails/$username/home/$username/$virtualhost ]; then - unlink /usr/jails/$username/home/$username/$virtualhost +if [[ -h /usr/jails/$username/home/$username/$domain ]]; then + unlink /usr/jails/$username/home/$username/$domain fi # if virtualhost symlink exists in home dir remove it -if [ -h /home/$username/$virtualhost ]; then - unlink /home/$username/$virtualhost +if [[ -h /home/$username/$domain ]]; then + unlink /home/$username/$domain fi # if virtualhost dir exists in jail remove it -if [ -d /usr/jails/$username/srv/www/$virtualhost ]; then - rm -r /usr/jails/$username/srv/www/$virtualhost +if [[ -d /usr/jails/$username/srv/www/$domain ]]; then + rm -r /usr/jails/$username/srv/www/$domain fi # remove virtualhost dir -if [ -d /srv/www/$virtualhost ]; then - rm -r /srv/www/$virtualhost +if [[ -d /srv/www/$domain ]]; then + rm -r /srv/www/$domain fi diff --git a/bin/vhost-deploy.sh b/bin/vhost-deploy.sh index cd31ab9..5c8083f 100755 --- a/bin/vhost-deploy.sh +++ b/bin/vhost-deploy.sh @@ -12,97 +12,60 @@ help() thisfilename=$(basename -- "$0") echo "Add virtualhost to this server, including shell user and MySQL database." echo "" - echo "usage: $thisfilename virtualhost [OPTIONS]" + echo "usage: $thisfilename -d [OPTIONS]" echo "" echo " -h Print this help." - echo " -u USERNAME Username to use for this virtualhost. Optional, defaults to first 8 alphanumeric characters of virtualhost." - echo " -p PASSWORD Password for username. Optional, random password generated if none specified." + echo " -d Domain name of VirtualHost to remove." + echo " -u Username to use for this virtualhost. Optional, defaults to first 8 alphanumeric characters of virtualhost." + echo " -p Password for username. Optional, random password generated if none specified." echo " -j Whether or not to jail the user. Optional, default is to not jail user." exit } -# check for and set virtualhost -if [ -n "$1" ]; then - if [ $1 == "-h" ]; then - help - elif vhost::validate_domain $1; then - virtualhost="${1,,}" - shift - else - echo "ERROR: Invalid virtualhost: $1" - exit 1 - fi -else - help +vhost:getoptions "$@" + +# check for domain (virtualhost) +if [[ -z $domain ]]; then + echo "domain is required" + exit fi -while getopts "hu:p:j" opt; do - case "${opt}" in - h ) - help - exit;; - u ) - username=${OPTARG} - ;; - p ) - password=${OPTARG} - ;; - j ) - jail=true - ;; - \? ) - echo "Invalid option: $OPTARG" - exit 1 - ;; - : ) - echo "Invalid option: $OPTARG requires an argument" - exit 1 - ;; - esac -done - -# check virtualhost -if [ ! -n "$virtualhost" ]; then - echo "virtualhost not set" - exit 1 -fi - -if [ -d /srv/www/$virtualhost ] || [ -f /etc/apache2/sites-available/$virtualhost.conf ]; then - echo "virtualhost for $virtualhost already installed" +if [[ -d /srv/www/$domain ]] || [[ -f /etc/apache2/sites-available/$domain.conf ]]; then + echo "virtualhost for $domain already installed" exit 1 fi # check for and set username -if [ ! -n "$username" ]; then - username=`echo $virtualhost | sed 's|\.||'` +if [[ -z "$username" ]]; then + username=`echo $domain | sed 's|\.||'` username=`echo ${username:0:8}` if grep -q "^$username:" /etc/passwd; then # username already exists, try another - username=`echo $virtualhost | sed 's|\.||'` + username=`echo $domain | sed 's|\.||'` username=`echo ${username:0:7}` if grep -q "^$username:" /etc/passwd; then # username already exists, try another - username=`echo $virtualhost | sed 's|\.||'` + username=`echo $domain | sed 's|\.||'` username=`echo ${username:0:6}` if grep -q "^$username:" /etc/passwd; then # username already exists, try another - username=`echo $virtualhost | sed 's|\.||'` + username=`echo $domain | sed 's|\.||'` username=`echo ${username:0:5}` if grep -q "^$username:" /etc/passwd; then # username already exists, try another - username=`echo $virtualhost | sed 's|\.||'` + username=`echo $domain | sed 's|\.||'` username=`echo ${username:0:9}` if grep -q "^$username:" /etc/passwd; then # username already exists, try another - username=`echo $virtualhost | sed 's|\.||'` + username=`echo $domain | sed 's|\.||'` username=`echo ${username:0:10}` if grep -q "^$username:" /etc/passwd; then # username already exists, try another - username=`echo $virtualhost | sed 's|\.||'` + username=`echo $domain | sed 's|\.||'` username=`echo ${username:0:11}` if grep -q "^$username:" /etc/passwd; then # username already exists, try another - username=`echo $virtualhost | sed 's|\.||'` + username=`echo $domain | sed 's|\.||'` username=`echo ${username:0:12}` if grep -q "^$username:" /etc/passwd; then echo "trouble setting unique username, specify '-u USERNAME' to use an existing username" @@ -119,19 +82,19 @@ fi if ! grep -q "^$username:" /etc/passwd; then # check for and set password - if [ ! -n "$password" ]; then + if [[ -z "$password" ]]; then password=`/usr/bin/pwgen 12 1` fi # add user /usr/local/bin/vhost-user-add.sh $username -p "$password" # if jail option is set then jail user - if [[ $jail = true ]]; then + if [[ -n $jail ]]; then /usr/local/bin/vhost-user-jail.sh $username > /dev/null 2>&1 fi fi # add virtualhost -/usr/local/bin/vhost-add.sh $virtualhost $username > /dev/null 2>&1 +/usr/local/bin/vhost-add.sh $domain $username > /dev/null 2>&1 # add mysql database -/usr/local/bin/vhost-mysql-db-add.sh $virtualhost > /dev/null 2>&1 +/usr/local/bin/vhost-mysql-db-add.sh $domain > /dev/null 2>&1 diff --git a/bin/vhost-destroy.sh b/bin/vhost-destroy.sh index 2140182..2ec1862 100755 --- a/bin/vhost-destroy.sh +++ b/bin/vhost-destroy.sh @@ -12,40 +12,41 @@ help() thisfilename=$(basename -- "$0") echo "Remove virtualhost and associated user & database & db user from this server." echo "" - echo "usage: $thisfilename virtualhost" + echo "usage: $thisfilename -d " echo "" echo " -h Print this help." + echo " -d Domain name of VirtualHost to remove." exit } -# check for and set virtualhost -if [ -n "$1" ]; then - if [ $1 == "-h" ]; then - help - elif [ ! -d /srv/www/$1 ]; then - echo "virtualhost dir does not exist" - exit 1 - else - virtualhost="${1,,}" - fi -else - help +vhost:getoptions "$@" + +# check for domain (virtualhost) +if [[ -z $domain ]]; then + echo "domain is required" + exit +fi + +# check that virtualhost dir exists +if [[ ! -d /srv/www/$domain ]]; then + echo "virtualhost dir does not exist" + exit 1 fi # check for database and delete if it exists -database=${virtualhost//./dot} +database=${domain//./dot} database=${database//-/dash} -if [ -d /var/lib/mysql/$database ]; then - /usr/local/bin/vhost-mysql-db-del.sh $virtualhost +if [[ -d /var/lib/mysql/$database ]]; then + /usr/local/bin/vhost-mysql-db-del.sh $domain fi # get & set username for this virtualhost -username=$(stat -c '%U' /srv/www/$virtualhost) +username=$(stat -c '%U' /srv/www/$domain) # check for a delete varnish config # del virtualhost files & configs -/usr/local/bin/vhost-del.sh $virtualhost +/usr/local/bin/vhost-del.sh $domain # check for any remaining virtualhosts before deleting user # same check is done in vhost-user-del.sh @@ -53,11 +54,11 @@ username=$(stat -c '%U' /srv/www/$virtualhost) vhost::set-virtualhostArray for v in "${virtualhostArray[@]}" do - if [ $(stat -c '%U' /srv/www/$v) = $username ]; then + if [[ $(stat -c '%U' /srv/www/$v) = $username ]]; then existingvirtualhosts=true fi done -if [ -n "$existingvirtualhosts" ]; then +if [[ -n "$existingvirtualhosts" ]]; then /usr/local/bin/vhost-user-del.sh $username fi diff --git a/bin/vhost-disable.sh b/bin/vhost-disable.sh index d165187..b23ef7f 100755 --- a/bin/vhost-disable.sh +++ b/bin/vhost-disable.sh @@ -12,24 +12,21 @@ help() thisfilename=$(basename -- "$0") echo "Disable Apache config for specified virtualhost." echo "" - echo "usage: $thisfilename virtualhost" + echo "usage: $thisfilename -d " echo "" echo " -h Print this help." + echo " -d Domain name of VirtualHost to remove." exit } -# check for and set virtualhost -if [ -n "$1" ]; then - if [ $1 == "-h" ]; then - help - else - virtualhost="${1,,}" - fi -else - echo "virtualhost not set" - exit 1 +vhost:getoptions "$@" + +# check for domain (virtualhost) +if [[ -z $domain ]]; then + echo "domain is required" + exit fi -if [[ -h /etc/apache2/sites-enabled/$virtualhost.conf ]]; then - a2dissite --quiet $virtualhost && systemctl --quiet is-active apache2 && systemctl --quiet reload apache2 +if [[ -h /etc/apache2/sites-enabled/$domain.conf ]]; then + a2dissite --quiet $domain && systemctl --quiet is-active apache2 && systemctl --quiet reload apache2 fi diff --git a/bin/vhost-enable.sh b/bin/vhost-enable.sh index 62c9b14..6295053 100755 --- a/bin/vhost-enable.sh +++ b/bin/vhost-enable.sh @@ -12,11 +12,14 @@ help() thisfilename=$(basename -- "$0") echo "Enable Apache config for virtualhost." echo "" - echo "usage: $thisfilename macro_name vhost [subdomain|alias]" + echo "usage: $thisfilename -d -m [-o ||] [-h]" echo "" - echo " -h Print this help." + echo " -h Print this help." + echo " -d Domain name of VirtualHost to remove." + echo " -m Name of Apache macro to apply." + echo " -o