vhost-stack/bin/vhost-deploy.sh
2023-05-04 17:00:35 -07:00

128 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# 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)
# load include file
source $(dirname $0)/vhost.sh
help()
{
thisfilename=$(basename -- "$0")
echo "Add virtualhost to this server, including shell user and MySQL database."
echo ""
echo "usage: $thisfilename -d <domain> [-u <username>] [-p <password>] [-x <fpmmax>] [-j <0|1>] [-w] [-h]"
echo ""
echo " -h Print this help."
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."
echo " -x <fpmmax> PHP-FPM pm.max_children. Optional, defaults to 4, recommended range 2-12 on Shared Server."
echo " -j <0|1> Whether or not to jail the user. 0 = no, 1 = yes. Default is 1, which can be overriden in main config."
echo " -w Write user & mysql info to files."
exit
}
vhost:getoptions "$@"
# check for domain (virtualhost)
if [[ -z $domain ]]; then
echo "domain is required"
exit
fi
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 (length between 3 and 12 characters)
if [[ -z "$username" ]]; then
strippeddomain=`echo $domain | sed 's|\.||'`
strippeddomain=`echo $strippeddomain | sed 's|-||'`
username=`echo ${strippeddomain:0:8}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:7}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:6}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:5}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:9}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:10}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:11}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:12}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:4}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo ${strippeddomain:0:3}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
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
fi
fi
fi
fi
if ! grep -q "^$username:" /etc/passwd; then
# check for and set password
if [[ -z "$password" ]]; then
password=`/usr/bin/pwgen 12 1`
fi
# check for and set php-fpm process manager max children
if [[ -z $fpmmax ]]; then
fpmmax=$FPM_MAX
fi
# add user
if [[ -n $write ]]; then
/usr/local/bin/vhost-user-add.sh -u $username -p "$password" -x $fpmmax -w
else
/usr/local/bin/vhost-user-add.sh -u $username -p "$password" -x $fpmmax
fi
# check for and set jail option
if [[ -z $jail ]]; then
jail=$JAIL_USER
fi
# if jail option is yes (1) then jail user
if [[ $jail == 1 ]]; then
# 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
fi
fi
# add virtualhost
/usr/local/bin/vhost-add.sh -d $domain -u $username > /dev/null 2>&1
# add mysql database
if [[ -n $write ]]; then
/usr/local/bin/vhost-mysql-db-add.sh -d $domain -w > /dev/null 2>&1
else
/usr/local/bin/vhost-mysql-db-add.sh -d $domain > /dev/null 2>&1
fi