Select Page
Welcome to our Support Center
< All Topics

How To Install the Apache Web Server on Ubuntu 20.04

How To Install the Apache Web Server on Ubuntu 20.04?

 

Introduction

The Apache HTTP server is the world’s most widely-used web server, renowned for its powerful features, including dynamically loadable modules, robust media support, and extensive integration with other popular software.

In this guide, we will walk you through the process of installing an Apache web server on your Ubuntu 20.04 server.

Step 1 — Installing Apache

Apache is included in Ubuntu’s default software repositories, allowing for installation using standard package management tools.

First, update the local package index to ensure it reflects the latest upstream changes:

bash

$ sudo apt update

Next, install the apache2 package

bash

$ sudo apt install apache2

Managing Apache 2 service on Ubuntu

We will use the systemctl command to start and enable the Apache service.

bash

$ sudo systemctl enable apache2

Start the Apache Service, use the systemd init system by typing:

bash

$ sudo systemctl start apache2

The web server should already be up and running.

To confirm the service is running, use the systemd init system by typing:

bash

$ sudo systemctl status apache2

 

Output
● apache2.service – The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-07-19 06:32:23 UTC; 6h ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 3018563 (apache2)
Tasks: 12 (limit: 14213)
Memory: 33.0M
CPU: 2.544s
CGroup: /system.slice/apache2.service
├─3018563 /usr/sbin/apache2 -k start
├─3018566 /usr/sbin/apache2 -k start
├─3018569 /usr/sbin/apache2 -k start

 

Step 2 — Setting Up Virtual Hosts

Virtual hosts allow you to run multiple websites on a single server by configuring separate domain names or IP addresses. Here’s how to set up virtual hosts for Apache on your server:

We will configure a domain named your_domain_name, which you should replace with your actual domain name.

By default, Apache on Ubuntu 20.04 comes with a single server block enabled, which is configured to serve documents from the /var/www/html directory. 

Create the directory for your_domain_name as follows:

bash

$ sudo mkdir /var/www/your_domain_name

Then, assign ownership of the directory using the $USER environment variable:  init system by typing:

bash

$ sudo chown -R $USER:$USER /var/www/your_domain_name

If you haven’t changed your umask value, the permissions of your web roots should be correct by default. To verify and set the appropriate permissions, allowing the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can use the following command:

bash

$ sudo chmod -R 755 /var/www/your_domain_name

Next, create a sample index.html page using nano or your preferred text editor:  the files while granting only read and execute permissions to groups and others, you can use the following command:


$ sudo nano /var/www/your_domain_name/index.html

Add the following sample HTML inside the file:

 

<html>
<head>
<title>Welcome to Your_domain_name!</title>
</head>
<body>
<h1>Success! The your_domain_name Apache Setup is working!</h1>
</body>
</html>

 

Save and close the file once you are done.

To have Apache serve this content, you need to create a virtual host file with the appropriate directives. Rather than altering the default configuration file found at /etc/apache2/sites-available/000-default.conf create a new file at /etc/apache2/sites-available/your_domain_name.conf

 

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain_name
ServerAlias www.your_domain_name
DocumentRoot /var/www/your_domain_name
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

 

Save and close the file once you are done.

Let’s enable the file with the a2ensite tool:

bash

$ sudo a2ensite your_domain_name.conf

Disable the default site defined in 000-default.conf:

bash

$ sudo a2dissite 000-default.conf

Next, test for configuration errors:

bash

$ sudo apache2ctl configtest

You should see the following output:

 

Output
Syntax OK

 

Restart Apache to apply your changes:

bash

$ sudo systemctl restart apache2

Apache should now be serving your domain name. You can verify this by navigating to http://your_domain_name.com, where you should see something like this: