Showing posts with label EB Environment Setup. Show all posts
Showing posts with label EB Environment Setup. Show all posts

AWS Elastic Beanstalk Environment Setup with PHP8.0, NGNIX & Amazon Linux 2

  

  1. Download a working version

  2. Create Environment with PHP8.0 with Amazon Linux 2.
    Setup Environment & Run application using Elastic Beanstalk.

    1. Download a working application version from Elastic beanstalk from the existing environment.

      1. Use platform settings if needed and add +x permission to all the .platform files as follows:

        chmod +x .platform/hooks/postdeploy/01_managePermissions.sh ... ...

         

      2. Add hook file for folder permissions.
        .platform/hooks/postdeploy/01_managePermissions.sh

        #!/bin/bash # Give permissions sudo chmod -Rf 777 /var/log/nginx sudo chmod -Rf 777 /var/log/php-fpm

         

      3. Add hook file for starting the service on deployment.
        .platform/hooks/postdeploy/02_environmentConfig.sh

        #!/bin/bash # chkconfig has been replaced by systemctl in Linux 2 instance. sudo systemctl enable nginx systemctl is-enabled nginx sudo service nginx start sudo service nginx status

         

      4. Add above 2 files inside .platform/confighooks to execute it on every configuration change. To avoid duplicate code refer to the above file as the source.
        .platform/confighooks/postdeploy/01_managePermissions.sh

        #!/bin/bash source "/var/app/current/.platform/hooks/postdeploy/01_managePermissions.sh"


        .platform/confighooks/postdeploy/02_environmentConfig.sh

        #!/bin/bash source "/var/app/current/.platform/hooks/postdeploy/02_environmentConfig.sh"

         

      5. Override the php.conf files as per the application requirement as follows :

        .platform/nginx/conf.d/elasticbeanstalk/php.conf

        # Add root settings to the directory root /var/www/html/dist; index index.html index.htm index.php; # Comment this for local applications # Redirect to url with https if ($http_x_forwarded_proto != 'https') { return 301 https://$host$request_uri; } location / { try_files $uri $uri/ /index.html?$query_string; }

         

    2. Create a new Environment.

      1. Go to Elastic Beanstalk.

      2. Create a new Environment. https://us-west-2.console.aws.amazon.com/elasticbeanstalk/home?region=us-west-2#/environments

      3. Select environment tier → Web server environment.

      4. Enter

        1. Application Name,

        2. Environment Name

        3. Domain name [Optional]

        4. Platform → Managed platform

          1. Platform → PHP

          2. Platform branch → PHP 8.0 running on 64bit Amazon Linux 2

          3. Platform version → 3.3.7 (Recommended)

        5. Application code → Upload your code

          1. Source code origin → Local file → Choose file → Upload a working application version downloaded from Elastic beanstalk from the existing environment.

        6. Click on → Configure more options

    3. Set up Environment configuration page

      1. Modify software :

        1. Proxy server → Nginx

        2. Document root →

        3. Environment properties → [Provide the key-value pair for setting Environment variables.]
          ENVIRONMENT: staging/production

      2. Modify instances :

        1. Root volume type → Container default

        2. Size →

        3. EC2 security groups → [select ssh security group if needed]

      3. Modify capacity :

        1. Auto scaling group → Environment type → Load Balancer

        2. Scaling triggers →

      4. Load Balancer : [Do later for HTTPS to avoid grey status]

        1. Add this once the Environment is ready to avoid any failure.
          Listeners → add 443 HTTPS
          [Use Valide certificate from ACM
          Check the HTTPS access using the test domains defined on Route53.]

      5. Rolling updates and deployments :

        1. Deployment policy → Rolling with additional batch

        2. Rolling update type → Rolling based on Health. [If multiple instances available]

      6. Security :

        1. Virtual machine permissions → Choose EC2 key pair

      7. Monitoring :

        1. Health reporting → Enhanced

      8. Click on → Create Environment

    4. Use the Domain name to verify the functionality.

  3. If needed restart/rebuild the EB environment.

  4. Modify Application Load Balancer

    Add Listener for Port - 443 - Https with valid Certificate details.

     

  5. Modify Application Load Balancer → Processes → Edit “default” process

    Update HTTP code → 301 [For web_client application)

    To support the below redirection

    # Redirect to url with https if ($http_x_forwarded_proto != 'https') { return 301 https://$host$request_uri; }
  6. Update Route53 for the specific hosted zone

  7. Deployment to the new Environment using Pipeline.

    1. Update Pipeline with correct EB Environment name & EB Application name for deployment.

    2. Run the pipeline to deploy updated code to the new environment.

  8. If needed add ssh-keys for devs.

  9. Setup Cloudwatch

  10. Create AMI of the instance. Use the AMI ID on the EB configuration & update.

Note :

Restart apache after reboot instances with Amazon Linux 2 AMI

In Amazon Linux 2 AMI you need to follow these steps to restart apache after reboot:

  • In AMI2 they are using systemctl for managing services check if it is installed on your machine.

  • Use this command to check if nginx is listed

    systemctl list-units --type=service
  • To enable nginx start on bootup

    sudo systemctl enable nginx
  • To check if nginx enabled to start on bootup Linux system

    systemctl is-enabled nginx

After this nginx will be started if we reboot the Linux system.