Template Setup - Gunicorn for HestiaCP

Template Setup - Gunicorn for HestiaCP

Location

/usr/local/hestia/data/templates/web/nginx/
That’s the Nginx proxy template directory. Any Gunicorn proxy template (from your existing setup or a custom one) will be in this folder.

Typical Gunicorn template files

A full template set usually has the same base name and these extensions:
  • gunicorn.tpl – HTTP (port 80)
  • gunicorn.stpl – HTTPS (port 443)
  • gunicorn.sh – optional script
So on the server that already has the Gunicorn template, the files you copy are:
  • /usr/local/hestia/data/templates/web/nginx/gunicorn.tpl
  • /usr/local/hestia/data/templates/web/nginx/gunicorn.stpl
  • (and gunicorn.sh if it exists)
The base name might differ (e.g. proxy_gunicorn or gunicorn_proxy). To see the exact names on the existing server:
ls -la /usr/local/hestia/data/templates/web/nginx/

If template not exist:

sudo nano /usr/local/hestia/data/templates/web/nginx/gunicorn.tpl
#=========================================================================#
# Default Web Domain Template                                             #
# DO NOT MODIFY THIS FILE! CHANGES WILL BE LOST WHEN REBUILDING DOMAINS   #
# https://hestiacp.com/docs/server-administration/web-templates.html      #
#=========================================================================#

server {
    listen      %ip%:%proxy_port%;
    server_name %domain_idn% %alias_idn%;

    error_log /var/log/apache2/domains/%domain%.error.log error;

    location / {
        # Replace the default proxy_pass with Gunicorn socket:
        proxy_pass http://unix:/home/%user%/web/%domain%/public_html/gunicorn.sock;
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /static/ {
        alias /home/%user%/web/%domain%/public_html/static/;
        expires max;
    }

    location /media/ {
        alias /home/%user%/web/%domain%/public_html/media/;
        expires max;
    }

    # etc...

    include %home%/%user%/conf/web/%domain%/nginx.conf_*;
}
sudo nano /usr/local/hestia/data/templates/web/nginx/gunicorn.stpl
#=========================================================================#
# Default Web Domain Template                                             #
# DO NOT MODIFY THIS FILE! CHANGES WILL BE LOST WHEN REBUILDING DOMAINS   #
# https://hestiacp.com/docs/server-administration/web-templates.html      #
#=========================================================================#

server {
    listen      %ip%:%proxy_ssl_port% ssl;
    server_name %domain_idn% %alias_idn%;

    ssl_certificate     /home/%user%/conf/web/%domain%/ssl/%domain%.pem;
    ssl_certificate_key /home/%user%/conf/web/%domain%/ssl/%domain%.key;

    location / {
        proxy_pass http://unix:/home/%user%/web/%domain%/public_html/gunicorn.sock;
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /static/ {
        alias /home/%user%/web/%domain%/public_html/static/;
        expires max;
    }

    location /media/ {
        alias /home/%user%/web/%domain%/public_html/media/;
        expires max;
    }

    include %home%/%user%/conf/web/%domain%/nginx.ssl.conf_*;
}
sudo chmod 644 /usr/local/hestia/data/templates/web/nginx/gunicorn.tpl
sudo chmod 644 /usr/local/hestia/data/templates/web/nginx/gunicorn.stpl

Copying to the new HestiaCP server

  1. Copy the same .tpl.stpl (and optional .sh) files into /usr/local/hestia/data/templates/web/nginx/ on the new server.
  1. In HestiaCP, edit the domain and choose this template as the “Nginx template” (or “Proxy template”) for the domain.
  1. Rebuild the domain so the config is regenerated, e.g.:
  • Rebuild user: v-rebuild-user username
  • sudo /usr/local/hestia/bin/v-rebuild-user admin
  • Rebuild one domain: v-rebuild-web-domain username domain.tld
Note: Don’t edit the built-in templates (defaulthosting, etc.) directly; use a copy (e.g. your Gunicorn set) so updates don’t overwrite your changes.