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: .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 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 we should have typed 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 ( in their browsers, but at times users would type . 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 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

NOTE: Don't forget a dot at the end of hostname or

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 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 .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 then your .conf file MUST be .We'll create the store subdomain later in this tutorial.Now use nano to edit the file by using the command sudo nano .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 or without www. Add ServerAlias   or .CHange DocumentRoot to /var/www/html/ . We haven't created the directory 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 and into the directory /var/www/html/ .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 that we'll put all out files and when we add other domains or subdomains we can put their files in their specific directories such as directory could be used for holding the 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 directory, mkdir /var/www/html/ a simple index.php file in it by nano /var/www/html/ 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 


Restart apache by service apache2 reload

When you go to or 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 all the way to the end and everything will work like charm.Let's create a subdomain together.As usual go to /etc/apache2/sites-available


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


Save and exit the file.Create a /var/www/html/ directory by mkdir /var/www/html/ , then save an index.php file and write some simple PHP script in it by nano /var/www/html/ .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 or you should be able to see the following page.

So now you have both and 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......