Search code examples
apache.htaccessamazon-web-servicesamazon-elastic-beanstalk

Configure apache on elastic beanstalk


I'm developing with django on elastic beanstalk and I want to make two changes to apache configuration:

1. redirect www.domain.com to domain.com

2. redirect http://domain.com to https://domain.com

I don't have experience with apache configuration, googling it gave me the idea that I should put RewriteRules in .htaccess file.

example: How to force https on amazon elastic beanstalk without failing the health check

I couldn't find instructions on how to do it with elastic beanstalk configuration (.ebextensions), I tried to simply put a .htaccess file in my root filder and deploy but it didn't work.

Does anyone know how it's possible to add the RewriteRules in elastic beanstalk?


Solution

  • this is an easy solution

    1. ssh into your EC2 instance
    2. copy the contents of /etc/httpd/conf.d/wsgi.conf into a local file called wsgi.conf which will be placed in the base folder of your application
    3. Edit the local version of wsgi.conf and add the following redirect rules within the < VirtualHost> < /VirtualHost> tags

      RewriteEngine On
      RewriteCond %{HTTP:X-Forwarded-Proto} !https
      RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
      
    4. Change the “/status” to whatever page you are using as a health check page.

    5. Save the file
    6. Edit your < app>.conf file inside your .ebextensions directory to add a container command to copy this version of wsgi.conf over Amazon’s version

      container_commands:
      01_syncdb:
        command: "django-admin.py syncdb --noinput" leader_only: true
      02_collectstatic:
        command: "django-admin.py collectstatic --noinput"
      03_wsgireplace:
        command: 'cp wsgi.conf /etc/httpd/conf.d/wsgi.conf'
      ...
      
    7. Deploy the code.

    8. The deployed version of wsgi.conf at /etc/httpd/conf.d/wsgi.conf will now include the necessary redirect rules.

    It should work and the file will be properly updated for each deployment. The only thing to watch for is if Amazon changes their base wsgi.conf file contents in the future, then your copy may no longer work.

    Source : rickchristianson