2021-04-04 14:15:16 -07:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2022-08-22 13:22:16 -07:00
|
|
|
# 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)
|
2021-04-04 13:28:22 -07:00
|
|
|
|
|
|
|
# any script that includes this conf file will force user to be root
|
|
|
|
if [ "$USER" != "root" ]; then
|
2022-04-20 12:02:18 -07:00
|
|
|
exec sudo -u root $0 $@
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# constants
|
|
|
|
|
2023-04-16 10:49:38 -07:00
|
|
|
FPM_MAX=4
|
2023-05-04 17:00:35 -07:00
|
|
|
JAIL_USER=1
|
2023-05-04 17:15:27 -07:00
|
|
|
WRITE_INFO=1
|
2023-04-16 10:49:38 -07:00
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# functions
|
|
|
|
|
|
|
|
function vhost::set-virtualhostArray () {
|
|
|
|
|
|
|
|
cd /srv/www
|
|
|
|
virtualhostArray=(`ls -1|grep -v ^html$`)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function vhost::set-phpVersion () {
|
|
|
|
|
|
|
|
PHP_MAJOR_VERSION=`php -r "echo PHP_MAJOR_VERSION;"`
|
|
|
|
PHP_MINOR_VERSION=`php -r "echo PHP_MINOR_VERSION;"`
|
|
|
|
phpVersion=$PHP_MAJOR_VERSION.$PHP_MINOR_VERSION
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-09-18 12:35:55 -07:00
|
|
|
function vhost::set-phpVersionArray () {
|
|
|
|
|
|
|
|
mapfile -t phpVersionArray < <( /usr/bin/update-alternatives --list php-fpm.sock | sed "s|/run/php/php||g" | cut -d \- -f 1 )
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-30 14:57:56 -07:00
|
|
|
function vhost::set-opensslpass () {
|
|
|
|
|
|
|
|
if [[ -f "/root/.vhost.ini" ]]; then
|
|
|
|
opensslpass=`grep -E '^opensslpass\s?=' /root/.vhost.ini | cut -d = -f 2 | tr -d ' '`
|
|
|
|
else
|
|
|
|
install --owner=root --group=root --mode=640 /dev/null /root/.vhost.ini
|
|
|
|
opensslpass=`/usr/bin/pwgen 16 1`
|
|
|
|
echo "opensslpass = $opensslpass" >> /root/.vhost.ini
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# crude but good enough domain name format validation
|
|
|
|
function vhost::validate_domain () {
|
|
|
|
local my_domain=$1
|
2021-10-02 14:54:58 -07:00
|
|
|
if [[ $my_domain =~ ^(([a-zA-Z0-9](-?[a-zA-Z0-9])*)\.)+[a-zA-Z]{2,}$ ]] ; then
|
2021-04-04 13:28:22 -07:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2021-09-16 16:21:35 -07:00
|
|
|
|
2021-10-05 11:33:24 -07:00
|
|
|
function vhost:getoptions () {
|
|
|
|
local OPTIND
|
2024-09-18 12:35:55 -07:00
|
|
|
while getopts "cd:f:i:m:o:p:u:j:hnvw:x:" opt ; do
|
2021-10-05 11:33:24 -07:00
|
|
|
case "${opt}" in
|
|
|
|
h ) # display help and exit
|
|
|
|
help
|
|
|
|
exit
|
|
|
|
;;
|
2022-05-02 16:19:00 -07:00
|
|
|
c ) # cvs - output in cvs format
|
|
|
|
cvs=true
|
|
|
|
;;
|
2021-10-05 11:33:24 -07:00
|
|
|
d ) # domain name (virtualhost) to act on
|
|
|
|
domain=${OPTARG,,}
|
|
|
|
if ! vhost::validate_domain $domain; then
|
|
|
|
echo "ERROR: $domain is not a valid domain name."
|
2023-05-10 15:23:54 -07:00
|
|
|
exit 1
|
2021-10-05 11:33:24 -07:00
|
|
|
fi
|
|
|
|
;;
|
2024-09-18 12:35:55 -07:00
|
|
|
f ) # php-fpm version
|
|
|
|
fpm=${OPTARG}
|
|
|
|
vhost::set-phpVersionArray
|
|
|
|
if [[ ! " ${phpVersionArray[@]} " =~ " ${fpm} " ]]; then
|
|
|
|
echo "Invalid PHP-FPM version."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
2021-10-05 11:33:24 -07:00
|
|
|
i ) # User ID (UID) for new user
|
|
|
|
uid=${OPTARG}
|
|
|
|
;;
|
2021-10-05 13:08:28 -07:00
|
|
|
m ) # macro - Apache mod_macro name
|
|
|
|
macro=${OPTARG}
|
|
|
|
;;
|
|
|
|
o ) # option - usually applied to previously specified variable
|
2021-10-05 11:33:24 -07:00
|
|
|
# e.g. could be subdomain or alias depending on the macro defined
|
|
|
|
option=${OPTARG}
|
|
|
|
;;
|
|
|
|
p ) # password
|
|
|
|
password=${OPTARG}
|
|
|
|
;;
|
|
|
|
u ) # username
|
|
|
|
username=${OPTARG,,}
|
|
|
|
;;
|
2023-05-04 17:00:35 -07:00
|
|
|
j ) # jail - Whether or not to jail the user. 0 = no, 1 = yes.
|
|
|
|
jail=${OPTARG}
|
|
|
|
if [[ $jail != "0" ]] && [[ $jail != "1" ]]; then
|
|
|
|
echo "ERROR: Invalid jail setting: -j $jail."
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-10-05 11:33:24 -07:00
|
|
|
;;
|
|
|
|
n ) # dry-run
|
|
|
|
dryrun=true
|
|
|
|
;;
|
|
|
|
v ) # verbose
|
|
|
|
verbose=true
|
|
|
|
;;
|
|
|
|
w ) # write - store data in file
|
2023-05-04 17:15:27 -07:00
|
|
|
write=${OPTARG}
|
|
|
|
if [[ $write != "0" ]] && [[ $write != "1" ]]; then
|
2023-05-10 15:23:54 -07:00
|
|
|
echo "ERROR: Invalid write setting: -w $write"
|
2023-05-04 17:15:27 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
2021-10-05 11:33:24 -07:00
|
|
|
;;
|
2023-04-10 11:36:52 -07:00
|
|
|
x ) # php-fpm pm.max_children
|
|
|
|
fpmmax=${OPTARG}
|
|
|
|
if [[ $fpmmax != +([[:digit:]]) ]] || [[ $fpmmax -eq 0 ]]; then
|
|
|
|
echo "ERROR: $fpmax for -x max_children not a valid number."
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
;;
|
2021-10-05 11:33:24 -07:00
|
|
|
\? )
|
|
|
|
echo "Invalid option: $OPTARG"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
: )
|
|
|
|
echo "Invalid option: $OPTARG requires an argument"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
}
|
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
# check for local config, which can be used to override any of the above
|
|
|
|
if [[ -f /usr/local/etc/vhost.conf ]]; then
|
|
|
|
source /usr/local/etc/vhost.conf
|
|
|
|
fi
|