Apache HTTP

Install and Configure Apache Web Server on Ubuntu 20.04

Apache web server is the most extensively used open-source web server supported on the majority of the OS including Linux, Windows, MacOS, Solaris, etc. It is highly customizable and can be integrated with other modules. Installing and configuring Apache for basic setup is quite easy. This article will explain how to install and configure the Apache web server on Ubuntu operating system.

Note: We have explained the procedure explained in this article on a Ubuntu 20.04 LTS system.

Installing Apache2; Step 1: Update

First, we will need to update the system repository index to install the most recent version of Apache2. To do so, launch the Terminal by using the Ctrl+Alt+T shortcut and execute the following command:

$ sudo apt update

Note that, only an authorized user can install, update, or remove the packages from the Linux system.

Step 2: Install Apache2

Next in this step, we will install Apache2 web server. For this, execute the below command in Terminal:

$ sudo apt install apache2

The system might ask for confirmation by providing you with a Y/n option. Hit y and then Enter to continue. After that, the Apache2 web server and its all dependencies will be installed on your system.

Once installed, verify the version of the Apache server as follows:

$ apache2 -version

Firewall configuration

Now, we will need to open certain ports on our system in order to access Apache from outside. First, let’s list the application profiles that we need to give Apache access to. Run the following command to do so:

$ sudo ufw app list

Here you can see different apache profiles.

We will use the highly restrictive profile ‘Apache’ to enable network ctivity on port 80.

$ sudo ufw allow ‘Apache’

Now check the status which will show Apache allowed in firewall.

$ sudo ufw status

Configuring Apache web server; Verifying Apache service

Before moving towards configuration, first, verify if the Apache service is operational. For this, execute the below command in Terminal:

$ sudo systemctl status apache2

From the above output, you can see the Apache2 service is active and running.

Another approach to verify if Apache is running fine by requesting a web page from the Apache web server. To do so, find your IP address using the following command:

$ hostname –I

Then open the web browser and access apache welcome page as follows:

http://192.168.72.134

Replace the 192.168.72.134 by the IP address of your machine.

By navigating to the above link in the browser, you see the Apache welcome page which is the indication that the Apache server is working properly.

Setting Up Virtual Hosts in Apache

If you have multiple domains that need to be server from the single Apache web server, then you will require to set up virtual hosts. In the following, we will show you how to set up a virtual host in Apache. We will set up the domain name “info.net”. Make sure to replace the info.ne with your own domain name.

Step 1: Create a directory for your domain

In this step, we will create a directory for our domain name. This directory will be used for storing the data on our website.

Run the following command in Terminal by replacing the info.net with your own domain name:

$ sudo mkdir -p /var/www/info.net/html

Change the directory ownership to current user:

$ sudo chown -R $USER:$USER /var/www/info.net/html

Assign necessary permissions as follows:

$ sudo chmod -R 755 /var/www/info.net

Step 2: Make a sample page for your website

We have setup virtual host and assign necessary permission. Now we, will create a sample page for our website. We will create the sample page using Nano editor, however, any text editor can be used for this purpose.

$ nano /var/www/info.net/html/index.html

Copy paste these lines of HML code:

<html>
<head>
<title>Welcome to info.net!</title>
</head>
<body>
<h1>You are running info.net on Ubuntu 20.04!</h1>
</body>
</html>

Now use Ctrl+O to save and then Ctrl+X to exit the file.

Step 3: Create a virtual host file

Apache server comes with virtual host file by default. This file is used to serve the contents of the web server. However, we will generate the new virtual host file with the following command:

$ sudo nano /etc/apache2/sites-available/info.net.conf

Now enter the below lines by replacing the info.net by your own domain name.

<VirtualHost *:80>
ServerAdmin admin@info.net
ServerName info.net
ServerAlias info.net
DocumentRoot /var/www/info.net/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now use Ctrl+O to save and then Ctrl+X to exit the file.

Step 4: Activate virtual host configuration file

In this step, we will be creating the virtual host configuration file. For this, execute the following command in Terminal:

$ sudo a2ensite info.net.conf

Now disable the “000-default.conf” default virtual configuration file as follows:

$ sudo a2dissite 000-default.conf

Now restart Apache to activate the new configuration as follows:

$ sudo systemctl restart apache2

Step 5: Test for errors

Once all the configurations are completed, you can test for any configuration errors:

$ sudo apache2ctl configtest

You might receive the following error:

In order to resolve this error, edit the servername.conf file:

$ sudo nano /etc/apache2/conf-available/servername.conf

Then add this line by replacing the info.net with your own domain name:

ServerName info.net

Save and exit the servername.conf file and run:

$ sudo a2enconf servername

Now again execute:

$ sudo apache2ctl configtest

This time, hopefully, you will not receive any error.

Step 6: Test virtual host

Now the Apache web server is ready to serve our domain. Let’s test this by navigating to the following link in the browser:

http://info.net

Replace the info.net with your domain name.

The following index page shows the Apache server is ready to serve our domain name.

Managing Apache server

In order to manage the Apache server, here are some of the useful commands that you can run in Terminal:

To start the Apache server:

$ sudo systemctl start apache2

To stop the Apache server:

$ sudo systemctl stop apache2

To stop and then start Apache”

$ sudo systemctl stop apache2

To reload apache server to update the new configurations:

$ sudo systemctl reload apache2

To start Apache at boot:

$ sudo systemctl enable apache2

To disable Apache at boot:

$ sudo systemctl disable apache2

This article has explained in detail the installation and configuration of Apache web server in Ubuntu 20.04. We have also explained setting up a virtual host. Now you can set up multiple domains in the same Apache server. In the end, we have mentioned some commands that can be very helpful in managing the Apache web server.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.