How to have a free website hosting and SSL on AWS EC2 and Ubuntu 18.04

Are you looking for a free hosting space for deploying your website? You should consider Amazon Web Services (AWS) EC2 solution. Using AWS EC2 is quite simple and all you need to do in create your own instance.

After logging in to AWS console, click on EC2 and then proceed to Create Instance. Select Ubuntu 18.04 and click on next. Select t2.micro. Allocate necessary space in GBs and click on Proceed.

You need to create your key pair which is needed for accessing the instance using FTP / Putty. Once your instance is ready, click on Connect.

The system would ask for username, enter ubuntu.



Enter the following command for setting up Apache2 and MySQL

sudo apt-get update

sudo apt-get upgrade

sudo apt install apache2

sudo apt install mysql-server

After installation, you need to setup the MySQL password. It can be done using the following commands

sudo mysql

SELECT user,authentication_string,plugin,host FROM mysql.user;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';


SELECT user,authentication_string,plugin,host FROM mysql.user;


Once the MySQL is configured, you need to install a connection library which enables the communication between Apache2 and MySQL.

sudo apt install php libapache2-mod-php php-mysql

sudo systemctl restart apache2

sudo systemctl status apache2

sudo a2enmod rewrite

sudo systemctl restart apache2

sudo nano /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted

. . .


sudo systemctl restart apache2

Now, you should be in a position where your Apache2 can communicate with MySQL. By default, /var/www/html/ folder would be used for hosting your files; but if you want to change the default configurations, follow the below steps, else you can proceed to configuring the DNS records.



Replace domain_name with your registered domain name.

sudo mkdir /var/www/domain_name

sudo chown -R $USER:$USER /var/www/domain_name

sudo chmod -R 755 /var/www/domain_name

nano /var/www/domain_name/index.html

Configure Apache2 sites available by creating the a conf file.

sudo nano /etc/apache2/sites-available/domain_name.conf

Now you need to add the below code

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName domain_name
    ServerAlias www.domain_name
    DocumentRoot /var/www/domain_name
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Save the file and exit. Now you need to enable apache2 for loading the data from your newly created folder.

sudo a2ensite domain_name.conf

sudo a2dissite 000-default.conf

sudo apache2ctl configtest

sudo systemctl restart apache2

You are almost done with the configuration!!

Copy the IPv4 address from EC2 dashboard. Go to the Domain Control Panel of your registered domain name.



Create a A record entry and add the IPv4 address here.

It would take some time for the DNS changes to propagate. Usually, it takes around 1 hr or so for the changes to reflect throughout the globe.

Now your domain name must be fetching the index.html which we created earlier.

Head back to the instance and enable your free SSL certificate using the below commands.

sudo add-apt-repository ppa:certbot/certbot

sudo apt install python-certbot-apache

sudo nano /etc/apache2/sites-available/

sudo apache2ctl configtest

sudo systemctl reload apache2

sudo certbot --apache -d domain_name -d www.domain_name

Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience. You can deploy your website files using Filezilla or any FTP application using the Key generated earlier.