Per user extProcessors

Panormitis

Verified User
Joined
Sep 13, 2014
Messages
53
By default, OpenLiteSpeed uses the same LSPHP pool for all users and the pool is multiplied by the number of workers configured. This is ok, but when some users/sites are resource hogs they might use the vast majority of PHP_LSAPI_CHILDREN for themselves and that will make the rest sites to have poor performance.
OpenLiteSpped doesn't have a way to limit children usage per account, only LiteSpeed has that feature, but we can achieve something similar with per user extProcessors, so each user will have it's own guaranteed pool, without messing with the resources of other accounts.
Keep in mind OpenLiteSpeed has a limit in the code, of the maximum extProcessors that can be used (#define MAX_EXT_APP_NUMBER 100 in mainserverconfig.h), so what I'm going to demonstrate here, works if your server has about 100 clients or less, otherwise the only way to increase that limit, is to edit mainserverconfig.h, increase MAX_EXT_APP_NUMBER, build OpenLiteSpeed from source and use it.

Now, without further ado, let's begin!
Let's customize the VHost templates first:
Bash:
mkdir -p /usr/local/directadmin/data/templates/custom
cd /usr/local/directadmin/data/templates/custom
cp ../openlitespeed_vhost.conf . # if you don't already have a customized file.
nano openlitespeed_vhost.conf

# Change:
|?SCRIPTHANDLER=lsphp`PHP1_RELEASE`|
# To:
|?SCRIPTHANDLER=php`PHP1_RELEASE`|

# Change:
    add                     lsapi:|SCRIPTHANDLER| inc
    add                     lsapi:|SCRIPTHANDLER| php
    add                     lsapi:|SCRIPTHANDLER| phtml
    add                     lsapi:|SCRIPTHANDLER| php|PHP1_RELEASE|
# To:
    add                     lsapi:|USER|-|SCRIPTHANDLER| inc
    add                     lsapi:|USER|-|SCRIPTHANDLER| php
    add                     lsapi:|USER|-|SCRIPTHANDLER| phtml
    add                     lsapi:|USER|-|SCRIPTHANDLER| php|PHP1_RELEASE|

# Save changes and close the file.
# The next file is from https://docs.directadmin.com/webservices/openlitespeed/customizing-ols.html#how-to-set-up-webmail-example-com-with-openlitespeed
# If you haven't applied that customization, move on.

nano cust_openlitespeed.CUSTOM.8.pre

# Change:
   add                     lsapi:|SCRIPTHANDLER| inc
   add                     lsapi:|SCRIPTHANDLER| php
   add                     lsapi:|SCRIPTHANDLER| phtml
   add                     lsapi:|SCRIPTHANDLER| php|PHP1_RELEASE|
# To:
   add                     lsapi:lsphpwebapps inc
   add                     lsapi:lsphpwebapps php
   add                     lsapi:lsphpwebapps phtml
   add                     lsapi:lsphpwebapps php|PHP1_RELEASE|

# Save changes and close the file.
Now let's create the script that will generate the extProcessors. I'm using a more relaxed extProcessor for the admin sites and stricter extProcessors for the clients:
This guide assumes the script is saved as /root/scripts/add-lsphp-extProcessors.sh and 700 permissions are set.
Bash:
#!/bin/bash

# Avoid no-match expands to literal glob.
shopt -s nullglob

# Master array for all extProcessors.
declare -a EXT_PROCESSORS=()

# Loop through all openlitespeed.conf files.
for openlitespeed_conf in /usr/local/directadmin/data/users/*/openlitespeed.conf; do
    # Extract USERNAME-phpXX entries.
    entries=$(grep -oE "lsapi:.*-php[0-9]{1,2}" "$openlitespeed_conf" | sed 's/^lsapi://')

    if [[ -n "$entries" ]]; then
        while read -r entry; do
            [[ -n "$entry" ]] && EXT_PROCESSORS+=("$entry")
        done <<< "$entries"
    fi
done

# Remove duplicates, sort, and reassign to array.
readarray -t EXT_PROCESSORS < <(printf "%s\n" "${EXT_PROCESSORS[@]}" | LC_ALL=C sort -u)

# FOR DEBUGGING: Show results.
# echo "extProcessors needed:"
# printf '%s\n' "${EXT_PROCESSORS[@]}"
# exit 1

############################################################################
### Building the file /usr/local/lsws/conf/httpd-extprocessors.conf now! ###
############################################################################

TOTAL_EXT_PROCESSORS=0

for EXT_PROCESSOR in "${EXT_PROCESSORS[@]}"; do
  USERNAME="${EXT_PROCESSOR%%-*}"
  PHP_VERSION="${EXT_PROCESSOR#*-}"

  if [ "${USERNAME}" = "admin" ]; then
    if ! grep -qF "${EXT_PROCESSOR}" /usr/local/lsws/conf/httpd-extprocessors.conf ; then
cat <<EOF >> /usr/local/lsws/conf/httpd-extprocessors.conf

extProcessor ${EXT_PROCESSOR} {
  type                    lsapi
  address                 uds://tmp/lshttpd/${EXT_PROCESSOR}.sock
  autoStart               1
  maxConns                30
  env                     PHP_LSAPI_CHILDREN=30
  env                     PHPRC=/home/${USERNAME}/php.ini   # Custom php.ini file for admin, remove that line if you don't want this.
  env                     LSPHP_ENABLE_USER_INI=on   # You might want this on or off, adjust accordingly.
  env                     LSAPI_MAX_REQUESTS=5000
  env                     LSAPI_SLOW_REQ_MSECS=2000
  env                     LSAPI_DEBUG_LOG=0
  env                     LSAPI_AVOID_FORK=1
  path                    /usr/local/${PHP_VERSION}/bin/lsphp
  initTimeout             60
  memSoftLimit            2047M
  memHardLimit            2047M
  procSoftLimit           400
  procHardLimit           500
  backlog                 100
  instances               1
  priority                0
  retryTimeout            0
  persistConn             1
  respBuffer              1
}

EOF
TOTAL_EXT_PROCESSORS=$((TOTAL_EXT_PROCESSORS + 1))
    fi
  else
    if ! grep -qF "${EXT_PROCESSOR}" /usr/local/lsws/conf/httpd-extprocessors.conf ; then
cat <<EOF >> /usr/local/lsws/conf/httpd-extprocessors.conf

extProcessor ${EXT_PROCESSOR} {
  type                    lsapi
  address                 uds://tmp/lshttpd/${EXT_PROCESSOR}.sock
  autoStart               1
  maxConns                20
  env                     PHP_LSAPI_CHILDREN=20
  env                     LSPHP_ENABLE_USER_INI=off   # You might want this on or off, adjust accordingly.
  env                     LSAPI_MAX_REQUESTS=5000
  env                     LSAPI_SLOW_REQ_MSECS=2000
  env                     LSAPI_DEBUG_LOG=0
  env                     LSAPI_AVOID_FORK=0
  path                    /usr/local/${PHP_VERSION}/bin/lsphp
  initTimeout             30
  memSoftLimit            2047M
  memHardLimit            2047M
  procSoftLimit           400
  procHardLimit           500
  backlog                 100
  instances               1
  priority                0
  retryTimeout            0
  persistConn             1
  respBuffer              1
}

EOF
TOTAL_EXT_PROCESSORS=$((TOTAL_EXT_PROCESSORS + 1))
    fi
  fi
done

if [ ${TOTAL_EXT_PROCESSORS} -gt 0 ]; then
  systemctl restart litespeed
  echo "${TOTAL_EXT_PROCESSORS} extProcessors added successfully! OpenLiteSpeed restarted!"
fi

exit 0
Now we have to create the hook scripts that will trigger the script automatically.
Bash:
mkdir -p /usr/local/directadmin/custombuild/custom/hooks/openlitespeed/post
cd /usr/local/directadmin/custombuild/custom/hooks/openlitespeed/post
echo '#!/bin/bash' > last_things.sh
echo 'exec /root/scripts/add-lsphp-extProcessors.sh' >> last_things.sh
echo 'exit 0' >> last_things.sh
chmod 700 last_things.sh

mkdir -p /usr/local/directadmin/custombuild/custom/hooks/rewrite_confs/post
cp -p last_things.sh /usr/local/directadmin/custombuild/custom/hooks/rewrite_confs/post

# I have also created a user_httpd_write_post.sh hook script, just in case.
# If you already have one, just add: exec /root/scripts/add-lsphp-extProcessors.sh
# Otherwise, let's create one:
mkdir -p /usr/local/directadmin/scripts/custom
cp -p last_things.sh /usr/local/directadmin/scripts/custom/user_httpd_write_post.sh
Now just do: da build openlitespeed
That's it!
 
Last edited:
Back
Top