If you believe my posts will help someone please feel free to share.
We’ve installed Apache, a web server but we still can’t see a web page.
This is because you can’t accept inbound traffic. You are allowing inbound traffic on port 22 for SSH but not on port 80 needed for HTTP.
So we need to allow Inbound traffic from Port 80
And now when we visit our public domain we see the default web page of the Apache HTTP server.
This page tells us the default location to add HTML content
[ec2-user@ip-172-30-1-107 ~]$ ls -la
drwx------ 3 ec2-user ec2-user 4096 Feb 16 19:22.
drwxr-xr-x 3 root root 4096 Feb 16 19:22..
-rw-r--r-- 1 ec2-user ec2-user 18 Aug 30 19:00 .bash_logout
-rw-r--r-- 1 ec2-user ec2-user 193 Aug 30 19:00 .bash_profile
-rw-r--r-- 1 ec2-user ec2-user 124 Aug 30 19:00 .bashrc
drwx------ 2 ec2-user ec2-user 4096 Feb 16 19:22.ssh
[ec2-user@ip-172-30-1-107 html]$ cd /var/www/html
[ec2-user@ip-172-30-1-107 html]$ ls
[ec2-user@ip-172-30-1-107 html]$ ls -la
drwxr-xr-x 2 root root 4096 Feb 16 21:41 .
drwxr-xr-x 6 root root 4096 Feb 16 21:18 ..
[ec2-user@ip-172-30-1-107 html]$ nano index.html
Oh nooooooo permission denied (see message at bottom of next image).
What the hell. Well its because the permission for this directory is set to root, and we are logged in as ec-user.
Have you noticed in our command line
If we want to create a file in this directory as is, we need to write the file with root permissions.
You NEVER want to log in as root, and it is really hard in an Amazon EC2 server without some making some serous changes. Fortunately we can use the sudo command to do this.
If you add the prefix sudo to most linux commands, that command will run with elevated privileges required to perform certain, usually administrative tasks.
Basically if you need to do shit as root then instead of logging in as root use the sudo command.
The ec2-user has sudo privileges on AWS EC2 servers.
So a quick aside as I show you.
Sometimes you can find the list of users who can sudo here
However, on this Amazon EC2 instance the file is located here
and we don’t have permission to see it as ec2-user
This is a bit meta but if you need to elevate your privileges with sudo to read the contents of the sudoers file.
[ec2-user@ip-172-30-1-107 etc]$ sudo nano /etc/sudoers.d/cloud-init
When the files opens you will see ec2-user listed.
A side note, you may have tried to use the change directory command, cd to get into the sudoers.d directory; however, you would get a permission denied as this directory requires root. You can’t use sudo cd because cd is part of the shell.
sudo foo means run the program foo as root. cd is not a program but an in-built command and sudo only applies to programs.
If for some reason you really wanted to cd into this directory you’d have to become root (this is NOT recommended)
[ec2-user@ip-172-30-1-107 etc]$ sudo -s
[root@ip-172-30-1-107 etc]# cd sudoers.d
[root@ip-172-30-1-107 sudoers.d]# ls
To get out of root and back to ec2-user simply use exit
[root@ip-172-30-1-107 sudoers.d]# exit
See how the username before the @ changes from root to ec2-user
Phew ok back to making out first HTML page.
Control X will exit and ask you to save.
Navigate back to the public url for this EC2 instance. Boom!