vhost-stack/bin/vhost-deploy.sh

78 lines
1.8 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
# -v virtualhost (required)
# -u username (optional)
# -p password (optional)
# -j jail (true or false, optional)
while getopts ":v:u:p:j:" opt; do
case "${opt}" in
v )
virtualhost=${OPTARG}
;;
u )
username=${OPTARG}
;;
p )
password=${OPTARG}
;;
j )
jail=${OPTARG}
;;
\? )
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-14 13:33:58 -07:00
if ! vhost::validate_domain $virtualhost; then
echo "ERROR: $virtualhost is not a valid domain name."
exit 1
fi
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}`
fi
if ! grep -q "^$username:" /etc/passwd; then
# check for and set password
if [ ! -n "$password" ]; then
password=`/usr/bin/pwgen 12 1`
fi
# set userid
userid=`awk -F: '{uid[$3]=1}END{for(x=1000; x<=65534; x++) {if(uid[x] != ""){}else{print x; exit;}}}' /etc/passwd`
2021-08-14 13:04:46 -07:00
# add user
/usr/local/bin/user-add.sh $username "$password" $userid
# if jail option is set then jail user
2021-04-04 13:28:22 -07:00
if [[ $jail = true ]]; then
2021-08-14 13:04:46 -07:00
/usr/local/bin/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
/usr/local/bin/vhost-add.sh $virtualhost $username > /dev/null 2>&1 &