简体   繁体   English

Django用mod_wsgi如何设置PYTHONHASHSEED env变量?

[英]Django with mod_wsgi how to set PYTHONHASHSEED env variable?

How to run django with mod_wsgi with PYTHONHASHSEED environment variable set to random? 如何使用mod_wsgi运行django并将PYTHONHASHSEED环境变量设置为随机? Is it a good approach to set it in django settings like this? 在像这样的django设置中设置它是一个好方法吗?

os.environ['PYTHONHASHSEED'] = 'random'

mod_wsgi 4.1.0 introduces an option for this ( http://modwsgi.readthedocs.org/en/latest/release-notes/version-4.1.0.html ); mod_wsgi 4.1.0为此引入了一个选项( http://modwsgi.readthedocs.org/en/latest/release-notes/version-4.1.0.html ); you would add to your Apache config: 你会添加到你的Apache配置:

WSGIPythonHashSeed random

If you can't run that version, you have to set the variable in the startup environment of the Apache process, which will be OS-specific. 如果无法运行该版本,则必须在Apache进程的启动环境中设置该变量,该进程将特定于操作系统。 For Fedora or RHEL 7, you can create /etc/systemd/system/httpd.service: 对于Fedora或RHEL 7,您可以创建/etc/systemd/system/httpd.service:

.include /lib/systemd/system/httpd.service
[Service]
Environment=PYTHONHASHSEED=random

then systemctl daemon-reload; systemctl restart httpd.service 然后是systemctl daemon-reload; systemctl restart httpd.service systemctl daemon-reload; systemctl restart httpd.service . systemctl daemon-reload; systemctl restart httpd.service For pre-systemd Red Hats, you can edit /etc/sysconfig/httpd. 对于预先系统化的Red Hats,您可以编辑/ etc / sysconfig / httpd。 For Debian, it's /etc/apache2/envvars. 对于Debian,它是/ etc / apache2 / envvars。

Here's a WSGI file to test if it's working (based on the example in the mod_wsgi docs): 这是一个WSGI文件来测试它是否正常工作(基于mod_wsgi文档中的示例):

import sys

def application(environ, start_response):
    status = '200 OK'

    try:
        hr = sys.flags.hash_randomization
        if hr == 0:
            output = 'Hash randomization disabled'
        elif hr == 1:
            output = 'Hash randomization enabled'
        else:
            output = 'Unknown hash randomization: ' + str(hr)
    except AttributeError:
        output = 'Hash randomization not supported'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM