如何在 AWS ElasticBeanstalk 中修改 apache VirtualHost

How to modify apache VirtualHost in AWS ElasticBeanstalk

我想在 ElasticBeanstalk 中我的实例的 apache 指令中插入一些行。 (我的实例是 64 位 Amazon Linux 2016.03 v2.1.0 运行ning Python 2.7 for 运行ning Django with mod_wsgi)。

我创建了一个文件 "configuration_httpd.py" :

# -*- coding: utf-8 -*-
import os
import fileinput

current_dir = os.path.dirname(os.path.abspath(__file__))

custom_conf = open(current_dir+'/httpd.conf','r').read()

try:
    for line in fileinput.input('/etc/httpd/conf.d/wsgi.conf', inplace=1):
        if line.startswith('</VirtualHost>'):
            print custom_conf
            print line,
        else:
            print line,
except OSError:
    print "Error '/etc/httpd/conf.d/wsgi.conf' unknown"

它在使用 .ebextensions/django.config 文件的部署中执行:

packages:
  yum:
    libjpeg-turbo-devel: []
    libpng-devel: []

container_commands:
  01_setup_apache:
    command: "python configure_httpd.py"
  02_migrate:
    command: "python manage.py migrate"
    leader_only: true
  03_collectstatic:
    command: "python manage.py collectstatic --noinput"

option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: "djangoproject.settings"
    PYTHONPATH: "/opt/python/current/app/djangoproject:$PYTHONPATH"
  aws:elasticbeanstalk:container:python:
    WSGIPath: "djangoproject/wsgi.py"

此文件已执行,但文件“/etc/httpd/conf.d/wsgi.conf”完好无损。

为什么?

当我使用 ssh 登录我的实例时,我可以使用 sudo 运行 脚本 "configure_httpd.py" 并且它可以工作。

为什么这个脚本在部署时不起作用?

请帮忙。谢谢

在 google 上几个小时后,我发现指令 <Directory> 允许使用 <VirtualHost> 指令之外的重写规则。例子:

<Directory /opt/python/current/app/>
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteRule ^(.*)$  [R=200,L]
</Directory>

把这个放在一个文件里,然后复制到/etc/httpd/conf.d in container_commands.