The term “Virtual Hosting” refers to the hosting of many domains on a single server. In Linux-based systems such as Ubuntu 22.04, a Virtual Host is a configuration directive in Apache that permits you to operate several websites on a single server. This write-up will discuss the method to Set Up Apache Virtual Hosts on Ubuntu 22.04. So, let’s start!
Step 1: Check apache2 service status
Before setting up Virtual Hosts, it is important to check if Apache is working or not. For this purpose, we will verify the status of the “apache2” service:
systemctl status apache2
The below-given shows that the “apache2” service is active on our system:
Step 2: Set up Apache Virtual Host on Ubuntu 22.04
To set a virtual host in Apache, create a directory that will be utilized to store data on the website. For this purpose, we will move to
the “/var/www” directory using the following “cd” command:
cd /var/www/
Then, we will create a directory for our domain “ubuntuserver.local”. Here, you can specify your domain name in the below-given command:
sudo mkdir -p /var/www/ubuntuserver.local/
Utilize the “chown” command for changing the ownership of the “ubuntuserver.local” directory:
sudo chown -R www-data:www-data /var/www/ubuntuserver.local
Step 3: Creating a web page
To create a sample “index.html” web page for our website, we will use the “nano” editor:
sudo nano /var/www/ubuntuserver.local/index.html
Write out the following code in the opened file:
<html> <head> <title>Welcome to thetimes.ro</title> </head> <body> <h1>Success! This is my first virtual host configuration!</h1> </body> </html>
After adding the code, press “Ctrl+O” to save the file:
Step 4: Creating an Apache Virtual Host file
At this point, we have created a directory for our domain and updated its ownership. Now, we will create a virtual host file under the default directory of Apache host files:
sudo nano /etc/apache2/sites-available/ubuntuserver.local.conf
In the opened virtual host file, add the following lines of code. Also, you have to replace the information related to “ServerName”, “ServerAlias”, and “DocumentRoot” according to your settings:
<VirtualHost *:80> ServerAdmin danychrys@danychrys.ro ServerName ubuntuserver.local ServerAlias www.ubuntuserver.local DocumentRoot /var/www/ubuntuserver.local ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Press “Ctrl+O” to save the added code of the virtual host configuration file:
Step 5: Enable Virtual Host file
Execute the following “a2ensite” command for enabling the created virtual host file:
sudo a2ensite ubuntuserver.local.conf
Then disable the default configuration file:
sudo a2dissite 000-default.conf
After performing the specified operation. Restart the “apache” service on your Ubuntu 22.04 system:
sudo systemctl restart apache2
Step 6: Error testing
In the last step of Apache2 configuration, test the configuration errors:
sudo apache2ctl configtest
In case of having an error-free configuration file, the execution of the above-given command will let you know that the Syntax is “OK”:
Then, restart the “apache2” service on your Ubuntu 22.04 system:
sudo systemctl restart apache2
Step 7: Apache Virtual Host testing
Finally, test your Virtual host by navigating to the specified domain. In our case, the domain is “ubuntuserver.local”:
The displayed information justifies that our Apache Virtual Host is up and working perfectly on Ubuntu 22.04 system.