我在aws弹性beanstalk中遇到自定义配置文件的问题。
我的应用程序是python flask app。
我将01wsgi.config文件放入.ebextensions。
并将其压缩然后上传到弹性beanstalk。
源部署得很好,但配置没有执行。
如何使其正常工作?
目录结构:
source_root
- .ebextensions
-- 01wsgi.config
- application
- application.wsgi
01wsgi.config内容:
files:
"/etc/httpd/conf.d/wsgi.conf":
mode: "000644"
owner: root
group: root
content: |
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /opt/python/run/baselinenv
WSGISocketPrefix run/wsgi
WSGIRestrictEmbedded On
<VirtualHost *:80>
#############
# TYPES FIX #
#############
AddType text/css .css
AddType text/javascript .js
####################
# GZIP COMPRESSION #
####################
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/javascript application/x-javascript application/x-httpd-php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip
Header append Vary User-Agent env=!dont-vary
Alias /static/(.*)? /opt/python/current/app/application/frontend/static-build/
<Directory /opt/python/current/app/application/frontend/static-build/>
Order allow,deny
Allow from all
Header append Cache-Control "max-age=2592000, must-revalidate"
</Directory>
WSGIScriptAlias / /opt/python/current/app/application.py
<Directory /opt/python/current/app/>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess wsgi processes=1 threads=15 display-name=%{GROUP} \
python-path=/opt/python/current/app:/opt/python/run/venv/lib/python2.7/site-packages user=wsgi group=wsgi \
home=/opt/python/current/app
WSGIProcessGroup wsgi
WSGIScriptReloading On
</VirtualHost>
我按照以下文件:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html
将您的wsgi.conf文件放入.ebextensions目录。
制作一个将wsgi.conf复制到ondeck的配置文件。
01wsgi.config内容:
container_commands:
replace_wsgi_config:
command: "cp .ebextensions/wsgi.conf /opt/python/ondeck/wsgi.conf"
答案 0 :(得分:2)
我想添加一些关于其他问题的信息:如果您进行不执行完整部署的更改(例如更改环境变量),Beanstalk将覆盖您的apache hosts文件。在大多数情况下,您的网络服务器将停止提供您的应用,除非您实际上没有自定义wsgi.conf
。
现在,要明确,您是正确的,因为您需要将您的WSGI配置放在.ebextentions
目录中。然后使用container command将配置文件移动到EB查看的位置。您可以使用以下命令安全地执行此操作:
# Replace the default wsgi with ours
cp .ebextensions/wsgi.conf ../wsgi.conf
为防止在未执行部署的更新期间覆盖自定义wsgi.conf
,您需要修补重新创建wsgi.conf
的本机EB挂钩。我还没有找到任何关于此的文档,但是custom hooks已经记录在案,而本地文档也是以同样的方式工作。
这是我一直在使用的猴子补丁:
# Elastic Beanstalk always forces generation of apache hosts,
# stop it by returning true in the function that does it
sed ' /return True # DO NOT REGENERATE APACHE HOSTS/d' /opt/elasticbeanstalk/hooks/config.py \
| sed -e 's/def generate_apache_config(params, filename):/def generate_apache_config(params, filename):\n return True # DO NOT REGENERATE APACHE HOSTS/1' \
-e 's/if not os.path.exists(WSGI_STAGING_CONFIG):/if not os.path.exists(WSGI_STAGING_CONFIG):\n return True # DO NOT REGENERATE APACHE HOSTS/1' \
> /tmp/config.py && mv -f /tmp/config.py /opt/elasticbeanstalk/hooks/config.py
SSH进入EB实例并查看/opt/elasticbeanstalk/hooks/config.py
,您将看到EB在部署或更新环境期间所做的工作。
快乐的AWSing!