Setting Up Virtual Hosts For Web Servers

In our last article, we installed LAMP stack with basic configurations on our server.

So we now have Apache2 + PHP + MariaDB services installed and configured

If you need to place just a single website on this server then all you need to do is upload its files and directories to the /var/www/html directory at the server. But what if you need to host more than just one site? Use a dedicated server for each one?! Nah, forget it! What we're going to do instead is make use of Virtual Hosts.

It’s actually a good idea to use VirtualHost even if you're hosting just one website at the moment, since you never really know when you might need to expand. As you might have guessed, Virtual Hosts is a way to host more than one website on the same server. We’ll use a name-based VirtualHost for our example in this article, which basically means that we'll be using one IP address for multiple sites. So we 're going to add a two VirtualHosts to our server: let's call ithem ‘ServerSuitTheBest.com’ and ‘iLoveServerSuit.com’.

Let’s allow using name-based VirtualHosts, and create some folders with test index files:

[root@ServerSuit]# echo NameVirtualHost *:80 >> \ /etc/httpd/conf/httpd.conf [root@ServerSuit]# mkdir /var/www/ServerSuitTheBest [root@ServerSuit]# mkdir /var/www/iLoveServerSuit [root@ServerSuit]# echo '<?php echo "ServerSuit The Best!"; ?>' > \ /var/www/ServerSuitTheBest/index.php [root@ServerSuit]# echo '<?php echo "I love ServerSuit!"; ?>' > \ /var/www/iLoveServerSuit/index.php

Now we'll add a new file to the /etc/httpd/conf.d/ directory, and name it ‘virtualhost.conf’:

[root@ServerSuit conf]# cat /etc/httpd/conf.d/virtualhosts.conf <VirtualHost *:80> ServerName ServerSuitTheBest.com ServerAlias www.ServerSuitTheBest.com DocumentRoot /var/www/ServerSuitTheBest </VirtualHost> <VirtualHost *:80> ServerName iLoveServerSuit.com ServerAlias www.iLoveServerSuit.com DocumentRoot /var/www/iLoveServerSuit </VirtualHost>

There are a lot more directives you can use in the VirtualHost block that allow an administrator to provide separated log files for a website, different admin contact email etc., but we'll use this for the sake of simplicity.

Now all we need is restart the Apache daemon to implement our config changes:

[root@ServerSuit]# service httpd restart

To check how it works you’ll need to open your hosts file in Windows (%Windir%\System32\drivers\etc\hosts) and add a few lines to it (local administrator rights required):

<your server IP> ServerSuitTheBest.com <your server IP> iLoveServerSuit.com

Finally, when you open your newly added website in a browser you should see something like this:

That's just a sample one-line PHP file we added earlier, but you get the idea. You can add a new website on the server and upload its contents he same way. It’s actually not as complicated as setting up a LAMP stack.

Don't forget that at ServerSuit, you just need one click!

When you install LAMP or LEMP package on your server, you can add a new domain just by putting in its name and clicking the ‘Add’ button! That’s it! All other steps will be performed by ServerSuit: apache2 or nginx VirtualHosts will be configured and added on affordable VPS without having to... do much of anything! Give us a try!

Remember to follow us @serversuit to stay updated on new articles and product releases. See you next time!