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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

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

Okay