在 Elastic Beanstalk 配置中设置 Python WSGI Daemon --maximum-requests 值

Set Python WSGIDaemon --maximum-requests value in ElasticBeanstalk Configuration

我正在寻找有关如何在 运行 Django 的 AWS ElasticBeanstalk Python 环境中设置 --maximum-requests 值的说明。请注意,此环境不使用 Linux 2 图像,因此 gunicorn 不是一个选项,也不使用 procfile。

maximum-requests=nnn Defines a limit on the number of requests a daemon process should process before it is shutdown and restarted.

This might be use to periodically force restart the WSGI application processes when you have issues related to Python object reference count cycles, or incorrect use of in memory caching, which causes constant memory growth.

If this option is not defined, or is defined to be 0, then the daemon process will be persistent and will continue to service requests until Apache itself is restarted or shutdown.

Avoid setting this to a low number of requests on a site which handles a lot of traffic. This is because the constant restarting and reloading of your WSGI application may cause unecessary load on your system and affect performance. Only use this option if you have no other choice due to a memory usage issue. Stop using it as soon as any memory issue has been resolved.

You can use the graceful-timeout option in conjunction with this option to reduce the chances that an active request will be interrupted when a restart occurs due to the use of this option.

您必须替换使用 WSGIDaemonProcess 指令的 Apache 服务器配置。

SSH 到 EC2 实例 运行 您的弹性 beantalk 应用程序并更改到配置目录 /etc/httpd/conf.d/

在那里寻找包含 WSGIDaemonProcess.

的文件
grep -rnwl . -e 'WSGIDaemonProcess'

替换您的 elasticbeanstalk 应用程序配置中匹配文件的内容。

生成配置的位置(在应用程序部署的暂存阶段)可以使用 shell 命令方便地获取:

/opt/elasticbeanstalk/bin/get-config container -k wsgi_staging_config

.ebextensions/wsgi.config

files:
  /opt/elasticbeanstalk/hooks/appdeploy/pre/05_wsgi.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      # Set max-requests option in generated wsgi.conf
      sed -i -e '/WSGIDaemonProcess wsgi/ a\
      \ \ maximum-requests=1000 \
      ' $(/opt/elasticbeanstalk/bin/get-config container -k wsgi_staging_config)

为了实现这一点,我必须在包含以下内容的 .ebextensions 文件夹中创建一个配置。

您需要从您的服务器复制 wsgi.conf 文件,以确保您首先拥有正确的 EB 设置。

files:
  "/opt/elasticbeanstalk/local/override_wsgi_conf.py":
    mode: "000755"
    owner: root
    group: root
    content: |
        #!/usr/bin/env python
        import os
        import sys
        sys.path.append(os.path.dirname(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
        import config
    
        MY_APACHE_TEMPLATE = r'''
        # Customized wsgi.conf.  If you're seeing this, good!
 
        LoadModule wsgi_module modules/mod_wsgi.so
        WSGIPythonHome /opt/python/run/baselinenv
        WSGISocketPrefix run/wsgi
        WSGIRestrictEmbedded On

        <VirtualHost *:80>

        Alias /static/ /opt/python/current/app/static/
        <Directory /opt/python/current/app/static/>
        Order allow,deny
        Allow from all
        </Directory>


        WSGIScriptAlias / /opt/python/current/app/key_collector_backend/wsgi.py


        <Directory /opt/python/current/app/>
        Require all granted
        </Directory>

        WSGIDaemonProcess wsgi processes=3 threads=20 maximum-requests=10000 display-name=%{GROUP} \
        python-home=/opt/python/run/venv/ \
        python-path=/opt/python/current/app user=wsgi group=wsgi \
        home=/opt/python/current/app
        WSGIProcessGroup wsgi
        </VirtualHost>

        LogFormat "%h (%{X-Forwarded-For}i) %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

        WSGIPassAuthorization On
        WSGIApplicationGroup %{GLOBAL}

        '''


        def main():
            try:
                WSGI_STAGING_CONFIG = config.get_container_config('wsgi_staging_config')
                print 'Overriding WSGI configuration in %s' % WSGI_STAGING_CONFIG
                open(WSGI_STAGING_CONFIG, 'w').write(MY_APACHE_TEMPLATE)
            except Exception, e:
                config.emit_error_event(config.USER_ERROR_MESSAGES['badappconfig'])
                config.diagnostic("Error generating config during configdeploy/pre: %s"
                                    % str(e))
                sys.exit(1)
    
    
        if __name__ == '__main__':
            config.configure_stdout_logger()
            main()
 
commands:
 
  5_app_deploy_dir:
    command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/pre"
  5_config_deploy_dir:
    command: "mkdir -p /opt/elasticbeanstalk/hooks/configdeploy/pre"
 
  10_app_deploy_file:
    command: "cp -p /opt/elasticbeanstalk/local/override_wsgi_conf.py /opt/elasticbeanstalk/hooks/appdeploy/pre/90_override_wsgi_conf.py"
 
  20_config_deploy_file:
    command: "cp -p /opt/elasticbeanstalk/local/override_wsgi_conf.py /opt/elasticbeanstalk/hooks/configdeploy/pre/90_override_wsgi_conf.py"

有关完整详细信息,请参阅此线程。 https://forums.aws.amazon.com/thread.jspa?threadID=163369