How to Install the Apache Web Server on Ubuntu 20.04

What is Web Server?

A web server is a software application or a computer system that serves web content over the internet. It handles requests from clients, such as web browsers, and delivers the requested web pages, files, or other resources to the client. When a user enters a URL (Uniform Resource Locator) into a web browser, the browser sends an HTTP (Hypertext Transfer Protocol) request to the web server associated with that URL. The web server processes the request and sends back an HTTP response containing the requested content, which is then rendered and displayed by the web browser.

Commonly used web server software includes Apache HTTP Server (Apache), Nginx, Microsoft Internet Information Services (IIS), and LiteSpeed. Each web server has its own features, performance characteristics, and configuration options.

What is Apache Web Server?

The Apache HTTP Server, commonly referred to as Apache, is one of the most popular and widely used web servers in the world. Apache is known for its stability, performance, and flexibility, and it runs on various operating systems, including Linux, Unix, Windows, and macOS. It supports multiple protocols, including HTTP, HTTPS, FTP, and more.

In this guide, you’ll learn how to install an Apache web server on your Ubuntu 20.04 server.

How to Install the Apache Web Server on Ubuntu 20.04

Step 1. Install Apache

Open a terminal or SSH into your Ubuntu 20.04 server and using the following command the switch to the root user so you have the permission for later operations. Then, input password as prompted.

$ sudo -i
Switch to root user using sudo -i

Next, update the package repositories to ensure you have the latest package information.

$ apt update
update the package repositories

Once the package repositories are updated, you can install the Apache web server by running the following command:

$ apt install apache2

And type y to confirm the installation.

Install Apache

Step 2. Adjust Firewall

If you have a firewall enabled, such as UFW, you need to allow incoming HTTP (port 80) and HTTPS (port 443) traffic. Run the following commands to allow HTTP and HTTPS traffic:

$ ufw allow 'Apache'

Then, make sure the service is active by running the command:

$ systemctl status apache2
Apache status

From the above screenshot, we can see the Apache service is running. You can also verify that Apache is installed and running correctly by requesting a page from Apache. Open a web browser and enter your server's IP address or domain name. If you do not know your server’s IP address, try the command below. If Apache is working correctly, you should see the Apache Ubuntu Default Page.

$ hostname -I

If Apache is working correctly, you should see the Apache Ubuntu Default Page as below.

default Apache2 Ubuntu Default Page.

Step 3. Set Up Virtual Hosts (Recommended)

A virtual host, also known as a virtual server, is a configuration in a web server that allows multiple websites or web applications to be hosted on a single physical server. Each virtual host has its own domain name or IP address and operates as if it is a separate server, even though it shares the underlying hardware resources with other virtual hosts. We will set up a domain called your_domain, but you should replace this with your own domain name.

Apache on Ubuntu 20.04 has one virtual host enabled by default that is configured to serve documents from the /var/www/html directory. We can create a new directory within /var/www for a your_domain site. Replace your_domain with the actual domain name of your site. In this case, we use mytestsite.com. Then, assign ownership of the directory to the user you’re currently signed in and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others

$ mkdir /var/www/your_domain 
$ chown -R $USER:$USER /var/www/your_domain
$ chmod -R 755 /var/www/your_domain
Create a new directory

Next, create a sample index.html page using your favorite editor. Here, we use nano editor.

$ nano /var/www/your_domain/index.html
Apache status

Then create a sample HTML as below.

Apache status

Then, save the file by pressing CTRL + X, then Y and ENTER.

Next, create a virtual host configuration file for your domain. Use the following command to create and open the file in a text editor:

$ nano /etc/apache2/sites-available/your_domain.conf
default Apache2 Ubuntu Default Page.

In the configuration file, add the following content, replacing your_domain with your actual domain name. Then, save the file by pressing CTRL + X, then Y and ENTER.

default Apache2 Ubuntu Default Page.

Now enable the file with the a2ensite tool and disable the default site defined in 000-default.conf:

$ a2ensite your_domain.conf
$ a2dissite 000-default.conf
Enable new site
Disable the default site

After that, we can test for configuration errors with the command below. And you should receive the "Syntax OK" output as the screenshot shows.

$ apache2ctl configtest
Enable new site

Finally, restart Apache so that the changes take effect.

$ systemctl restart apache2
Restart Apache

When Apache is restarted, it should now be serving your domain name. You can test this by navigating to http://your_domain, and you should see the content you configured for your index page.

Verify successful virtual host setup

Manage Apache Process

Here are some basic management commands that is commonly used.

Run the commands to stop, start, and restart your web server respectively.
$ systemctl stop apache2
$ systemctl start apache2
$ systemctl restart apache2
If you are simply making configuration changes, Apache can often reload without dropping connections with the reload command:
$ systemctl restart apache2
By default, Apache is configured to start automatically when the server starts. If this is not what you want, disable this behavior
$ systemctl disable apache2
You can always enable the behavior using the command.
$ systemctl enable apache2

Conclusion

Installing the Apache web server on Ubuntu 20.04 is a straightforward process that allows you to host websites and applications on your server. By following the step-by-step instructions, you can easily set up Apache and get started with serving your content to the world. With Apache up and running, you're well on your way to hosting multiple sites, providing services, and sharing your online presence with others.