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 webmaster@demo.com
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.
How to install and set up Apache Virtual Hosts on Ubuntu?
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.