2021-04-04 13:28:22 -07:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# vhost-stack
|
|
|
|
# https://git.stack-source.com/msb/vhost-stack
|
2022-08-22 13:22:16 -07:00
|
|
|
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2021-04-04 13:28:22 -07:00
|
|
|
|
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 ""
|
2021-10-05 11:33:24 -07:00
|
|
|
echo "usage: $thisfilename -d <domain> [OPTIONS]"
|
2021-09-16 16:21:35 -07:00
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
2021-10-05 11:33:24 -07:00
|
|
|
echo " -d <domain> Domain name of VirtualHost to remove."
|
|
|
|
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."
|
2021-09-16 16:21:35 -07:00
|
|
|
echo " -j Whether or not to jail the user. Optional, default is to not jail user."
|
2021-10-05 11:57:44 -07:00
|
|
|
echo " -w Write user & mysql info to files. Warning! This inlcudes the unencrypted passwords."
|
2021-09-16 16:21:35 -07:00
|
|
|
exit
|
|
|
|
}
|
2021-04-04 13:28:22 -07:00
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
vhost:getoptions "$@"
|
2021-04-04 13:28:22 -07:00
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
# check for domain (virtualhost)
|
|
|
|
if [[ -z $domain ]]; then
|
|
|
|
echo "domain is required"
|
|
|
|
exit
|
2021-08-24 14:59:23 -07:00
|
|
|
fi
|
2021-08-14 13:33:58 -07:00
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -d /srv/www/$domain ]] || [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
|
|
|
|
echo "virtualhost for $domain already installed"
|
2021-04-04 13:28:22 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check for and set username
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -z "$username" ]]; then
|
2022-09-13 12:58:18 -07:00
|
|
|
strippeddomain=`echo $domain | sed 's|\.||'`
|
|
|
|
strippeddomain=`echo $strippeddomain | sed 's|-||'`
|
|
|
|
username=`echo ${strippeddomain:0:8}`
|
2021-10-02 15:31:35 -07:00
|
|
|
if grep -q "^$username:" /etc/passwd; then
|
|
|
|
# username already exists, try another
|
2022-09-13 12:58:18 -07:00
|
|
|
username=`echo ${strippeddomain:0:7}`
|
2021-10-02 15:31:35 -07:00
|
|
|
if grep -q "^$username:" /etc/passwd; then
|
|
|
|
# username already exists, try another
|
2022-09-13 12:58:18 -07:00
|
|
|
username=`echo ${strippeddomain:0:6}`
|
2021-10-02 15:31:35 -07:00
|
|
|
if grep -q "^$username:" /etc/passwd; then
|
|
|
|
# username already exists, try another
|
2022-09-13 12:58:18 -07:00
|
|
|
username=`echo ${strippeddomain:0:5}`
|
2021-10-02 15:31:35 -07:00
|
|
|
if grep -q "^$username:" /etc/passwd; then
|
|
|
|
# username already exists, try another
|
2022-09-13 12:58:18 -07:00
|
|
|
username=`echo ${strippeddomain:0:9}`
|
2021-10-02 15:31:35 -07:00
|
|
|
if grep -q "^$username:" /etc/passwd; then
|
|
|
|
# username already exists, try another
|
2022-09-13 12:58:18 -07:00
|
|
|
username=`echo ${strippeddomain:0:10}`
|
2021-10-02 15:31:35 -07:00
|
|
|
if grep -q "^$username:" /etc/passwd; then
|
|
|
|
# username already exists, try another
|
2022-09-13 12:58:18 -07:00
|
|
|
username=`echo ${strippeddomain:0:11}`
|
2021-10-02 15:31:35 -07:00
|
|
|
if grep -q "^$username:" /etc/passwd; then
|
|
|
|
# username already exists, try another
|
2022-09-13 12:58:18 -07:00
|
|
|
username=`echo ${strippeddomain:0:12}`
|
2021-10-02 15:31:35 -07:00
|
|
|
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
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -z "$password" ]]; then
|
2021-04-04 13:28:22 -07:00
|
|
|
password=`/usr/bin/pwgen 12 1`
|
|
|
|
fi
|
2021-08-14 13:04:46 -07:00
|
|
|
# add user
|
2021-10-05 11:57:44 -07:00
|
|
|
if [[ -n $write ]]; then
|
2021-10-05 13:46:59 -07:00
|
|
|
/usr/local/bin/vhost-user-add.sh -u $username -p "$password" -w
|
2021-10-05 11:57:44 -07:00
|
|
|
else
|
2021-10-05 13:46:59 -07:00
|
|
|
/usr/local/bin/vhost-user-add.sh -u $username -p "$password"
|
2021-10-05 11:57:44 -07:00
|
|
|
fi
|
2021-08-14 13:04:46 -07:00
|
|
|
# if jail option is set then jail user
|
2021-10-05 11:33:24 -07:00
|
|
|
if [[ -n $jail ]]; then
|
2022-09-01 15:32:26 -07:00
|
|
|
# job is run in the background as it can take about 1 minute to complete
|
|
|
|
/usr/local/bin/vhost-user-jail.sh -u $username >/dev/null 2>/dev/null &
|
|
|
|
# pause to give above script time to create jail home dir which is used by vhost-add.sh below
|
|
|
|
sleep 1
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2021-08-14 13:04:46 -07:00
|
|
|
# add virtualhost
|
2021-10-05 13:46:59 -07:00
|
|
|
/usr/local/bin/vhost-add.sh -d $domain -u $username > /dev/null 2>&1
|
2021-09-16 16:21:35 -07:00
|
|
|
|
|
|
|
# add mysql database
|
2021-10-05 11:57:44 -07:00
|
|
|
if [[ -n $write ]]; then
|
2021-10-05 13:46:59 -07:00
|
|
|
/usr/local/bin/vhost-mysql-db-add.sh -d $domain -w > /dev/null 2>&1
|
2021-10-05 11:57:44 -07:00
|
|
|
else
|
2021-10-05 13:46:59 -07:00
|
|
|
/usr/local/bin/vhost-mysql-db-add.sh -d $domain > /dev/null 2>&1
|
2021-10-05 11:57:44 -07:00
|
|
|
fi
|