Overwriting mail nginx port with custom template

I have some Hestiacp servers with Varnish installed, and they rocks.

But there is a problem with nginx.conf in /home/*/conf/mail/*/nginx.conf files, because they are not obeying any custom template, that I know, so every time HestiaCP has an automatic update, the “listen” line is rewritten and uses port 80 instead of the nginx custom port that I use, which is 8082.

So, it causes that Nginx reloads and restarts from the HestiaCP panel do not run well and fail in silent mode, showing this kind of error in the Nginx log:

2026/07/01 20:45:50 [emerg] 3330392#3330392: bind() to :80 failed (98: Address already in use)

The web server still runs for the current domains, but then, when an SSL certificate is going to expire and a new SSL certificate is issued automatically, it does not work as expected, despite it being shown correctly in the HestiaCP panel, and the domain is still using the old SSL certificate because the Nginx reload/restart is not working correctly.

Is there a workaround to avoid this? Is there any way to set custom Nginx templates for mail? Currently I am fixing it by hand, but then, in the next HestiaCP update, the problem will appear again.

No, Hestia doesn’t have that feature.

You can create a post install script to make the change automatically after every Hestia upgrade.

mkdir -p /etc/hestiacp/hooks/
touch /etc/hestiacp/hooks/post_install.sh
chmod +x /etc/hestiacp/hooks/post_install.sh

And edit /etc/hestiacp/hooks/post_install.sh to add the script:

Note: below script is just an example, review and modify it to suit your needs.

#!/usr/bin/env bash
set -euo pipefail

BIN="/usr/local/hestia/bin"
TPL_DIR="/usr/local/hestia/data/templates/mail/nginx"
default_tpl="$TPL_DIR/web_system.tpl"
default_stpl="$TPL_DIR/web_system.stpl"
custom_tpl="$TPL_DIR/custom_web_system.tpl"
custom_stpl="$TPL_DIR/custom_web_system.stpl"

for f in "$custom_tpl" "$custom_stpl"; do
    if [[ ! -f "$f" ]]; then
        echo "[ ! ] Missing $f, aborting" >&2
        exit 1
    fi
done

cp -f "$default_tpl" "${default_tpl}.bak"
cp -f "$default_stpl" "${default_stpl}.bak"

cp -f "$custom_tpl" "$default_tpl"
cp -f "$custom_stpl" "$default_stpl"

echo "[ + ] Rebuilding mail domains"
export restart="no"
"$BIN/v-list-users" list | while read -r user; do
    echo "      - $user..."
    "$BIN/v-rebuild-mail-domains" "$user" &>/dev/null
done

if nginx -t &>/dev/null; then
    systemctl reload nginx
    echo "[ + ] nginx reloaded successfully"
else
    echo "[ ! ] Invalid nginx configuration, reload skipped" >&2
    exit 1
fi

Many thanks @sahsanu, it looks like a great solution. I have applied the script on the hook also for “disabled” and “snappymail” mail templates, because they are also using the %web_port% var, to avoid the same problem when disabling it for any reason.

I have seen that the main problem comes from this file:

/usr/local/hestia/install/hst-install-debian.sh

that has hardcoded the WEB_PORT var and omits any other WEB_PORT var stored in the server during an HestiaCP install/update for the mail nginx files. So the hook seems the only permanent solution in this scenario.