DEV Community

Cover image for Installing Apache or Nginx on Amazon EC2 Linux 2 AMI using a user data script
Buddhi Eashwarage
Buddhi Eashwarage

Posted on

Installing Apache or Nginx on Amazon EC2 Linux 2 AMI using a user data script

When you're going to host a website on an Amazon EC2 instance by using an Amazon Linux 2 AMI, you need to set up a web server.

It can be either Apache or Nginx.

Installing a web server manually by SSH into the instance later once you spin up the instance will be easier when you are dealing with only 1 instance.

But when you're going to deal with a fleet of instances that will have 3-4 instances, this will be a time-consuming thing.

Ex:- You're going to configure an ALB (Application Load Balancer) with different target groups. In that case, you'll need to maintain the same web server across several EC2 instances.

Therefore, the most efficient way is to add a bash script under "User data" section in "Advanced details" when you're going to create an EC2 instance, and before you spin up the instance.

Advanced details

Add the script here.

User data

Script to install Apache web server.

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd

Script to install Nginx web server.

#!/bin/bash
yum update -y
amazon-linux-extras install nginx1 -y
systemctl enable nginx
systemctl start nginx

Top comments (0)