From 5425e7a16825a5ee016237b2b05cb5916a1305c6 Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Sun, 16 Apr 2023 10:50:42 -0700 Subject: [PATCH] move php-fpm config from vhost-add to user-add --- bin/vhost-add.sh | 42 +++++++++++++----------------------------- bin/vhost-deploy.sh | 15 +++++++-------- bin/vhost-user-add.sh | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/bin/vhost-add.sh b/bin/vhost-add.sh index c507c8c..9c27c0a 100755 --- a/bin/vhost-add.sh +++ b/bin/vhost-add.sh @@ -13,12 +13,11 @@ help() thisfilename=$(basename -- "$0") echo "Add virtualhost to this server." echo "" - echo "usage: $thisfilename -d -u [-x ] [-h]" + echo "usage: $thisfilename -d -u [-h]" echo "" echo " -h Print this help." echo " -d Domain name to add as a VirtualHost. www. subdomain is automatically aliased." echo " -u Username to install VirtualHost for. Username must already exist." - echo " -x PHP-FPM pm.max_children. Optional, defaults to 4, recommended range 2-12 on Shared Server." echo " If need be run vhost-user-add.sh first." echo " Or use vhost-deploy.sh instead to automatically generate username." } @@ -37,11 +36,6 @@ if [[ -z $username ]]; then exit fi -# check for php-fpm process manager max children -if [[ -z $fpmmax ]]; then - fpmmax=4 -fi - if [[ ! -d /home/$username ]]; then echo "home dir for $username does not exist" exit 1 @@ -74,29 +68,19 @@ if [[ -d /usr/jails/$username ]]; then fi fi -# php-fpm pool +# set sendmail_path in php-fpm, but only if not already set 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 = $fpmmax" >> /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@$domain" >> /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" +if [[ -f /etc/php/$phpVersion/fpm/pool.d/$username.conf ]]; then + if ! /bin/grep -q "^php_admin_value\[sendmail_path\]" /etc/php/$phpVersion/fpm/pool.d/$username.conf; then + echo "php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -fwebmaster@$domain" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf + # restart php-fpm if it's active + if systemctl is-active --quiet php$phpVersion-fpm ; then + # check for valid config before restarting + 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 fi diff --git a/bin/vhost-deploy.sh b/bin/vhost-deploy.sh index 01eb7bc..7151a6e 100755 --- a/bin/vhost-deploy.sh +++ b/bin/vhost-deploy.sh @@ -93,11 +93,15 @@ if ! grep -q "^$username:" /etc/passwd; then 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" -w + /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" + /usr/local/bin/vhost-user-add.sh -u $username -p "$password" -x $fpmmax fi # if jail option is set then jail user if [[ -n $jail ]]; then @@ -108,13 +112,8 @@ if ! grep -q "^$username:" /etc/passwd; then fi fi -# check for php-fpm process manager max children -if [[ -z $fpmmax ]]; then - fpmmax=4 -fi - # add virtualhost -/usr/local/bin/vhost-add.sh -d $domain -u $username -x $fpmmax > /dev/null 2>&1 +/usr/local/bin/vhost-add.sh -d $domain -u $username > /dev/null 2>&1 # add mysql database if [[ -n $write ]]; then diff --git a/bin/vhost-user-add.sh b/bin/vhost-user-add.sh index f3dffea..b26dfd4 100755 --- a/bin/vhost-user-add.sh +++ b/bin/vhost-user-add.sh @@ -13,12 +13,13 @@ help() thisfilename=$(basename -- "$0") echo "Add system user to server." echo "" - echo "usage: $thisfilename -u [-p [-i ] [-w] [-h]" + echo "usage: $thisfilename -u [-p [-i ] [-x ] [-w] [-h]" echo "" echo " -h Print this help." echo " -u System username to add to server." echo " -p Password for username. Optional, random password generated if none specified." echo " -i Numberic User ID to assign to user. Optional, next available uid set if none specified." + echo " -x PHP-FPM pm.max_children. Optional, defaults to 4, recommended range 2-12 on Shared Server." echo " -w Write user info to /home/username/.passwd." exit } @@ -87,3 +88,33 @@ if [[ -n $write ]]; then echo "$userpasswdinfo" > /home/$username/.passwd 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 + # check for and set php-fpm process manager max children + if [[ -z $fpmmax ]]; then + fpmmax=$FPM_MAX + fi + echo "pm.max_children = $fpmmax" >> /etc/php/$phpVersion/fpm/pool.d/$username.conf + echo "pm.process_idle_timeout = 3s;" >> /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