在ElasticBeanstalk配置中设置Python WSGIDaemon --maximum-requests值

问题描述

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

maximum-requests = nnn定义请求数量的限制 守护进程应在关闭并重新启动之前进行处理。

这可能用于定期强制重新启动Wsgi应用程序 与Python对象参考相关的问题时的处理 计算周期,或不正确使用内存缓存,这会导致 持续的内存增长。

如果未定义此选项或将其定义为0,则守护程序 流程将保持不变,并将继续为请求提供服务,直到 Apache本身将重新启动或关闭

请避免在处理网站的网站上将其设置为少量请求 大量的流量。这是因为不断重启 重新加载Wsgi应用程序可能会导致不必要的加载 系统并影响性能。仅当您没有时才使用此选项 由于内存使用问题,其他选择。尽快停止使用 内存问题已解决

您可以结合使用graceful-timeout选项 选项,以减少活动请求的机会 由于使用此选项而在重新启动时中断。

解决方法

为此,我必须在.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

,

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

SSH到运行您的Elastic beantalk应用程序的EC2实例,并切换到配置目录/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)