Hello everyone.
In this blog, we will be discussing hosting a static website on AWS EC2.
What I will cover
1:Buying a domain and hosting the domain on AWS ROUTE 53
2:Hosting a website
3:How to configure SSL
1:Buying a domain and hosting the domain on AWS ROUTE 53.
-
There are multiple buying options in the market for domain buying and hosting platforms like
1.GoDaddy- Hostinger
- Bluehost
- Freenom(free domains) But, I would suggest using GoDaddy. And if you want to buy only for practice purposes then you can go for Freenom. In this blog, I will be using GoDaddy.
After buying a domain from Godaddy. Go to manage DNS.
After clicking u will see the Nameservers as shown below
- Then log in to your AWS console and go to ROUTE 53. There we will be hosting our DNS. To do that first create a hosted zone by giving your domain name.
- After that, you will be seeing some nameservers on route 53 you have to copy that and paste that on Godaddy nameserver. Then your DNS hosting will shift from GoDaddy to AWS.
2: Hosting a website
- Launch an EC2 instance. Here I am using amazon Linux 2. Make sure to allow port 80(HTTP) and port 443(HTTPS) for everyone.
After that connect to the instance using an ssh client.
Connect to the root by following the below commands.
sudo su
cd
- After connecting to the root install apache to make a web server by throwing the below commands.
yum install httpd
Then go to the following directory.
cd var/www/html
Here you need to move your website files.
For that, we will be using winscp
Here is the link to download winscp.
- After installing winscp. Open WinSCP and paste your public IP of the ec2 instance in the hostname. Then go to Advance->Authentication. There you need to give your private key.
Then give ec2-user as user-name.
Then move your file to the /var/www/html directory.
- Come back to your ssh client and check if the files are available in the/var/www/html directory or not by throwing
ls
If your files are there the throw below commands.
systemctl start httpd
systemctl enable httpd
Your website is ready for hosting. You can copy your IP in chrome and check whether the website is live or not.
Okay, you successfully hosted your domain and deployed your website on your server.
After completing the previous steps you need to register your IP with your domain.
For that go to hosted zone you have created in ROUTE 53 and click on create record there you have to create a record by giving www as a subdomain(Give names by using those you want your customers to come over to your website) and paste your public IP in the value box.
There you go. If your website is successfully deployed you can check by hitting your subdomain and domain name on the browser.
3:How to configure SSL using Let's Encrypt
1.Download the Extra Packages for Enterprise Linux (EPEL) 7
repository packages. These are required to supply dependencies
needed by Certbot.
-Navigate to your home directory (/home/ec2-user). Download
EPEL using the following command.
sudo wget -r --no-parent -A 'epel-release-*.rpm'
https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
-Install the repository packages as shown in the following
command.
sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm
-Enable EPEL as shown in the following command.
sudo yum-config-manager --enable epel*
-You can confirm that EPEL is enabled with the following command.
sudo yum repolist all
It should return information similar to the following.
[ec2-user ~]$
...
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 enabled: 12949+175
epel-debuginfo/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 - Debug enabled: 2890
epel-source/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 - Source enabled: 0
epel-testing/x86_64 Extra Packages for Enterprise Linux 7 - Testing - x86_64 enabled: 778+12
epel-testing-debuginfo/x86_64 Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Debug enabled: 107
epel-testing-source/x86_64 Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Source enabled: 0
...
2.Edit the main Apache configuration file, /etc/httpd/conf/httpd.conf. Locate the "Listen 80" directive and add the following lines after it, replacing the example domain names with the actual Common Name and Subject Alternative Name (SAN).
<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName "example.com"
ServerAlias "www.example.com"
</VirtualHost>
Save the file and restart Apache.
sudo systemctl restart httpd
Your website is ready.
I hope you will find this informative.:>
Top comments (0)