vhost-stack/bin/vhost-deploy.sh

138 lines
3.9 KiB
Bash
Raw Normal View History

2021-04-04 13:28:22 -07:00
#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
2021-04-04 14:15:16 -07:00
# load include file
source $(dirname $0)/vhost.sh
2021-04-04 13:28:22 -07:00
2021-09-16 16:21:35 -07:00
help()
{
thisfilename=$(basename -- "$0")
echo "Add virtualhost to this server, including shell user and MySQL database."
echo ""
echo "usage: $thisfilename virtualhost [OPTIONS]"
echo ""
echo " -h Print this help."
2021-10-02 15:31:35 -07:00
echo " -u USERNAME Username to use for this virtualhost. Optional, defaults to first 8 alphanumeric characters of virtualhost."
2021-09-16 16:21:35 -07:00
echo " -p PASSWORD 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
}
2021-04-04 13:28:22 -07:00
2021-09-16 16:21:35 -07:00
# check for and set virtualhost
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
elif vhost::validate_domain $1; then
virtualhost="${1,,}"
2021-10-02 15:31:35 -07:00
shift
2021-09-16 16:21:35 -07:00
else
echo "ERROR: Invalid virtualhost: $1"
exit 1
fi
else
help
fi
while getopts "hu:p:j" opt; do
2021-04-04 13:28:22 -07:00
case "${opt}" in
2021-09-16 16:21:35 -07:00
h )
help
exit;;
2021-04-04 13:28:22 -07:00
u )
username=${OPTARG}
;;
p )
password=${OPTARG}
;;
j )
2021-09-16 16:21:35 -07:00
jail=true
2021-04-04 13:28:22 -07:00
;;
\? )
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
2021-08-24 14:59:23 -07:00
fi
2021-08-14 13:33:58 -07:00
if [ -d /srv/www/$virtualhost ] || [ -f /etc/apache2/sites-available/$virtualhost.conf ]; then
2021-04-04 13:28:22 -07:00
echo "virtualhost for $virtualhost already installed"
exit 1
fi
# check for and set username
if [ ! -n "$username" ]; then
username=`echo $virtualhost | sed 's|\.||'`
username=`echo ${username:0:8}`
2021-10-02 15:31:35 -07:00
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $virtualhost | 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 ${username:0:6}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $virtualhost | 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 ${username:0:9}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $virtualhost | 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 ${username:0:11}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $virtualhost | 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"
exit 1
fi
fi
fi
fi
fi
fi
fi
fi
2021-04-04 13:28:22 -07:00
fi
if ! grep -q "^$username:" /etc/passwd; then
# check for and set password
if [ ! -n "$password" ]; then
password=`/usr/bin/pwgen 12 1`
fi
2021-08-14 13:04:46 -07:00
# add user
2021-09-16 16:21:35 -07:00
/usr/local/bin/vhost-user-add.sh $username -p "$password"
2021-08-14 13:04:46 -07:00
# if jail option is set then jail user
2021-04-04 13:28:22 -07:00
if [[ $jail = true ]]; then
2021-08-24 15:03:50 -07:00
/usr/local/bin/vhost-user-jail.sh $username > /dev/null 2>&1
2021-04-04 13:28:22 -07:00
fi
fi
2021-08-14 13:04:46 -07:00
# add virtualhost
2021-08-24 14:59:06 -07:00
/usr/local/bin/vhost-add.sh $virtualhost $username > /dev/null 2>&1
2021-09-16 16:21:35 -07:00
# add mysql database
/usr/local/bin/vhost-mysql-db-add.sh $virtualhost > /dev/null 2>&1