Skip to main content

Command Palette

Search for a command to run...

User data in AWS

User data in EC2 Instance

Published
2 min read
User data in AWS

How to Use User Data and Install Nginx on an EC2 Instance During Creation

As shown in 1st part we will launch in ec2 instance

In the networking section, we will allow SSH and HTTP traffic.

Next, we will click on Advanced details at the bottom and go to the user data option.

In the user data section, we will write a script to install Nginx.

The user-data script always runs as the root user.

#!/bin/bash
apt-get update
apt-get install nginx -y
echo "Welcome to my web page :)"> var/www/html/index.html

And now launch instance.

Now paste your public IP into a new tab and check if the web page is displaying.

It’s running fine :)

How to Manually Install Nginx on an EC2 Instance

Login as root user

sudo -i

Now we will update & Upgrade our server

apt-get update
apt-get upgrade

if not login as root user then use below commands

sudo apt-get update
sudo apt-get upgrade

Command to install Nginx

sudo apt-get install nginx -y #we use sudo when we are not login as root user

Now we will check nginx configuration

sudo nginx -t

Now we will check the current status of the Nginx web server

service nginx status

and we can use systemctl command for below mentioned actions

  • sudo systemctl status nginx — Check the status of the Nginx service.

  • sudo systemctl start nginx — Start Nginx.

  • sudo systemctl stop nginx — Stop Nginx.

  • sudo systemctl restart nginx — Restart Nginx.

  • sudo systemctl reload nginx — Reload Nginx without stopping it.

  • sudo systemctl enable nginx — Enable Nginx to start on boot.

  • sudo systemctl disable nginx — Disable Nginx from starting on boot

now we will access Nginx on localhost

curl localhost

Why did we get this result?

It's because there is a file located in the directory /var/www/html.

cd /var/www/html

Now, if we want to display something different on the Nginx web server, we can create a new HTML file and add the necessary details to it.

If we are not logged in as the root user, we need to change the permissions for this folder; otherwise, permission will be denied.

sudo chmod 777 /var/www/html
echo "Welcome to my web server :)"> index.html

Now we will check on localhost.

More from this blog

DevOps journey

34 posts

In this DevOps journey, we’ll explore a range of DevOps tools and related projects.

User data in AWS