This brief tutorial shows students and new users how to create multiple Apache2 Virtual Hosts. Virtual Hosting allows multiple independent websites or domains to be managed on a single Apache2 server. If the hardware Apache2 is running on can support multiple websites, Virtual Hosting can be used to run them.
Each site runs independently and doesn’t know the others running on the same Apache2 server. This is relevant for those wanting to run multiple websites and blogs without spending too much on hardware resources.
Apache2 virtual hosting is a reliable way to run multiple domains simultaneously on one server and this post shows you how to do it easily.
To get stated with Apache2 virtual hosting, continue with the steps below:
Step 1: Install Apache2
The first step to get virtual hosting configured on Apache2 is to install Apache2. If you don’t have Apache2 installed, you’re not going anywhere. To install Apache2, run the commands below.
sudo apt-get update sudo apt-get install apache2
Step 2: Create Virtual Hosting Configuration Files
Now that Apahce2 is installed, all your virtual host configurations file will live in the directory below. Each file will end in .conf.
Run each of the line below to create new virtual host configuration file.
sudo nano /etc/apache2/sites-available/VirtualHost1.conf sudo nano /etc/apache2/sites-available/VirtualHost2.conf sudo nano /etc/apache2/sites-available/VirtualHost3.conf
The three lines above create three virtual host configuration files named VirtualHost1.conf, VirtualHost2.conf and VirtualHost3.conf. Each of the configuration file will be used to run one domain or website.
Step 3: Configure VirtualHost Files
Now that your created three virtual host files, you can copy and paste the codes below into each file and save. Each configuration file will have information on the website and domain it controls.. so there should be three separate configurations.
Copy the block and paste the block of code below in each file corresponding with each website or domain.
<VirtualHost *:80> ServerName www.virtualhost1.com ServerAdmin firstname.lastname@example.org DocumentRoot /var/www/html/virtualhost1.com ErrorLog /var/log/apache2/virtual.host.error.log CustomLog /var/log/apache2/virtual.host.access.log combined LogLevel warn </VirtualHost>
Copy the above code and paste each configuration file, replacing the highlighted lines to match each domain or website DocumentRoot and ServerName.
Repeat the same for VirtualHost 2.
<VirtualHost *:80> ServerName www.virtualhost2.com ServerAdmin email@example.com DocumentRoot /var/www/html/virtualhost2.com ErrorLog /var/log/apache2/virtual.host.error.log CustomLog /var/log/apache2/virtual.host.access.log combined LogLevel warn </VirtualHost>
and so forth.
Now you must create three DocumentRoot directories for the three websites location defined on the DocumentRoot line.
sudo mkdir -p /var/www/html/virtualhost1.com sudo mkdir -p /var/www/html/virtualhost2.com sudo mkdir -p /var/www/html/virtualhost3.com
Now all you have to do is create a index.html file in each and test the sites.
Step 4: Enable Each Site
Now that you’ve create each virtual host configuration file, you must enable each. Run the commands below for each of the virtual host you created.
sudo a2ensite VirtualHost1.conf sudo a2ensite VirtualHost2.conf sudo a2ensite VirtualHost3.conf
Step 5: Restart Apache2
Finally, run the commands below to restart Apache2 webserver.
sudo systemctl restart apache2
Test each site after creating the index.html file in each root directory. Then browse the domain name used in the virtual host configuration files.
You may also like the post below: