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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay