#!/bin/bash # # vhost-stack # https://git.stack-source.com/msb/vhost-stack # MIT License Copyright (c) 2021 Matthew Saunders Brown # load include file source $(dirname $0)/vhost.sh 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." echo " Or use vhost-deploy.sh instead to automatically generate username." exit } # check for and set virtualhost & username if [ -n "$1" ]; then if [ $1 == "-h" ]; then help else # 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 fi else help 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 echo "/srv/www/$virtualhost /usr/jails/$username/srv/www/$virtualhost none bind 0 0" >> /etc/fstab.jails 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