I wanted to launch a little static website on an EC2 instance and I followed the following steps:
launched a t2.micro instance using console in us-east-1 region
attached an existing security group which allows http request, https and ssh.
logged in my ec2 through SSH and changed it into an apache web server using following commands:
commands:
#bin/bash
sudo su
yum update -y
yum install httpd -y
cd /var/www/html
Then I wrote a hello world html code in index.html file and started my web service
service httpd start
chkconfig on
Even after following all the above steps, when I open try to access the webpage by going to the public ipv4 address of my ec2 instance, I am getting a timout error.
Any idea what I might be missing here?
Thank you.
You can try creating new EC2 instance like below
Lunch the same type of instance
Add below script in user data to create HTML page
#!/bin/bash
Use this for your user data (script from top to bottom)
install httpd
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello World - $(hostname -f)" > /var/www/html/index.html
After launching EC2, open that security group and add below HTTP rule
At the end, manually type HTTP://your public address. sometimes by default its shows with HTTPS so just ensure you are typing HTTP only.
Related
I wrote a very simple spring-boot application and packed it in Docker.
The content of docker file is:
FROM openjdk:13
ADD target/HelloWorld-1.0-SNAPSHOT.jar HelloWorld.jar
EXPOSE 8085
ENTRYPOINT ["java", "-jar", "HelloWorld.jar"]
I pushed it to docker hub.
I created a new EC2 instance on aws. Then I connected to it and typed the following commands:
sudo yum update -y
sudo yum install docker -y
sudo service docker start
sudo docker run -p 80:8085 ****/docker-hello-world
The last command gave many messages on the screen that said that spring-boot application is running.
Looks great. However, when I opened my browser and typed: "http://ec2-54-86-87-68.compute-1.amazonaws.com/" (public DNS of EC2 machine).
I got "This site can’t be reached".
Do you know what I did wrong?
Edit: security groups that regard this machine are "default" and the following group that I defined:
Inside the EC2 machine, I typed:"curl localhost:8085" and got:
"curl: (52) Empty reply from server"
Ensure that your port's inbound traffic is enabled for your local IP address in your ec2 instance security group configuration
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html#adding-security-group-rule
Have you allowed inbound traffic for port 8085 in your security group configuration? That should be the first thing to check.
I found the solution.
It was port issues.
Instead of running
sudo docker run -p 80:8085 ****/docker-hello-world
I had to run:
sudo docker run -p 8085:8080 ****/docker-hello-world
This command says: "take the application that runs on port 8080 in the application and put it on port 8085 on docker".
I opened the browser and browsed to: "http://ec2-18-207-188-57.compute-1.amazonaws.com:8085/hello" and got the response I expected.
I am unable to reach EC2 instance after installing NGINX on that EC2 instance didn't get why this happen I just run below commands:
sudo apt update
sudo apt install nginx
sudo ufw app list
sudo ufw enable
sudo ufw allow 'Nginx HTTP'
sudo ufw status
before installing NGINX i am able to take SSH connection of EC2 but after doing this I am not reach to EC2 i cross check everything
SG Inbounds has enable 22 port [trying with all of three MyIp,Custom,Anywhere],
VPC has internet gateway which is properly bind
is there anything that i left?
or
"sudo ufw enable"
command run on my EC2 is creates the issue
Stop the running EC2 instance
Detach its /dev/sda1 volume
Start another EC2 instance, it should be on the same subnet
Attach volume to the new micro instance, as /dev/xvdf
SSH to the new instance and mount the volume to /mnt/tmp
sudo fdisk -l # check volumes and find the volume we wanna mount, in my case it's /dev/xvdf1
sudo mkdir /mnt/tmp
sudo mount /dev/xvdf1 /mnt/tmp
Disable UFW by setting ENABLED=no in /mnt/tmp/etc/ufw/ufw.conf
cd
sudo nano /mnt/tmp/etc/ufw/ufw.conf
Exit
Terminate the new instance
Detach the volume from it
Attach volume back to the main instance as /dev/sda1 Start the main instance
Login as before
Enable ssh connections
sudo ufw allow ssh
sudo ufw enable
Kudos to: https://stackoverflow.com/a/49432706
You should allow SSH connections before enabling the firewall so;
Execute
sudo ufw allow ssh
Before
sudo ufw enable
How do I configure a Python script to run as a service (re-launch on system restart, restart on failure) in Amazon AWS EC2 instance?
You can create a systemd service on the ec2 instance to achieve this. Steps are:
Create a service definition file:
sudo vi /lib/systemd/system/mypythonservice.service
Add the systemd unit file definition. You can check this or the systemd reference guide for more details:
[Unit]
Description=My Python Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/myuser/mypythonproject.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
Set the necessary permissions on the file:
sudo chmod 644 /lib/systemd/system/mypythonservice.service
Reload the systemd daemon:
sudo systemctl daemon-reload
Enable the service to start on reboot:
sudo systemctl enable mypythonservice.service
And of course you can add all of this as part of a EC2 Instance User Data script to automatically configure on instance launch.
Configure a Python as a service in AWS EC2
After much unsuccessful research to set up a Python API written on custom port 8080 to run on Amazon's Linux AMI operating system (AWS), I decided to solve this dilemma and share the solution with all of you.
See the solution in this link.
I have a linux AWS instance. I am running the following script on it:
#!/usr/bin/env bash
#This script installs java, sbt and the application
#Run this script on a new EC2 instance as the user-data script, which is run by `root` on machine start-up.
sudo yum update -y
sudo yum install -y docker
sudo service docker start
docker run repo/carrie
Everything installs and I get the below message in the logs:
REST interface bound to /0.0.0.0:8080
However when I try to actually access the port like so:
curl 0.0.0.0/8080
I get the below message:
Failed to connect to 0.0.0.0 port 8080: Connection refused
I have tried editing the inbound rules so that 8080 is open but it doesn't seem to work. Maybe because I'm editing the rules after the instance has already launched?
You have to publish the container's port to the host in the docker run command
$ docker run --help
...
-p, --publish list Publish a container's port(s) to the host
...
The last line of your script should look like this if the process in the container listens on port 80:
docker run -p 8080:80 repo/carrie
The container gets its own interface, hence host's 0.0.0.0 is not applicable.
Tell docker to bind container port 8080 out to the host:
docker run -p 8080:8080 repo/carrie
How do I run and access iPython Notebook (in Docker on EC2) from the browser?
This is what I tried:
From EC2 Quick Start menu, selected Amazon Linux AMI 2015.03 on t2.micro instance.
Everything left as default, except 3 rules created for "Configure Security Group":
Type: "SSH"; Protocol: "TCP"; Port Range: "22"; Source: "Anywhere";
Type: "HTTPS"; Protocol: "TCP"; Port Range: "443"; Source: "Anywhere";
Type: "Custom TCP Rule"; Protocol: "TCP"; Port Range: "8888"; Source: "Anywhere";
After SSH'ing to instance:
$ sudo yum install -y docker ; sudo service docker start
$ sudo docker pull continuumio/miniconda # Anaconda includes iPython Notebook
$ sudo docker run -it -p 8888:8888 continuumio/miniconda ipython notebook
Then launching browser to https://ec2-xx-x-x-xxx.compute-1.amazonaws.com:8888 didn't work.
I wouldn't be too comfortable opening 443 and 8888 on the Internet for my EC2 instances. My common setup is Anaconda on an Ubuntu box.
I usually ssh port forward my ipython notebook sessions to my localhost on my macbook with this ssh command:
ssh -i myPrivateSSHKey.pem ubuntu#54.1.2.3 -L 8888:localhost:8888
Then I open Chrome and request URL:
http://127.0.0.1:8888