It works! message while connecting the EC2 public IP in web browser

0

I created the brand new EC2 instance and included SSH and HTTP security group. Also while creating i have included the user data with the below code. But if i try to open the public IPv4 address (http:// only) in browser, i am always getting It works! message. I am not getting hostname and other details. if i change the user data also i am not getting anything. i am stuck here. Please help.

Actual in screen: It works! Expected : Hello world from (IP address) etc...

#!/bin/bash yum update -y yum install -y httpd systemctl start httpd systemctl enable httpd echo "<h1>Hello world from $(hostname -f) </h1>" > /var/www/html/index.html

Vijay
asked 6 months ago632 views
5 Answers
1

Hello Vijay,

I hope you already have found answer by now, incase not then it is related to permissions. Once you have installed apache server it internally uses ec2-user which by default have no permissions to create files so you need to assign those permissions. You can use below script to make it work.

#!/bin/bash
yum update -y
yum install -y httpd 
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
echo " <h1> Hello world from $(hostname -f) </h1> " > /var/www/html/index.html
answered 4 months ago
  • Thank you @Harmesh Choubisa! This one worked perfectly for me. I am surprised it is still not upvoted :)

1

Hi Vijay,

Can you please check the Apache Configuration file at /etc/httpd/conf/httpd.conf, if there are no conflicting configurations that might be affecting the display of your custom index.html file.

Thanks
Atul

profile picture
answered 6 months ago
1

Hello.

It is executed with the following user data.
The AMI uses Amazon Linux2023.

#!/bin/bash 
yum update -y 
yum install -y httpd
systemctl start httpd 
systemctl enable httpd 
echo " <h1> Hello world from $(hostname -f) </h1> " > /var/www/html/index.html

d

profile picture
EXPERT
answered 6 months ago
1

I believe this is because the default document is configured to be index.php and not index.html

If you connect to your public IP address /index.html this will rerun your hello world.

/index.php will return it works.

You have 2 options. Update the default document configuration or echo your hello world into index.php

profile picture
EXPERT
answered 6 months ago
0

I tried so many things, but its working if i delete the key pair. Not sure why. and once its started working if i attach it again, still its working. I really dont know the reason behind it. but right now its working if i delete the key pair.. I will try to find the reason in coming days and post it. Thank everyone for your response and suggestion.

Vijay
answered 6 months ago
  • The key pair is only needed for direct ssh access. So I’m dubious it makes a difference

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions