AWS Elastic Beanstalk Environment Setup with PHP 8.1 & Amazon Linux 2

  

Setup EB Environment platform settings on the codebase

  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_initiateMnt.sh chmod +x .platform/hooks/postdeploy/02_managePermissions.sh ... ...

       

    2. Add a hook file for Creating a symlink to the EFS mounted drive
      .platform/hooks/postdeploy/01_initiateMnt.sh

      #!/bin/bash sudo yum install -y amazon-efs-utils #Accessing environment properties of EBS for Defined "ENVIRONMENT" source /opt/elasticbeanstalk/deployment/env if [ $ENVIRONMENT = "Staging" ]; then # Staging EFS_MNT_PATH="/mnt/staging" elif [ $ENVIRONMENT = "Production" ]; then # Production EFS_MNT_PATH="/mnt/path" fi # Verify the details echo "ENVIRONMENT : $ENVIRONMENT" echo "EFS_MNT_PATH : $EFS_MNT_PATH" #Create directory only if it alive.txt does not exists and mount the S3 drive if [ ! -f "$EFS_MNT_PATH/alive.txt" ]; then sudo /bin/mount -t efs -o tls fs-46e9a4ed:/ /mnt fi #Creating symlink to the EFS mounted drive ln -s $EFS_MNT_PATH /var/www/html/public/uploads

       

    3. Add a hook file for folder permissions.
      .platform/hooks/postdeploy/02_managePermissions.sh

      #!/bin/bash # Create Directories and give permissions sudo mkdir /var/www/html/storage sudo chmod -Rf 777 /var/www/html/storage sudo chmod -Rf 777 /var/log/httpd sudo chmod -Rf 777 /var/log/php-fpm

       

    4. Add a hook file for starting the service on deployment.
      .platform/hooks/postdeploy/03_environmentConfig.sh

      #!/bin/bash # chkconfig has been replaced by systemctl in Linux 2 instance. sudo systemctl enable httpd systemctl is-enabled httpd sudo service httpd start sudo service httpd status # Start amazon-cloudwatch-agent-ctl sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

       

    5. Add a hook file to direct the logs to the error file /var/log/httpd/error_log
      .platform/hooks/predeploy/serverConfig.sh

      #!/bin/bash # the script should be loaded after the www.conf # EB configures the php errors to go to /var/log/php-fpm/www-error.log, # but doesn't include that file in the default log files sent to CloudWatch. # This conf file directs the log files to the error file that is being sent to CloudWatch cat <<EOT > /etc/php-fpm.d/www-override.conf [www] php_admin_value[error_log] = /var/log/httpd/error_log ;catch_workers_output = yes ;App relevant configuration php_value[memory_limit] = 1024M

       

    6. Add the above 4 files inside .platform/confighooks as well to execute it on every configuration change. To avoid duplicate code refer to the above file as the source.

      1. Add a hook file for Creating a symlink to the EFS mounted drive
        .platform/hooks/postdeploy/01_initiateMnt.sh

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

         

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

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

         

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

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

         

      4. Add a hook file to direct the logs to the error file /var/log/httpd/error_log
        .platform/hooks/predeploy/serverConfig.sh

        #!/bin/bash source "/var/app/current/.platform/hooks/predeploy/serverConfig.sh"

         

         

    7. Add a file inside .platform/httpd/conf.d/elasticbeanstalk/vhost.conf to set the Environment configuration change.

      1. Add a hook file for setting the ENVIRONMENT & include_path
        .platform/httpd/conf.d/elasticbeanstalk/vhost.conf

        #Staging SetEnv ENVIRONMENT Staging #Production #SetEnv ENVIRONMENT Production

         

         

         

Create a new EB 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.1 running on 64bit Amazon Linux 2

      3. Platform version → 3.5.2 (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

  5. Set up the Environment configuration page

    1. Presets

      Select Custom configuration

    2. Modify software :

      1. Proxy server → apache

      2. Document root → /public

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

    3. Modify instances :

      1. Root volume type → General Purpose (SSD)

      2. Size → 50GB [Staging] / 30GB [Production]

      3. EC2 security groups →

        1. Staging - [Select the Zend API security group for access]

        2. Production - None

    4. Modify capacity :

      1. Auto scaling group → Environment type → Load Balancer → [Min-1, Max-2]

      2. Instance Type → Staging - t2.small, Production - m4.large

      3. Scaling triggers → CPU Utilization → maximum → Percentage [80, 20]

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

      1. Load balancer type - Application Load Balancer

      2. Staging -
        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.]

      3. Production - None

    6. Rolling updates and deployments :

      1. Deployment policy → Rolling with additional batch

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

    7. Security :

      1. Service Role → aws-elasticbeanstalk-service-role

      2. Virtual machine permissions → Choose EC2 key pair → aws-elasticbeanstalk-ec2-role

    8. Monitoring :

      1. Health reporting → Basic

    9. Click on → Create Environment

 

Health Check Configuration :

  1. Load balance → Listeners → Rules → Health check →

    1. Health check protocol → HTTP

    2. Path → /uploads/alive.txt

 

DB Security group configuration [Production]

  • Add the EB environment security group to the “DB-Production” security group

Set up Pipeline to Deployment to the new Environment

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

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

EC2 Instance Setup with the correct configuration

  1. SSH to the EC2 Instance [PHP 8.1 running on 64bit Amazon Linux 2]

    sudo service httpd status php -v sudo yum update -y sudo amazon-linux-extras | grep php # chkconfig has been replaced by systemctl in Linux 2 instance. sudo systemctl enable httpd systemctl is-enabled httpd #sudo yum install -y php-{pear,cgi,curl,mbstring,gd,gettext,bcmath,xml,intl,zip} #sudo yum install -y php-{memcached,devel,opcache,apcu,igbinary,imagick,odbc,pdo_pgsql,pgsql,soap,ssh2,uuid,xmlrpc} ## Error / Unsuccessful #sudo yum install -y php-{imap,mcrypt,mongodb,OAuth,PDO_ODBC} ###### Don't install PHP ###### sudo yum install -y php # It makes Thread Safety - Enabled
  2. Provide ssh access for developers if needed. [sudo nano ~/.ssh/authorized_keys]

  3. Make Sure its “Thread Safety - Disabled
    If “Thread Safety - Enabled”, it has issues while serving the concurrent requests.

    echo "<?php phpinfo(); ?>" >> /var/www/html/public/info.php ### Don't install PHP ### sudo yum install -y php # It makes Thread Safety - Enabled

    Access the website to verify the phpinfo.
    Ex : https://apiv2al2.com/info.php

     

  4. Create AMI for the image with PHP 8.1

  5. Use the AMI on the EB environment configuration

  6. If needed restart/rebuild the EB environment. [Rebuild EB environment fails as of now]

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

 

Setup Cloudwatch

Update the pipeline config value for

  1. EB environment name

  2. EB application name.

Update Route53 for the specific hosted zone [Ex: s1apiv2.com]

  1. Staging: Setup 1 record on Route 53 as follows:

    1. s1apiv2.com s1apiv2.us-west-2.elasticbeanstalk.com

  2. Production: Setup 2 records on Route 53 as follows:

    1. apiv2.com prodapiv2.us-west-2.elasticbeanstalk.com

Testing

Use the Domain name on Postman to verify the functionality.

 

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 httpd is listed

    systemctl list-units --type=service
  • To enable httpd start on boot up

    sudo systemctl enable httpd
  • To check if httpd enabled to start on boot up Linux system

    systemctl is-enabled httpd

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