Last updated on September 10th, 2024 at 06:00 am
Virtual hosts allow you to run multiple websites on a single Apache web server. You can also use it to run multiple subdomains on a single server. Once you have set up virtual hosts in Apache, then depending on the requested URL, Apache will automatically direct visitors to appropriate domain or subdomain. In this article, we will look at how to configure virtual hosts in Apache for Ubuntu, Debian Linux.
How To Set Up Apache Virtual Hosts on Ubuntu
Here are the steps to set up Apache Virtual Hosts on Ubuntu, Debian Linux.
1. Install Apache Server
Open terminal and run the following commands to install Apache web server.
$ sudo apt-get update $ sudo apt-get install apache2
2. Create Directory Structure
Let us say we want to host two domains example1.com and example2.com on our web server. We will create separate directories for each of these domains under Apache document root /var/www. We will also create public_html subfolders in each of these directories to store all necessary files for each of these websites.
Run the following commands to create two directories
$ sudo mkdir -p /var/www/example1.com/public_html $ sudo mkdir -p /var/www/example2.com/public_html
Also read : How to Create Self-Signed Certificate for Apache
3. Change File Permissions
When we create these two directories, they are owned by root user by default. We will need to change their file ownership to make them usable by Apache web server.
Run the following commands to change file ownership of the two directories.
$ sudo chown -R $USER:$USER /var/www/example1.com/public_html $ sudo chown -R $USER:$USER /var/www/example2.com/public_html
In the above code, we use $USER to grant permission to regular non-root user to access these directories.
We also need to change file permissions of Document Root folder to ensure that all files and pages are served properly.
$ sudo chmod -R 755 /var/www
Also read : How to Use Apache Bench for Load Testing
4. Create Index pages
Next we will create index.html pages for each of our website, for demo purposes.
$ sudo vi /var/www/example1.com/public_html/index.html
Copy paste the following HTML in it.
<html> <head> <title>Welcome to Example1.com!</title> </head> <body> <h1>Success! The example1.com virtual host is working!</h1> </body> </html>
Similarly, create index.html file for example2.com
$ sudo vi /var/www/example2.com/public_html/index.html
Copy paste the following HTML in it.
<html> <head> <title>Welcome to Example2.com!</title> </head> <body> <h1>Success! The example2.com virtual host is working!</h1> </body> </html>
Also read : How to Install Fail2ban in Apache
5. Create Virtual Hosts File
Apache provides a default virtual host file, which can be used as a template to create our virtual host files for the two domains. We will simply copy and and modify this file for our purpose.
Apache virtual host configuration file location is
/etc/apache2/sites-available/000-default.conf
Run the following command to create a copy of this file for example1.com
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example1.conf
Open this file using a text editor.
$ sudo vi /etc/apache2/sites-available/example1.conf
It will look something like
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Modify the DocumentRoot directive and point it to the location of document root for example1.com domain. We will also add ServerName directive as our domain name. This will be used by Apache to match domain name of incoming requests and direct them to appropriate virtual host. We will also add ServerAlias directive to match any variations of the domain. You can also change ServerAdmin email address as per your requirement, if you want.
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName example1.com ServerAlias www.example1.com DocumentRoot /var/www/example1.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Similarly, repeat the above steps for example2.com domain.
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example2.conf $ sudo vi /etc/apache2/sites-available/example2.conf
Edit the Document Root value of this file to location of document root for example2.com domain.
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName example2.com ServerAlias www.example2.com DocumentRoot /var/www/example2.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Also read : How to Install memcached in Apache
6. Enable Virtual Hosts
Run the following commands to enable the two virtual hosts
$ sudo a2ensite example1.conf $ sudo a2ensite example2.conf
Disable default virtual host
$ sudo a2dissite 000-default.conf
Also read : How to Choose a Web Hosting Service
7. Restart Apache Web Server
Restart Apache Web Server to apply changes
$ sudo systemctl restart apache2
OR
$ sudo service apache2 restart
Also read : How to Fix Too Many Redirects Error in Website
8. Test Virtual Hosts
Open browser and visit www.example1.com. You will see index page for example1.com.
Similarly, open browser and visit www.example2.com. You will see index page for example2.com.
Hopefully, this article will help you set up virtual hosts for Apache in Ubuntu. Similarly, you can also setup virtual hosts in NGINX.
Ubiq makes it easy to visualize data, and monitor them in real-time dashboards. Try Ubiq for free.
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.