为了账号安全,请及时绑定邮箱和手机立即绑定

在 ElasticBeanstalk 配置中设置 Python WSGIDaemon

在 ElasticBeanstalk 配置中设置 Python WSGIDaemon

精慕HU 2023-05-16 15:49:01
我正在寻找有关如何--maximum-requests在运行 Django 的 AWS ElasticBeanstalk Python 环境中设置值的说明。请注意,此环境不使用 Linux 2 映像,因此 gunicorn 不是一个选项,也不使用 procfile。maximum-requests=nnn 定义守护进程在关闭和重新启动之前应处理的请求数限制。当您遇到与 Python 对象引用计数周期相关的问题或内存缓存使用不当导致内存不断增长时,这可能会用于定期强制重启 WSGI 应用程序进程。如果此选项未定义,或定义为 0,则守护进程将持续存在并继续为请求提供服务,直到 Apache 本身重新启动或关闭。避免在处理大量流量的网站上将此设置为少量请求。这是因为 WSGI 应用程序的不断重启和重新加载可能会对您的系统造成不必要的负载并影响性能。仅当由于内存使用问题而别无选择时才使用此选项。一旦任何内存问题得到解决,请立即停止使用它。您可以将 graceful-timeout 选项与此选项结合使用,以减少由于使用此选项而导致重新启动时活动请求被中断的可能性。
查看完整描述

2 回答

?
杨__羊羊

TA贡献1943条经验 获得超7个赞

为了实现这一点,我必须在包含以下内容的 .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"



查看完整回答
反对 回复 2023-05-16
?
慕运维8079593

TA贡献1876条经验 获得超5个赞

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


通过 SSH 连接到运行您的弹性 beanstalk 应用程序的 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)


查看完整回答
反对 回复 2023-05-16
  • 2 回答
  • 0 关注
  • 143 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信