11

I've got a problem with Custom Configuration File in aws elastic beanstalk.

My application is python flask app.

I put 01wsgi.config file into .ebextensions.

and zipped it then upload to elastic beanstalk.

The source deployed well, but the configuration didn't executed.

How can I make it works properly?

directory structure:

source_root
  - .ebextensions
     -- 01wsgi.config
  - application
  - application.wsgi

01wsgi.config content:

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>

I followed document below:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html

SOLVED

Put your wsgi.conf file into .ebextensions directory.

And make a config file that copy wsgi.conf to ondeck.

01wsgi.config content:

container_commands:
  replace_wsgi_config:
    command: "cp .ebextensions/wsgi.conf /opt/python/ondeck/wsgi.conf"
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
dorajistyle
  • 468
  • 1
  • 5
  • 14

1 Answers1

2

I wanted to add some info about another gotcha: Beanstalk will overwrite your apache hosts file if you make changes that do not execute a full deployment (e.g. change environment variables). In most cases, your web server will stop serving up your app, unless you are not actually customizing wsgi.conf.

Now, to be clear, you are correct in that you need to put your WSGI config in the .ebextentions directory. Then you use a container command to move the configuration file to the location that EB looks at. You can do this safely with this command:

# Replace the default wsgi with ours
cp .ebextensions/wsgi.conf ../wsgi.conf

To prevent your custom wsgi.conf from being overwritten during an update that does not execute a deployment, you will need to monkey patch the native EB hook that recreates wsgi.conf. I haven't found any docs around this, but custom hooks are documented and the native ones work in the same fashion.

Here is the monkey patch that I have been using:

# 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 into an EB instance and take a look at /opt/elasticbeanstalk/hooks/config.py and you'll see what EB does during deployments or updates to the environment.

Happy AWSing!

Ryan Fisher
  • 1,485
  • 1
  • 19
  • 32
  • Where do you place the "Monkey Patch"? in your `wsgi.conf`? Is your custom wsgi.conf file placed in the same directory as `requirements.txt`? – WayBehind Dec 26 '18 at 20:42
  • @WayBehind The patch is to delete lines in `/opt/elasticbeanstalk/hooks/config.py` that re-deploy the default wsgi.conf. There is probably a better way to do this, it's been a long time and I'm not currently using Django on EB. – Ryan Fisher Jan 09 '19 at 03:07