Apache: Virtual hosts
Table of contents
The term Virtual Host refers to the practice of running more than one web site (such as company1.example.com and company2.example.com) on a single machine.
Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.
Here is the final result:
In case you bought a domain, the way to go is as simple as repeating the following steps:
What do you think? Will you use it?
- Introduction
- Directories Creation
- Home page for each Virtual Host
- Apache2 Virtual Host creation
- Optional step
- Final Result
Introduction
The term Virtual Host refers to the practice of running more than one web site (such as company1.example.com and company2.example.com) on a single machine.
Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.
Directories Creation
First things first, we need two directories for the virtual hosts that we'll be creating.
The way to go is to use the mkdir -p [route/path]:
mkdir -p /var/www/sergichafer.com/public_html
mkdir -p /var/www/plusOne.com/public_html
Before getting into the following step, don't forget to change read permissions.
sudo chmod -R 755 /var/www
The way to go is to use the mkdir -p [route/path]:
-p will create the parents folders for the following folders in case they don't exist.
mkdir -p /var/www/sergichafer.com/public_html
mkdir -p /var/www/plusOne.com/public_html
sudo chmod -R 755 /var/www
Home page for each Virtual Host
Following the track of the tutorial, we'll need a homepage that presents each new virtual host so that we can test if our tutorial is successful.
Let's create those index.html inside each public_html foto:
nano /var/www/sergichafer.com/public_html/index.html
This is a tricky point, let's learn a bit about the Apache virtual hosts conf files:
Virtual hosts conf files are files that specify the actual configuration of a virtual host, meaning that they specify what apache must respond to a domain call.
Example:
If apache recieve a call for sergichafer.com, it will resolve that the index.html to return will be the one located insider /var/www/sergichafer.com/public_html/index.html.
Apache offers a default configuration file called 000-default.conf that can be found at /etc/apache2/sites-available; this will serve us as a base to start a new one:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/sergichafer.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/sergichafer.com.conf
This following created files will have to look like this(change my virtual host domain name for yours):
Domain configuration explanation:
This is a optional step, so it will only be performed in case the used domains are not on your property or are just examples(this step must be used just to test it worked).
Edit your /etc/hosts file and add the following lines marked in bold(change the IP and the domain for yours):
Let's create those index.html inside each public_html foto:
nano /var/www/sergichafer.com/public_html/index.html
Apache2 Virtual Host creation
This is a tricky point, let's learn a bit about the Apache virtual hosts conf files:
Virtual hosts conf files are files that specify the actual configuration of a virtual host, meaning that they specify what apache must respond to a domain call.
Example:
If apache recieve a call for sergichafer.com, it will resolve that the index.html to return will be the one located insider /var/www/sergichafer.com/public_html/index.html.
Apache offers a default configuration file called 000-default.conf that can be found at /etc/apache2/sites-available; this will serve us as a base to start a new one:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/sergichafer.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/sergichafer.com.conf
This following created files will have to look like this(change my virtual host domain name for yours):
Domain configuration explanation:
- ServerName: Equals the exact definition of our domain name.
- ServerAlias: Defines alternatives to this domain that apache2 will redirect to the ServerName specified domain.
As always with apache, it's time to activate the recent changes and restart the server.
sudo a2ensite sergichafer.com.conf
sudo a2ensite plusOne.com.conf
sudo systemctl reload apache2
Optional step(Without having an actual domain)
This is a optional step, so it will only be performed in case the used domains are not on your property or are just examples(this step must be used just to test it worked).
Edit your /etc/hosts file and add the following lines marked in bold(change the IP and the domain for yours):
127.0.0.1 localhost
127.0.1.1 guest-desktop
111.111.111.111 example.com
111.111.111.111 example2.com
Final result(Without having an actual domain)
Here is the final result:
Final result(Having actual domains)
In case you bought a domain, the way to go is as simple as repeating the following steps:
But, instead of using fake domains, use the real ones.
In my case, I bought chaferser.tk and sergislm4.tk through a website called freenom.
https://www.freenom.com/es/index.html
If you use this website, my actual configuration for the domains was the following one:
And of course, here it is the final result:
In my case, I bought chaferser.tk and sergislm4.tk through a website called freenom.
https://www.freenom.com/es/index.html
If you use this website, my actual configuration for the domains was the following one:
And of course, here it is the final result:
What do you think? Will you use it?
Thank you for reading my amateur tutorial and I hope we'll keep improving together!
Comentarios
Publicar un comentario