Apache HTTP Ubuntu

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

Apache Server is one of the most famous web servers. This server is open-source and works on various platforms on the Internet, powering many HTTP servers. Apache is a flexible tool and contains within it various other tools that extend its features and usage.

Install Apache Web Server

In this article, I will show you how to install the Apache web server on Ubuntu 20.04. This requires root or administrative privileges, so log in to the system via root.

Step 1: Upgrade Your APT

As always, first, update and upgrade your APT.

$ sudo apt update

$ sudo apt upgrade

Step 2: Download and Install Apache

Next, download and install the Apache web server from the Ubuntu software repository by executing the following terminal command.

$ sudo apt install apache2

Step 3: Verify Apache Installation

To verify whether Apache has installed, check the server status. When the installation is complete, the apache2 server will start automatically.

$ sudo systemctl status apache2

Step 4: Enable Firewall Settings

Now, you should enable the firewall settings for the Apache webserver. To do this, use the UFW command to allow Apache traffic on Port 443 and Port 80 via the following terminal command.

$ sudo ufw allow ‘Apache Full’

Step 5: Verify Changes

You can verify this change by checking the firewall status with the following terminal command.

$ sudo ufw status

Step 6: Check If Apache Is Working

Check to see whether Apache is working correctly on your system. Open a web browser on your Ubuntu machine, open a new tab, and type the following URL into the URL bar. Be sure to replace the IP we have used with your own machine’s IP address.

URL = http://10.0.2.15

Figure: Apache service running in a browser window.

Step 7: Set Up Virtual Host

Now, you are ready to set up a virtual host using the installed Apache web server. Apache contains a test virtual host that is enabled by default when it is installed. Hosting a single website is easy; you just have to upload the website content in its configuration file under the “/var/www/html.” The path to this configuration file is defined as follows.

Path= /etc/apache2/sites-enabled/000-default.conf

Figure: Default configuration file opened in gedit editor.

Step 8: Create Domain Name

If you are hosting multiple websites, use the following command to create a new virtual host configuration file for every new website. The domain name created in this test example is www.example.com; this can be replaced with your desired domain name.

$ sudo mkdir -p /var/www/example.com

Step 9: Create New Directory File

Create a new file in the directory named “index.html” and paste the following content into this file.

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! example.com home page!</h1>
</body>
</html>

Figure: New index.html file with site content inside.

Save the file and close it. Change the file permission options with the following terminal command.

$ sudo chown -R www-data: /var/www/example.com

Step 10: Create Document in Text Editor

Open your favorite text editor and create this file in “/etc/apache2/sites-available” location. I am using the gedit text editor.

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/example.com/public_html
 
<Directory /var/www/example.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
 
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Figure: example.conf file opened with gedit editor.

Step 11: Link Configuration File

Link this configuration file to the a2ensite utility by executing the following terminal command.

$ sudo a2ensite example.com

To check for a syntax error in the configuration file, execute the following command.

$ sudo apachectl configtest

Step 12: Restart Apache

Now restart Apache service, open your web browser, and type the following URL into the URL bar.

$ sudo systemctl restart apache2

=URL = http://example.com'

Figure: example.com opened in a browser window.

Uninstalling Apache Web Server

You can completely uninstall the Apache web server via the following terminal commands.

$ sudo apt-get purge apache2

$ sudo apt-get autoremove

Conclusion

This article covered how to install the Apache web server, configure firewall settings for Apache, set up virtual hosts on Apache web server, and uninstall Apache.

About the author

Avatar

Younis Said

I am a freelancing software project developer, a software engineering graduate and a content writer. I love working with Linux and open-source software.