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.
/etc/apache2/sites-available/
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.
ServerName www.virtualhost1.com ServerAdmin [email protected] 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
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.
ServerName www.virtualhost2.com ServerAdmin [email protected] 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
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.
That’s it!
You may also like the post below:
Related posts:
- Students Tutorial – Allow WordPress Permalinks on Nginx or Apache2
- Redirect HTTP to HTTPS using Nginx or Apache2 on Ubuntu 17.04
- Apache2 301 Redirect Without htaccess File on Ubuntu 17.04
- Setup Varnish and Apache2 on Ubuntu 17.04 / 17.10
- Install WordPress on Ubuntu 17.04 / 17.10 with Apache2 HTTP/2 and Let’s Encrypt SSL
- Running Multiple WordPress Sites on Ubuntu 16.04 LTS with Apache2, MariaDB and PHP 7.1 Support
- Install Ghost CMS on Ubuntu 16.04 / 17.10 / 18.04 with MariaDB and Apache2 Proxy
- Install the Latest Apache2 HTTP Server ( 2.4.34 ) on Ubuntu 16.04 / 17.10 / 18.04 LTS Servers
- Apache2 HTTP Server ( 2.4.35 ) Released — Here’s How to Install / Upgrade on Ubuntu 16.04 / 18.04 LTS