Two Websites on One AWS LAMP Stack

Written by bobnoxious | Published 2022/03/29
Tech Story Tags: aws | aws-services | lamp-stack | amazon-lightsail | ssl | web-development | backend-developer | apache

TLDRvia the TL;DR App

I had the proverbial grand idea, and like a lot of these ideas, if they are at all realistic they are likely to have a few components that will serve to instantiate the idea. One of the components I needed here is not a new concept or idea in and of itself, but is in fact old school Apache Web Server.
We have one named domain being hosted on a LAMP stack server, and for various reasons, we want to have a second named domain hosted on the same server. It would certainly make it easy for one of the two domains to see what you were doing on the second domain, or alternatively provide content on the second domain based upon actions in the first.
The Apache documentation offers a page titled VirtualHost Examples at this link VirtualHost Examples - Apache HTTP Server Version 2.4 and the first entry there is titled “Running several name-based web sites on a single IP address.” The section presents a synopsis of how to accomplish this under Apache using “virtual host configuration files”.
I am not a pitchman for AWS, but I use it and I really like it. The two biggest reasons are first, that it is “economical” or as they say at my level, cheap, and second that it is the entire server, virtually. The reason I mention this is that there is no real financial incentive for using this to host multiple sites on one stack to save on server or hosting fees since a LAMP may be had with relative economy. A common use for this feature is to serve the same content to a list of domain names, for example to domain.com and domain.net and domain.org and so forth, but for our grand idea’s use we will serve different content for each of the two named domains we will host on our single IP. What is of import to the grand idea is that both of these Internet domains and their presence, their website, are in fact on the same host server.
The first requirement is obviously an Apache web server. We will assume that there is a functioning  AWS LAMP stack (on the AWS Lightsail cloud). The next consideration involves the resolution of those two domain names into the IP address of our web server. Let’s assume the LAMP stack is running as a web server and that our DNS services and domain registry use AWS Route53. 
Among those items we are going to include as a necessity to the grand idea is that our two domains both have SSL certification. Long away and far ago one of the reasons I made the jump to AWS had to do with SSL certificates. We are going to assume that our first domain uses SSL and that this function is provided by Let’s Encrypt ( https://letsencrypt.org/ )
So initially we have one of our two domains running successfully with SSL on the Internet using the AWS LAMP stack  host and DNS services. For our example this first domain is named isoblock.com.These are the steps we will take to add a second named domain to this already running server. The second domain in our example is named bobshitlist.com. The directions we present are not character- by- character line- by- line copy and paste commands, but there is enough detail shown such that each of the required tasks should not be difficult to understand and perform.
  1. Create a document root folder for the new domain
  2. Add content for the new domain
  3. Register the new domain
  4. Create DNS Zone record for the new domain that points to the first domain IP
  5. Add the new domain to the existing LetsEncrypt service
  6. Modify the Apache default virtual host configuration file
  7. Restart Apache

The to-do list step by step #

Document root folder for the new domain #
The result for the first two items in our to-do list are shown here in the figure below. We have a folder we named  public, that is added at the same level as the html folder and that has the same permissions. We did not change the owner and group names for these added folders. After we have created this new folder we can add some content as shown here below.
Set up DNS for the new domain #
In the next two steps on our to-do list we register our new domain name and create an appropriate  A record in the DNS zone file. This screenshot below is from a page in the AWS Lightsail management console and it shows the instances or servers we have running along with the IP address for each one. Note that we have selected a static IP address for the server (isoblock.com) that already hosts the first of our two domains.
The next screenshot below shows the A record we added to the new domain DNS Zone file, and as you may see this new A record is a simple pointer to the IP of the first domain.
AWS has a number of different features or methods by which we can accomplish these sorts of alias connections, but this method we have presented will work in most any Apache setup configuration.
Set up SSL for the new domain #
As we previously stated, this “multi-hosting” setup assumes that we already have a domain instance (web server) using the SSL protocol which in our case is supported by the LetsEncrypt certificate authority and the certbot program.
In the screenshot above we can see the results obtained by invoking certbot against our initial LetsEncrypt SSL setup. Canceling the certbot program as shown results in the message at the screen bottom which tells us how to include our new domain by invoking certbot with a list of domain names, the names shown as already covered by the current setup along with the addition of our new domain. Our command line will thus  be as shown here in the next screen below. We will tell certbot to (E)xpand the existing certificate to add our new domain and continue the setup.
Modify the Apache Virtual Hosts Configuration File #
Our “multi-hosting” setup assumes that we already have a functioning Apache web server so all we will need to do is add our new domain information to our existing configuration (conf) file. The particular conf file we want to edit is located in the /etc/apache2/sites-available folder and the file is named 000-default-le-ssl.conf and this screenshot below shows the original config file content. Of special note here are the ServerName and ServerAlias values along with the DocumentRoot value, and that we also redirect Port 80 non-SSL traffic to our Port 443 SSL host.
We will modify this configuration file to add our new domain, and to do this we will essentially duplicate, with slight modifications, the two sections of the previous file listing resulting in our config file as modified. Note especially the substitutions of the ServerName and ServerAlias values, and the specification of our previously created DocumentRoot for the new domain. Note also that both of our domains use the same key files.
Restart Apache to use the new Virtual Hosts Configuration File #
Now all that remains is to restart the Apache Web Server so that it will reflect our configuration changes. For our example there is a new Internet domain named bobshitlist.com being served with the HTTPS (SSL) protocol from the same AWS LAMP stack that also serves the isoblock.com domain. This next screenshot is the present landing page for bobshitlist.

Conclusion

So we have seen a procedure that demonstrates how we may easily add a second hosted domain to a LAMP stack on the AWS cloud. Keep your eye out for the manifestation of the grand idea.



Written by bobnoxious | Bob has been designing hardware and coding software for decades. He likes to draw and write. He’s a web cadet wannabe.
Published by HackerNoon on 2022/03/29