Virtual hosting in LAMP stack
   Dec. 30th,2017
   Ismail Nassor Mbarack

In previous tutorials, we learned how to install LAMP stack in your Ubuntu OS.We ended up on how you can use your IP address to view your website content.However, in real life, people won't use your IP address to access your website, they will simply use your domain eg: www.isunaslabs.com .So in this series of tutorials, we'll cover how to connect your IP address to your domain and how to host multiple domains and subdomains on the same server.

I assume that you can be able to view your website content via your IP address, if not go back and watch them first.You should also have phpMyAdmin installed on your server already though it isn't a necessity to follow along with this tutorial.Go to your domain setting, depending on where you bought your domain whether it's from GoDaddy, Hostgator or anywhere, find nameservers settings and change them to the Nameservers that match your current server.If you can't find where to change your domain Nameservers you can always ask your customer service they would be able to assist you with that.My servers are at Digital Ocean so I'll change my domain's nameservers to the digital ocean ones.

 

As you can see above that I'm using a domain tzsemall.com and I just changed my nameservers a.k.a NS-Records to digital ocean ones or if you don't have any nameservers fields you should create new ones.Digital Ocean nameservers are NS1.DIGITALOCEAN.COM   NS2.DIGITALOCEAN.COM  NS3.DIGITALOCEAN.COM .Above I only filled in two nameserver settings since my domain vendor accepts two nameservers as well.Changing nameservers means pointing your domain in a certain direction.What we've done just now is to point our domain to Digital Ocean data centers.

Next step we have to redirect our domain to our server's IP address through Digital Ocean domain control panel.Navigate to Networking on your Digital Ocean panel, you'll see an option to add a domain.Type your domain name and click the add domain button.

Once you click the Add Domain button you'll be redirected to the page that will require you to create your A-Records and CNAME.

 

Have you noticed above, Digital Ocean has added the NS Records for us already.All we need to do is to add the A-Records and CNAME Records.A-Records tells the Internet which IP address users should be redirected to when they type your domain in their browsers.Our domain is tzemall.com we should have typed tzemall.com in the 'hostname' filed but according to Digital Ocean instructions as seen in the picture below we should type @ since we are linking the ID address to the root domain, next  type in our server IP address in the 'will direct to' field then click Create Record button.

 

Now let's create CNAMEs Records, CNAME Records are the aliases to your domain.Up above we told the Internet to redirect users to our IP address when they type our domains (tzemall.com) in their browsers, but at times users would type www.tzemall.com . we still need to redirect that traffic to our IP address and that's what CNAMEs do basically, redirecting our domain aliases to our domain.

 

What we've done above is to tell the Internet to redirect all the www.tzemall traffic tzemall.com which will redirect to our IP address.Awesome! Click Create Record, it'll take 30 minutes to 24 hours for all the traffic to be successfully redirected to your new Nameservers and new IP address.SO be patient, go get a cup of coffee, watch TV a little bit then come back and try to go on your browser and type your domain, for me, I'll type tzemall.com.

NOTE: Don't forget a dot at the end of hostname ie:tzemall.com. or yourdomain.com.

If you don't add it Digital Ocean will automatically add it for you recently.

 

It works!To confirm that your Domain Name Servers (DNS) have reflected or not go to https://www.whatsmydns.net and check it's status in different locations.A big question now, how do you host more than one domain on the same server?That's called virtual hosting. It's very easy on LAMP stack.Login your server using the terminal and do the following:

Navigate to sites-enabled by cd /etc/apache2/sites-available 

See there's file 000-default.conf, this is the default file that we need to copy and change it according to our needs.Copy this file by using the following command cp 000-default.conf tzemall.com.conf .Make sure you use sudo if you didn't log in as a root user.

NOTE: The name of the .conf file ha to exactly the same as the root of the domain or subdomain we want to create eg: if you want your users to be able to visit store.tzemall.com then your .conf file MUST be store.tzemall.com.conf .We'll create the store subdomain later in this tutorial.Now use nano to edit the tzemall.com.conf file by using the command sudo nano tzema..com.conf .Take a look at what this file contains before we trim all unnecessary stuff from it.

This is the trimmed file

Uncomment ServerName and change it to tzemall.com or yourdomain.com without www. Add ServerAlias www.tzemall.com   or  www.yourdomain.com .CHange DocumentRoot to /var/www/html/tzemall.com . We haven't created the directory tzemall.com yet in /var/www/html but we will very soon.Change ErrorLog to match the DocumentRoot location.At last your file would look as follows:

What we've done above is to redirect the traffic coming into our server through port 80 via tzemall.com and www.tzemall.com into the directory /var/www/html/tzemall.com .Apache opens the html directory to the public by default.Everything that we put in the html directory can be seen by the public but to become tidier we've decided to create another directory called tzemall.com that we'll put all out tzemall.com files and when we add other domains or subdomains we can put their files in their specific directories such as mystore.tzemall.com directory could be used for holding the store.tzemall.com files in the future.By the way, you could change ServerAdmin to your email address if you want but for the sake of this tutorial, we won't change that.Exit and save the file.Make a tzemall.com directory, mkdir /var/www/html/tzemall.com.Create a simple index.php file in it by nano /var/www/html/tzemall.com/index.php and add some simple PHP scripts to it then save it.

The very last step is to enable our domain or subdomain so that the world can visit it and see our website content.Run the command a2ensite tzemall.com.conf 

 

Restart apache by service apache2 reload

When you go to tzemall.com or www.tzemall.com by now you should be able to see the message we typed earlier on in the index.php file.Congratulations you've configured your server successfully.

 

To configure a subdomain or another different domain on the server is sooooooo easy, just follow the same processes that you used to create a tzemall.com.conf all the way to the end and everything will work like charm.Let's create a subdomain store.tzemall.com together.As usual go to /etc/apache2/sites-available

 

Just make a copy of the file tzemall.com.conf since it's trimmed already.Edit the store.tzemall.com.conf file with nano using   nano store.tzemall.com.conf , edit the file accordingly.Mine is edited as follows

 

Save and exit the file.Create a /var/www/html/store.tzemall.com.conf directory by mkdir /var/www/html/store.tzemall.com , then save an index.php file and write some simple PHP script in it by nano /var/www/html/store.tzemall.com/index.php .Save and exit the file.

 

Now that you have everything good to go enable your subdomain then restart your apache like the way we did before.

If everything went well, when you browse store.tzemall.com or www.store.tzemall.com you should be able to see the following page.

So now you have both tzemall.com and store.tzemall.com working on the same server and every time you want to add another completely different domain or a subdomain of your current running domain on your server all you have to do is to follow the same procedures we did just now.

Enjoy running multiple domains on your server......