How to install and set up Apache Virtual Hosts on Ubuntu?
Last Updated :
02 Aug, 2022
Every website that is published on the Internet is housed on a web server (host), which is able to handle requests for web pages made by clients using browsers like Chrome, Firefox, or Internet Explorer and is connected to the network with a public IP address. Install a web server before hosting a website on your computer. One of the most well-known options is Apache, a free application that runs on both Windows and Unix platforms. When several domains are hosted on the same server, it is referred to as "virtual hosting." A Virtual Host is an Apache configuration directive that enables you to run several websites on a single server on Linux-based systems like Ubuntu 22.04 LTS. Types of Virtual Hosting are been described below:
- IP-Based Virtual Hosting: Each website on the Apache Server has its own distinct IP address.
- Name-Based Virtual Hosts: This allows you to add several domains with just one IP address.
Installation of Apache2 on Ubuntu
Step 1: Update the repositories by using the below command.
sudo apt update
Step 2: Install the apache2 package by using the apt package manager. Execute the below command in the terminal.
sudo apt-get install apache2
Set Up Apache Virtual Hosts on Ubuntu
Step 1: Check apache2 service status. Verify the status of the apache2 service by executing the below command.
systemctl status apache2
This command will show whether the apache2 service is active on our system:
For the specified verification, we can go to the respective browser and check what the "localhost" web page beholds for:
Step 2: Setup Apache Virtual Host on ubuntu 22.04
Create a directory that will be used to store data for the website before setting a virtual host in Apache. We will use the "cd" command to navigate to the "/var/www" directory for this reason.
$sudo mkdir -p /var/www/demo.com/public_html
To modify who owns the "demo.com" directory, use the "chown" command:
$ sudo chown -R www-data:www-data /var/www/demo.com
Step 3: Creating a web page
We will engage the "nano" editor to produce an example "index.html" web page for our website:
$ sudo nano /var/www/demo.com/index.html
In the file that has been opened, type the following code:
index.html
HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h2>Welcome To GFG</h2>
<p>Default code has been loaded into the Editor.</p>
</body>
</html>
To save the file after inserting the code, press "Ctrl+O":
Step 4: Creating an Apache Virtual Host File
We have now updated the ownership of our domain and built a directory for it. We will now make a virtual host file in the Apache host files default directory:
sudo nano /etc/apache2/sites-available/demo.com.conf
Add the following lines of code to the virtual host file that has been opened. Additionally, you must change the data for "ServerName," "ServerAlias," and "DocumentRoot" in accordance with your settings:
<VirtualHost *:80>
ServerName demo.com
ServerAlias www.demo.com
ServerAdmin [email protected]
DocumentRoot /var/www/demo.com/public_html
<Directory /var/www/demo.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/demo.com-error.log
CustomLog ${APACHE_LOG_DIR}/demo.com-access.log combined
</VirtualHost>
To save the modified code in the virtual host configuration file, press "Ctrl+O":
Step 5: Enable the Virtual Host file
To enable the newly created virtual host file, use the "a2ensite" command as follows:
$ sudo a2ensite demo.com.conf
Then disable the default configuration file:
$ sudo a2dissite 000-default.conf
After completing the action given. The "apache2" service needs to be restarted on your Ubuntu 22.04 LTS system.
$ sudo systemctl restart apache2
Step 6: Error testing
Check for configuration errors in the final phase of Apache2 configuration:
$ sudo apache2ctl configtest
Executing the mentioned command will notify you if your configuration file is error-free and if the syntax is "OK":
On our Ubuntu 22.04 LTS system, restart the "apache2" service after that:
$ sudo systemctl restart apache2
Step 7: Apache Virtual Host Testing
Navigate to the chosen domain to check your virtual host. The domain in this instance is "demo.com":
The information shown demonstrates that our Apache Virtual Host is operational and flawlessly running on an Ubuntu 22.04 LTS machine.
Similar Reads
How to Install Apache with PHP-FPM on Ubuntu? The Apache HTTP Server is a free, open-source, cross-platform web server software. It is developed and maintained by Apache Software Foundation. Apache is the most widely used web server around the world. The vast majority of Apache HTTP server instances run on Linux distribution, but current versio
4 min read
How to Install and Customize Apache on Ubuntu or Debian? One of the most dominant forces in web server software is the Apache HTTP Server, often shortened to Apache. This free and open-source project, overseen by the Apache Software Foundation, has been a major player in shaping the Internet since its launch in 1995. Installing an Apache web server on Ubu
2 min read
How To Install apache2-dev on Ubuntu The Apache HTTP Server Project's purpose is to provide a standards-compliant open-source HTTP server that is secure, efficient, and extensible. As a result, it has long been the most used web server on the Internet. This package contains development headers and the apxs2 binary for the Apache 2 HTTP
3 min read
How to Install Apache in Ubuntu using Ansible? Apache HTTP Server, commonly referred to as Apache, is a robust and broadly used web server for open-source web server programming. It is exceptionally adaptable and extensible, and it is famous for hosting websites and web applications. Ansible, then again, is a strong automation device that improv
6 min read
How to Setup Virtual Hosts with Apache Web Server on Linux? Setting up a virtual host in Apache web server on Linux allows us to host multiple websites on a single server. This virtual host is useful when one wants to host multiple projects for businesses managing multiple domains with a single server.PrerequisitesApache server installed.Root or sudo accessS
2 min read