Saturday, 9 September 2023

Configure EFS for Web Server

 Configure Amazon Elastic File System (EFS) to serve files for Apache Web Servers.


Step 1: Go to AWS console and search for EFS service. Click on Create file system.

Give the name as MyEFS. Note the VPC that this file system is created in.

Click on Create.

Ensure that the File system state is Available.


Step 2: Go to EC2 services and create a Linux Instance. Add tag as Name: WebserverEFS.

Make sure to have SSH and HTTP rule in the security group.

Note the Subnet (AZ).

For instructions and help in creating Linux Instance refer he the AWS EC2 Linux Instance document.


Step 3: Now create a new security group for the purpose of associating with the EFS.

In EC2, go to Network & Security -> Security Groups. Click on Create security groups.

In Basic details, name the group EFS-SG. Give appropriate description.

Scroll down to Inbound Rules. Click on Add Rule. Give the following attributes:

  • Type: NFS

  • Protocol: TCP, Port Range: 2049 (This is default selected)

  • Source: WebServerSG (The security group associated with the created Linux Web Server Instance)

Scroll to Outbound rules and check the default All Traffic value under Type.

Click on Create security group button in bottom right corner. Confirm that it is created.


Step 4: Go to EFS console. Select the file system created above. Click on View details. Scroll down and select Network. Click on Manage.

Here all the AZs from the present Region are listed along with the security groups. The default selection is default SG.

Click on the x and remove the default security group. Search and add the custom EFS SG created in above step. Click on Save.


Step 5: SSH into the Instance.

Now install amazon-efs-utils package which has efs mount helper using the following command:

sudo yum install -y amazon-efs-utils

Run the following command to install Apache Server:

sudo yum -y install httpd

And with the next command start the server:

sudo service httpd start


Step 6: After the utility installation are complete, make a directory for the mount point on Apache web server using the following command:

sudo mkdir /var/www/html/efs-new-mp

Run the next command to change directory to the html.

cd /var/www/html

Mount the file system to the directory created in the above step using the following command. Ensure that you replace the files system id that is marked blue in the below command with your own EFS ID:

sudo mount -t efs fs-2886e6f9:/ efs-new-mp


Step 7: Change the directory to the mount point that is created above using the command:

cd efs-new-mp

Create a new sub directory with following command:

sudo mkdir webappdir

Change the permissions of the above subdirectory with the following command:

sudo chown ec2-user webappdir

Change the directory to webappdir directory with following command:

cd webappdir

We load an HTML file into this directory:

echo "<html><h1>Hello from your EFS</h1></html>" > hello.html

To check, copy the DNS name of your Webserver instance and replace it in the blue part of the following URL. Run this URL and you should be able to access the HTML file.

ec2-35-154-125-101.ap-south-1.compute.amazonaws.com/efs-new-mp/webappdir/hello.html

Let this session be running.

Step 8: Now proceed to create one more Linux WebServer instance in a different subnet (AZ) than the first one. Make sure the same WebServerSG is associated to this instance.

Step 10: Follow Step 5 and Step 6 to SSH, install utils, install apache web server, create mount point directory and mount it.

Step 11: To check, copy the DNS name of your second Webserver instance and replace it in the blue part of the following URL. Run this URL and we should be able to access the HTML file.

ec2-13-233-68-216.ap-south-1.compute.amazonaws.com/efs-new-mp/webappdir/hello.html

Note: Close both sessions, Terminate the instances and delete the Elastic File System if you no longer need them.


No comments:

Post a Comment