Setup EB Environment platform settings on the codebase
Download a working application version from Elastic beanstalk from the existing environment.
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 ... ...
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
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
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
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
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.
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"
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"
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"
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"
Add a file inside .platform/httpd/conf.d/elasticbeanstalk/vhost.conf to set the Environment configuration change.
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.
Go to Elastic Beanstalk.
Create a new Environment. https://us-west-2.console.aws.amazon.com/elasticbeanstalk/home?region=us-west-2#/environments
Select environment tier → Web server environment.
Enter
Application Name,
Environment Name
Domain name [Optional]
Platform → Managed platform
Platform → PHP
Platform branch → PHP 8.1 running on 64bit Amazon Linux 2
Platform version → 3.5.2 (Recommended)
Application code → Upload your code
Source code origin → Local file → Choose file → Upload a working application version downloaded from Elastic beanstalk from the existing environment.
Click on → Configure more options
Set up the Environment configuration page
Presets
Select Custom configuration
Modify software :
Proxy server → apache
Document root → /public
Environment properties → [Provide the key-value pair for setting Environment variables.]
ENVIRONMENT: Staging/Production
Modify instances :
Root volume type → General Purpose (SSD)
Size → 50GB [Staging] / 30GB [Production]
EC2 security groups →
Staging - [Select the Zend API security group for access]
Production - None
Modify capacity :
Auto scaling group → Environment type → Load Balancer → [Min-1, Max-2]
Instance Type → Staging - t2.small, Production - m4.large
Scaling triggers → CPU Utilization → maximum → Percentage [80, 20]
Load Balancer : [Do later for HTTPS to avoid grey status]
Load balancer type - Application Load Balancer
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.]Production - None
Rolling updates and deployments :
Deployment policy → Rolling with additional batch
Rolling update type → Rolling based on Health. [If multiple instances available]
Security :
Service Role →
aws-elasticbeanstalk-service-role
Virtual machine permissions → Choose EC2 key pair →
aws-elasticbeanstalk-ec2-role
Monitoring :
Health reporting → Basic
Click on → Create Environment
Health Check Configuration :
Load balance → Listeners → Rules → Health check →
Health check protocol → HTTP
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
Update Pipeline config value with correct EB Application & EB Environment for deployment.
Run the pipeline to deploy updated code to the new environment.
EC2 Instance Setup with the correct configuration
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
Provide ssh access for developers if needed. [sudo nano ~/.ssh/authorized_keys]
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.phpCreate AMI for the image with PHP 8.1
Use the AMI on the EB environment configuration
If needed restart/rebuild the EB environment. [Rebuild EB environment fails as of now]
Add Listener for Port - 443 - Https with valid Certificate details.
Setup Cloudwatch
Update the pipeline config value for
EB environment name
EB application name.
Update Route53 for the specific hosted zone [Ex: s1apiv2.com]
Staging: Setup 1 record on Route 53 as follows:
Production: Setup 2 records on Route 53 as follows:
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.