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."
|
|
|
|
echo ""
|
|
|
|
echo "usage: $thisfilename virtualhost username [OPTIONS]"
|
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
|
|
|
echo ""
|
|
|
|
echo " Username must already exist. If need be run vhost-user-add.sh first."
|
2021-10-03 12:16:40 -07:00
|
|
|
echo " Or use vhost-deploy.sh instead to automatically generate username."
|
2021-09-16 16:21:35 -07:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
# check for and set virtualhost & username
|
2021-04-04 13:28:22 -07:00
|
|
|
if [ -n "$1" ]; then
|
2021-09-16 16:21:35 -07:00
|
|
|
if [ $1 == "-h" ]; then
|
|
|
|
help
|
2021-08-14 13:33:58 -07:00
|
|
|
else
|
2021-09-16 16:21:35 -07:00
|
|
|
# virtualhost
|
|
|
|
if vhost::validate_domain $1; then
|
|
|
|
virtualhost="${1,,}"
|
|
|
|
else
|
|
|
|
echo "ERROR: $1 is not a valid domain name."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# username
|
|
|
|
if [ -n "$2" ]; then
|
|
|
|
if [ $2 == "-h" ]; then
|
|
|
|
help
|
|
|
|
else
|
|
|
|
username="${2,,}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "username not set"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# last check for -h
|
|
|
|
if [ -n "$3" ]; then
|
|
|
|
if [ $3 == "-h" ]; then
|
|
|
|
help
|
|
|
|
fi
|
|
|
|
fi
|
2021-08-14 13:33:58 -07:00
|
|
|
fi
|
2021-04-04 13:28:22 -07:00
|
|
|
else
|
2021-09-16 16:21:35 -07:00
|
|
|
help
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -d /home/$username ]; then
|
|
|
|
echo "home dir for $username does not exist"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d /srv/www/$virtualhost ]; then
|
|
|
|
chown $username:$username /srv/www/$virtualhost
|
|
|
|
chmod 755 /srv/www/$virtualhost
|
|
|
|
else
|
|
|
|
install -d -o $username -g $username -m 755 /srv/www/$virtualhost
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d /srv/www/$virtualhost/html ]; then
|
|
|
|
chown $username:$username /srv/www/$virtualhost/html
|
|
|
|
chmod 755 /srv/www/$virtualhost/html
|
|
|
|
else
|
|
|
|
install -d -o $username -g $username -m 755 /srv/www/$virtualhost/html
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -e /home/$username/$virtualhost ]; then
|
|
|
|
ln -s /srv/www/$virtualhost /home/$username/$virtualhost
|
|
|
|
chown -h $username:$username /home/$username/$virtualhost
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d /usr/jails/$username ]; then
|
|
|
|
if [ ! -d /usr/jails/$username/srv/www/$virtualhost ]; then
|
|
|
|
install -d -o $username -g $username -m 755 /usr/jails/$username/srv/www/$virtualhost
|
|
|
|
mount --bind /srv/www/$virtualhost /usr/jails/$username/srv/www/$virtualhost
|
2021-08-18 16:13:02 -07:00
|
|
|
echo "/srv/www/$virtualhost /usr/jails/$username/srv/www/$virtualhost none bind 0 0" >> /etc/fstab.jails
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# php-fpm pool
|
|
|
|
vhost::set-phpVersion
|
|
|
|
if [ ! -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]; then
|
|
|
|
# create /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "[$username]" > /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "user = $username" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "group = $username" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
if [ -d /usr/jails/$username ]; then
|
|
|
|
echo "chroot = /usr/jails/$username" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
fi
|
|
|
|
echo "listen = /run/php/php$phpVersion-fpm-$username.sock" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "listen.owner = www-data" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "listen.group = www-data" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "pm = ondemand" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "pm.max_children = 12" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "pm.process_idle_timeout = 3s;" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
echo "php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -fwebmaster@$virtualhost" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf
|
|
|
|
# restart php$phpVersion-fpm
|
|
|
|
if systemctl is-active --quiet php$phpVersion-fpm ; then
|
|
|
|
if /usr/sbin/php-fpm$phpVersion -t >/dev/null 2>&1 ; then
|
|
|
|
systemctl reload php$phpVersion-fpm
|
|
|
|
else
|
|
|
|
echo "WARNING: php-fpm$phpVersion configuration test failed"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# create & enable apache config
|
|
|
|
/usr/local/bin/vhost-enable.sh VHostHTTP $virtualhost
|