What are the steps to create a Load balancer on EC2 instance? Screenshots are also attached.
Firstly, we need a website on EC2 instance to put it behind the ELB (Elastic Load Balancer).
WordPress Installation on EC2 and Amazon RDS
Step 1: Create an EC2 Instance.
Click on Launch Instance. As shown in the following screenshot.
Step 2: Choose AMI
In our case, we are going to select “Ubuntu Server 18.04 LTS”.
Step 3: Choose Instance Type
we have selected the “t2.micro” instance.
Configure Security Group
Also, we have to create a security group, as shown below, the security group has the following “inbound rules“:
As you will click on Review and Launch, you need to create a key pair or you can choose an existing key pair.
Finally, launch the Instance.
Prerequisites for WordPress
Web Server (Nginx)
Run the following command to install Nginx:
sudo apt-get install -y nginx
Install PHP 7.3 using following commands:
# sudo add-apt-repository ppa:ondrej/php # sudo apt-get update # sudo apt-get install -y php7.3 php7.3-fpm php7.3-cli php7.3-mbstring php7.3-mysql php7.3-curl php7.3-xml php7.3-bcmath php7.3-bz2 php7.3-common php7.3-dev php7.3-gd php7.3-imap php7.3-json php7.3-opcache php7.3-soap php7.3-sqlite3 php7.3-zip # sudo service php7.3-fpm start
Install ionCube Loader
Ioncube loader for PHP is optional. It depends upon your project requirements.
#wget https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz #tar -zxvf ioncube_loaders_lin_x86* #cd ioncube/ #ls –l
Here you need to select the right ioncube loader for installed PHP version on the server. To check the installed PHP version, just type the below command:
The next step is to find the location of the extension directory for PHP version 7.3, it is where the ioncube loader file will be installed.
php -i | grep extension_dir
Now need to copy ioncube loader for our PHP 7.3 version to the extension directory (/usr/lib/php/20180731).
sudo cp /tmp/ioncube/ioncube_loader_lin_7.3.so /usr/lib/php/20180731
The last step is to configure ioncube loader in following both files to work with PHP, in the
sudo vi /etc/php/7.3/cli/php.ini sudo vi /etc/php/7.3/fpm/php.ini
Add the following line as the first line:
zend_extension = /usr/lib/php/20180731/ioncube_loader_lin_7.3.so
Finally, restart Nginx & PHP-FPM service.
sudo systemctl restart nginx sudo systemctl restart php7.3-fpm
To check ioncube loader is installed, run following command:
Install WordPress on EC2 Instance
To start the installation, SSH, and log in to your EC2 Ubuntu machine using your terminal console. Run the following command.
To extract the compressed file into your website root directory, run the below command:
tar -zxvf latest.tar.gz
Create RDS Instance
We will install the RDS instance here.
Select the type of “Database Engine”
Configure database Settings
In the next step configure database instance identifier, username, password, etc.
After configuring settings, click on create a database. As shown in the below screenshot.
Connect WordPress Site with RDS Instance:
Under web server root directory, edit wp-config.php file and update database, user, password, and hostname. Now you can type IP in a browser and access your WordPress site.
Configure a Load Balancer
Step 1: Navigate to “Load Balancing” and click on “Create Load Balancer“.
Step 2: Select the “Load balancer type“. In this case, I’ve used the HTTP/HTTPS.
Step3: “Configure your load balancer“. Add name, Schema and IP address type (which is IPV4) in this case.
At the bottom Select VPC and Availability Zones.
After it, “Configure Security Settings“. It will prompt a warning as displayed in the picture below. This warning is just to let you know to use HTTPS instead of HTTP.
You can create a new or choose an existing security group.
The next step is to “Configure Routing“. As shown in the below screenshot.
Under “Register Targets“ add EC2 instance server.
The final step is to “Review” all the settings and create a Load Balancer.
The load balancer will now be created and deployed. You have a perfectly configured Load balancer on the AWS EC2 instance.
Configuring Amazon Route 53 to Route Traffic to an ELB Load Balancer
Update DNS record for a WordPress site to get traffic through Load Balancer. Here we have a DNS record hosted on Route 53.
1: open the Route 53 console
2: choose Hosted Zones.
3: Create a new A record with option Alias – Yes.
4: Under Alias Target – choose Load Balancer.
5: The final step is to click on create.
Install SSL (HTTPS) on Load Balancer
Step 1: Open the AWS certificate manager.
Step 2: Add domain name starting with *.domain_name.
Step 3: Next confirm and request. Once validation is completed, the certificate will be issued. For validation, add txt record in the WordPress domain DNS registrar.
Step 4: Navigate to Load Balancers and in the bottom pane, select the Listeners tab.
Step 5: Go to Edit and add https and add AWS certificate.
Choose the newly requested SSL.
Step 6: Save it and now SSL is deployed on the Load Balancer.