如何使用mod_wsgi运行django并将PYTHONHASHSEED环境变量设置为随机? 在像这样的django设置中设置它是一个好方法吗?
os.environ['PYTHONHASHSEED'] = 'random'
答案 0 :(得分:2)
mod_wsgi 4.1.0为此引入了一个选项(http://modwsgi.readthedocs.org/en/latest/release-notes/version-4.1.0.html);你会添加到你的Apache配置:
WSGIPythonHashSeed random
如果无法运行该版本,则必须在Apache进程的启动环境中设置该变量,该进程将特定于操作系统。对于Fedora或RHEL 7,您可以创建/etc/systemd/system/httpd.service:
.include /lib/systemd/system/httpd.service
[Service]
Environment=PYTHONHASHSEED=random
然后systemctl daemon-reload; systemctl restart httpd.service
。对于预先系统化的Red Hats,您可以编辑/ etc / sysconfig / httpd。对于Debian,它是/ etc / apache2 / envvars。
这是一个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]