Skip to content

Instantly share code, notes, and snippets.

@stefanpejcic
Created May 8, 2024 16:36
Show Gist options
  • Save stefanpejcic/37805c6781dc3beb1730fec82ee5ae34 to your computer and use it in GitHub Desktop.
Save stefanpejcic/37805c6781dc3beb1730fec82ee5ae34 to your computer and use it in GitHub Desktop.
gunicorn service.config.py
# Gunicorn configuration file
# https://docs.gunicorn.org/en/stable/configure.html#configuration-file
# https://docs.gunicorn.org/en/stable/settings.html
import multiprocessing
from gunicorn.config import Config
import configparser
import os
CONFIG_FILE_PATH = '/usr/local/panel/conf/panel.config'
def read_config():
config = configparser.ConfigParser()
if os.path.exists(CONFIG_FILE_PATH):
config.read(CONFIG_FILE_PATH)
return config
def get_ssl_status():
config = read_config()
# 'yes' is True, 'no' is False
return config.getboolean('DEFAULT', 'ssl', fallback=False)
# Check SSL status and import ssl and set SSL settings if 'yes'
if get_ssl_status():
# SSL Settings
import ssl
import socket
hostname = socket.gethostname()
certfile = f'/etc/letsencrypt/live/{hostname}/fullchain.pem'
keyfile = f'/etc/letsencrypt/live/{hostname}/privkey.pem'
ssl_version = 'TLS'
ca_certs = f'/etc/letsencrypt/live/{hostname}/fullchain.pem'
cert_reqs = ssl.CERT_NONE
ciphers = 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'
bind = ["0.0.0.0:2087"]
backlog = 2048
workers = 2
#workers = multiprocessing.cpu_count() + 1
# Use gevent worker class
worker_class = 'gevent'
worker_connections = 1000
timeout = 30
graceful_timeout = 30
keepalive = 2
max_requests = 1000
max_requests_jitter = 50
pidfile = 'adminpanel'
# BUG https://github.com/benoitc/gunicorn/issues/2382
#errorlog = "-" # Log to stdout
#accesslog = "-"
errorlog = "/var/log/openpanel/admin/error.log"
accesslog = "/var/log/openpanel/admin/access.log"
def post_fork(server, worker):
server.log.info("Worker spawned (pid: %s)", worker.pid)
def pre_fork(server, worker):
pass
def pre_exec(server):
server.log.info("Forked child, re-executing.")
def when_ready(server):
server.log.info("Server is ready. Spawning workers")
def worker_int(worker):
worker.log.info("worker received INT or QUIT signal")
# Allow specific IP addresses
#config.allow_ip = ['192.168.1.100']
forwarded_allow_ips = '*' # workaround for cloudflare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment