Apache HTTP

How to Install Apache Server and Set Up Virtual Hosts on Ubuntu 22.04

Apache web server is a free and open source cross-platform web server tool. It is fast, secure, reliable and highly customizable to meet the requirements of the users. It’s one of the most secure, efficient and widely adopted web servers that provides HTTP services to its users.

This article will guide you how you can install Apache server and setup Virtual Hosts on Ubuntu 22.04.

How to Install and Configure Apache Server on Ubuntu 22.04

In order to install Apache server on Ubuntu server on Ubuntu 22.04, you will need to perform the below mentioned steps:

Step 1: First, update the apt packages using the following command:

$ sudo apt update


Then run the following command to install the packages if required:

$ sudo apt upgrade


Step 2: Now, install Apache server using the following installation command in the terminal.

$ sudo apt install apache2


Step 3: To verify the status of Apache on Ubuntu 22.04, you can use the following command.

$ sudo systemctl status apache2


Step 4: After running the Apache on Ubuntu 22.04, you will be required to enable the Firewall setting for Apache server and to do this first switch to the root user:

$ sudo su

And then allow access:

# sudo ufw allow ‘Apache Full’


Step 5: You can also verify the firewall changes using the following command:

$ sudo ufw status


Step 6: Next, you have to check whether your Apache is working and for that go to your browser tab and enter the host address of your Ubuntu.

You can find the host address using the following command :

$ hostname -I


This confirms that your Apache service is running fine.

How to Setup a Virtual Hosts on Ubuntu 22.04

Now, you will need to set up a virtual host on Ubuntu 22.04 using the Apache server. By default, the Apache includes a test virtual host whenever you install it on your system. If you want to upload your content you will need to open the Apache configuration file which is placed in the “/var/www/html” directory. This works well for a single site but it won’t be a good option for someone who wants to develop multiple websites.

To enable the feature of creating multiple websites, you will be required to create a directory in the “/var/www” folder with your domain name which you have to set according to your choice. The syntax will look like this as shown below.

$ sudo mkdir -p /var/www/domain_name/public_html


In the above screenshot, “mawaixweb.com” is the domain name which you will have to choose by yourself.

Note: In the following section the domain name “mawaixweb.com” will be used. Change it according to your configuration.

Now, create an index.html page inside your domain using the following command.

$ sudo nano /var/www/[domain_name]/public_html/index.html

In out case the “[domain_name]” is “mawaixweb.com”, so command will be:

$ sudo nano /var/www/mawaixweb.com/public_html/index.html

Paste the following script into the “index.html” file.

<!DOCTYPE html>

<html lang="en" dir="ltr">

<head>

<meta charset="utf-8">

<title>Welcome to [domain_name]</title>

</head>

<body>

<h1>Success! [domain_name] home page!</h1>

</body>

</html>


You can alter the code according to your preferences. Now, use Ctrl+X and press Y to save the file and then press enter to go back to the command-line.

Now provide ownership to the directory using the following command.

$ sudo chown -R www-data: /var/www/[domain_name]


Now, to create a virtual host file, open your domain configuration file using the following command:

$ sudo nano /etc/apache2/sites-available/[domain_name].conf

Then paste the below text and change the “domain_name” accordingly:

<VirtualHost *:80>

ServerName [domain_name]

ServerAlias [www.domain_name]

ServerAdmin webmaster@[domain_name]

DocumentRoot /var/www/[domain_name]/public_html

 

<Directory /var/www/[domain_name]/public_html>

Options -Indexes +FollowSymLinks

AllowOverride All

</Directory>

 

ErrorLog ${APACHE_LOG_DIR}/domain_name-error.log

CustomLog ${APACHE_LOG_DIR}/domain_name-access.log combined

</VirtualHost>


Save the file and close it.

Now, enable the file using a2ensite tool using the following command.

$ sudo a2ensite [domain_name]


Disable the default site “000-default.conf” using the below-given command:

$ sudo a2dissite 000-default.conf


Once the above steps are finished restart or reload the apache service using the following command.

$ sudo systemctl restart apache2


Now, go to your browser and type the address ”http://domain_name”.


That’s it,  Virtual host is up and ready on Ubuntu 22.04. 

Conclusion

Apache web server is an open-source cross platform that allows users to develop different websites with ease using the secure and reliable web server system. The above guide will help you in setting up an Apache Server on Ubuntu 22.04 and once it’s completed, you can then easily set up a Virtual Host that includes your personal website link.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.