TutorialsJoint

Host Multiple domain, subdomain or website on AWS Lightsail ?

May 28, 2020

Amazon Lightsail an easy to use and compact version of Amazon AWS. It was a turning point how I used AWS. I was rather hesitant in using AWS before Lightsail as I am not good with Dev-Ops and managing cloud, but with Lightsail it becomes super easy. So Let’s answer the question on how you can configure multiple domain on lightsail with apache2 or

Can I host Multiple Domains or Websites on AWS Lightsail ?

Yes, you can host multiple domains on same instance of lightsail. Steps are really simple:

  1. Add DNS record to point domain or subdomain to same instance.
    1. It can be done via Networking Tab in Lightsail or from AWS Route 53.
  2. Create virtual host on the instance in order to map domain to a directory.
  3. You can also configure SSL certificate using Let’s encrypt and certbot for multiple domains.

Mapping Multiple domains on same Lightsail Instance

It is actually very easy to host multiple sites on same instance, it is as easy as creating a virtual host on your localhost environment. In this tutorial I will show you how to do the same. Using Apache virtual host as well as using nginx and webinoly which makes it easy to configure ssl certificate on the domains.

Create a new Ubuntu Instance in Lightsail

Set up Apache2 Server

To Setup a server, I am using Apache2 for first example you can use apache or nginx as per your liking.

Terminal window
sudo apt update
sudo apt install apache2
sudo service apache2 status

<VirtualHost *:80>
ServerName testa.mayanktiwari.in
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/testa
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I have removed the comments to keep it clean and short. First domain/app is now done. To configure second or more domains follow these steps.

So final code will look like code block below.

<VirtualHost *:80>
ServerName testa.mayanktiwari.in
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/testa
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName testb.mayanktiwari.in
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/testb
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

As you can see only two lines have changed, however I recommend changing the filename for error and access logs.

sudo service apache2 restart

That is all for the back end, Now back in Lightsail panel, go to Networking and create a DNS Zone.

Create and Map DNS Records

To create DNS Record please follow the steps below.

Lightsail Multiple Domains with SSL and Nginx

Previous section was all about apache2 and Lightsail networking tab to create DNS. If you wish to use Route 53 please refer to this post where I have described how you can Map Godaddy Domain to AWS Route 53. In this section I’ll continue the sections from above. This section I’ll describe how to use Webinoly to host multiple domains or multiple apps on Lightsail. I have another article which describes how you can host wordpress on Lightsail with Nginx and Let’s encrypt SSL.

Install webinoly – an optimized Nginx Web Server

Terminal window
wget -qO weby qrok.es/wy && sudo bash weby 3
Terminal window
#to install wordpress immediately
sudo site testa.mayanktiwari.in -wp
#to create a virtual host for any php site with MySQL support
sudo site testb.mayanktiwari.in -msyql
Terminal window
#list the existing sites
sudo site -list
+ testa.mayanktiwari.in (WordPress)
+ testb.mayanktiwari.in (PHP)
Found: 2 sites
Terminal window
#Enable ssl certificate
sudo site testa.mayanktiwari.in -ssl=on

All this information and more examples are available on this detailed tutorial page of webinoly.