AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)
AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Terminating HTTPS on EC2 Instances Running PHP

For PHP container types, you use a configuration file to enable the Apache HTTP Server to use HTTPS.

Add the following snippet to your configuration file, replacing the certificate and private key material as instructed, and save it in your source bundle's .ebextensions directory.

The configuration file performs the following tasks:

  • The packages key uses yum to install mod24_ssl.

  • The files key creates the following files on the instance:


    Configures the Apache server. This file loads when the Apache service starts.


    Creates the certificate file on the instance. Replace certificate file contents with the contents of your certificate.


    YAML relies on consistent indentation. Match the indentation level when replacing content in an example configuration file and make sure that your text editor uses spaces, not tab characters, to indent.

    If you have intermediate certificates, include them in server.crt after your site certificate:

          -----BEGIN CERTIFICATE-----
      certificate file contents
      -----END CERTIFICATE-----
      -----BEGIN CERTIFICATE-----
      first intermediate certificate
      -----END CERTIFICATE-----
      -----BEGIN CERTIFICATE-----
      second intermediate certificate
      -----END CERTIFICATE-----

    Creates the private key file on the instance. Replace private key contents with the contents of the private key used to create the certificate request or self-signed certificate.

Example .ebextensions/https-instance.config

    mod24_ssl : []

    mode: "000644"
    owner: root
    group: root
    content: |
      LoadModule ssl_module modules/
      Listen 443
      <VirtualHost *:443>
        <Proxy *>
          Order deny,allow
          Allow from all

        SSLEngine             on
        SSLCertificateFile    "/etc/pki/tls/certs/server.crt"
        SSLCertificateKeyFile "/etc/pki/tls/certs/server.key"
        SSLProtocol           All -SSLv2 -SSLv3
        SSLHonorCipherOrder   On
        SSLSessionTickets     Off
        Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
        Header always set X-Frame-Options DENY
        Header always set X-Content-Type-Options nosniff
        ProxyPass / http://localhost:80/ retry=0
        ProxyPassReverse / http://localhost:80/
        ProxyPreserveHost on
        RequestHeader set X-Forwarded-Proto "https" early
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN CERTIFICATE-----
      certificate file contents
      -----END CERTIFICATE-----
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN RSA PRIVATE KEY-----
      private key contents # See note below.
      -----END RSA PRIVATE KEY-----


Avoid commiting a configuration file that contains your private key to source control. After you have tested the configuration and confirmed that it works, store your private key in Amazon S3 and modify the configuration to download it during deployment. For instructions, see Storing Private Keys Securely in Amazon S3.

In a single instance environment, you must also modify the instance's security group to allow traffic on port 443. The following configuration file retrieves the security group's ID using an AWS CloudFormation function and adds a rule to it:

Example .ebextensions/https-instance-single.config

    Type: AWS::EC2::SecurityGroupIngress
      GroupId: {"Fn::GetAtt" : ["AWSEBSecurityGroup", "GroupId"]}
      IpProtocol: tcp
      ToPort: 443
      FromPort: 443

For a load balanced environment, you configure the load balancer to either pass secure traffic through untouched, or decrypt and re-encrypt for end-to-end encryption.