So what are Virtual Hosts and what are they for?
John has a web server, connected to the internet with a single IP address. He has a website, called JohnBlog.com which is pointing to this web server. He creates a blog site, everything works fine, he is happy. Suddenly he realizes he needs another website for his new website, ExoticFoods.com.
He does not want to buy another webserver, a new public IP, etc. So what can he do?
Luckily this is where virtual hosts can help. When a client computer loads a website, it looks up the iP address of the domain first, and then sends a query to the webserver with the requested domain name. That means John’s existing webserver even though has only one single IP address, John can have as many websites hosted on his server as long as he sets up virtual hosts for each domain. Technically saying, he tells the webserver if someone queries JohnBlog.com, then load the files form /var/www/JohnBlog.com, if ExoticFoos.com is requested load the /var/www/ExoticFoods.com files, and so on.
Let’s see how it is configured.
Before You Begin
We need a working CentOS box as a start. You can find a detailed guide of setting one up HERE, although we don’t need to have a full LAMP stack. The bare minimum we need is Apache installed and running on the server, and open the firewall of course to allow the web traffic in.
# Install apache
sudo yum install httpd
# Start apache at boot automatically
sudo systemctl start httpd
# Start apache now
sudo systemctl enable httpd
# Allow the http and https incoming ports on the firewall
firewall-cmd --zone=public --permanent --add-port=80/tcp
firewall-cmd --zone=public --permanent --add-port=443/tcp
firewall-cmd --reload
1. Set up Folders for the website files
We need to create folders where the website files can be stored. By default they are stored in the /var/www/html directory, we create two new folders, and change the owner to the apache user so the apache service will have full access to the files.
# Create the folders for the two sites
sudo mkdir -p /var/www/JohnBlog.com
sudo mkdir -p /var/www/ExoticFoods.com
# Create a simple webpage to display in both:
sudo sh -c "echo John\'s website! > /var/www/JohnBlog.com/index.html"
sudo sh -c "echo Welcome to ExoticFoods! > /var/www/ExoticFoods.com/index.html"
# Change owner of the website directories to the apache service user
sudo chown apache:apache -R /var/www
2. Virtual Hosts config directories
We will need to create separate config files for both websites which are stored in the /etc/httpd/conf.d directory. All configuration files in that directory will be imported by apache at startup time, as configured in /etc/httpd/conf/httpd.conf by default
3. Create the Virtual Hosts config files for the webserver
In the /etc/httpd/conf.d directory we create a separate config file for both JohnBlog.com and ExoticFoods.com as follows:
sudo vi /etc/httpd/conf.d/JohnBlog.com.conf
<VirtualHost *:80>
ServerName JohnBlog.com
ServerAlias www.JohnBlog.com
DocumentRoot /var/www/JohnBlog.com
</VirtualHost>
sudo vi /etc/httpd/conf.d/ExoticFoods.com.conf
<VirtualHost *:80>
ServerName ExoticFoods.com
ServerAlias www.ExoticFoods.com
DocumentRoot /var/www/ExoticFoods.com
</VirtualHost>
4. Restart the Apache Service and Ready to Go!
sudo systemctl restart httpd
Let’s browse to the sites 🙂
Comments