How to Install Apache Web Server on Ubuntu 22.04

Page content

Ubuntu 22.04 install apache

Apache is the most used server worldwide that is well known for its flexibility. It includes a large number of modules that can be used to extend the functionality of the core web server.

The latest stable version of Apache released April 6th 2023 is 2.4.57. It is also the recommended version over the previous releases. Apache is simple to set and allows you to use the .htaccess file for securing and speeding up your website. Whether you are a wordpress user or using another CMS, you can use the .htaccess file for managing website security, speed and several aspects of its functionality including redirects.

In this post, we will discuss the installation process of Apache web server on Ubuntu 22.04.

Before you begin, you need a virtual machine with a non root sudo user. If you have not created a user with sudo privileges, please create one. Please also enable the firewall before proceeding, if you have not enabled it yet. You can easily check the status of the firewall using the following command:

sudo ufw status verbose

Its output on an Apache webserver (if the firewall is active) will look like the following:

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     LIMIT IN    Anywhere                  
80,443/tcp (Apache Full)   ALLOW IN    Anywhere                  
22/tcp (v6)                LIMIT IN    Anywhere (v6)             
80,443/tcp (Apache Full (v6)) ALLOW IN    Anywhere (v6) 

If the firewall is not active, please enable it with the following command:

sudo ufw enable

Install Apache server on Ubuntu 22.04

Apache web server installation on Ubuntu 22.04 is straightforward and simple. You will not need to download the server from the download link since it is available inside Ubuntu’s software repositories.

We will begin with updating the local package index:

sudo apt update
sudo apt upgrade -y

Let the updates run, which might take a few minutes. Once the updates are complete, we are ready to install the Apache web server.

sudo apt install apache2 -y

Using the above command, the apt package manager will install the Apache webserver and the required dependencies. Just hit enter when prompted for confirmation and continue with default selection.

Now that the Apache server is installed, we are ready to proceed but our task is not finished yet since we need to open firewall ports for Apache and then set the virtual hosts file for our website to become operational.

Open firewall ports

Now it is time to adjust the firewall ports to allow access to the default ports. First enable the firewall with the following command:

It will prompt for a confirmation to continue:

Command may disrupt existing ssh connections. Proceed with operation (y|n)? 

Just type y and hit enter.

Firewall is active and enabled on system startup

When we installed the Apache server on our virtual machine, Apache registered itself with the UFW. Now, there are a few apache profiles available that can be used to enable or disable access to the Apache server through the firewall.

You can check out the list of available profiles using the following command:

sudo ufw app list

It will output a list of available profiles including Apache, Apache full and Apache Secure. Depending on the ports you are planning to use, you can use one of the three profiles. However, if you are planning to use ssl and non ssl, you will need to open both port 80 and 443. Otherwise, you can use strict ssl, in which case you will need to open port 443 only.

So, if you are planning to allow http and https traffic, you will need to enable port 80 and 443. In this case, you will need to allow the profile Apache full. Otherwise, if you are planning to open only the http port or port 80, the profile to be enabled is ‘Apache’. Currently, we are going to enable both http and https traffic on our webserver, for which we will enable Apache Full. to do that, type the following command and enter:

sudo ufw allow ‘Apache Full’

Otherwise, if you want to allow only https traffic to the Apache server through the firewall, then you can enable the Apache Secure profile with the following command. However, in this case, you will also need to enable ssl to start receiving traffic on your server.

sudo ufw allow ‘Apache Secure’

So, unless you really want to allow http traffic also, you can use the Apache Secure profile for a more secure connection to your server.

Now, you can verify the changes with the following command. The output will show you the active profile and allowed status:

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     LIMIT       Anywhere                  
Apache Full                ALLOW       Anywhere                  
22/tcp (v6)                LIMIT       Anywhere (v6)             
Apache Full (v6)           ALLOW       Anywhere (v6)  

As you can see in the above output, the status is active and http and https traffic (Apache Full) is allowed.

Test Apache Server

So, basically you have installed the Apache server and allowed traffic on default ports now. At this stage, you can check the status of your server and if it is receiving traffic. Run the following command to verify if the Apache server is active,

sudo systemctl status apache2

Its output will show you if the server is active or not. If Apache server is running, you will receive an output like the following:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-08-30 06:17:13 UTC; 1 week 3 days ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 2068379 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
   Main PID: 1888605 (apache2)
      Tasks: 21 (limit: 19154)
     Memory: 296.1M
        CPU: 44min 41.862s
     CGroup: /system.slice/apache2.service
             ├─1888605 /usr/sbin/apache2 -k start
             ├─2071536 /usr/sbin/apache2 -k start
             ├─2071547 /usr/sbin/apache2 -k start
             ├─2071566 /usr/sbin/apache2 -k start
             ├─2071570 /usr/sbin/apache2 -k start
             ├─2071579 /usr/sbin/apache2 -k start
             ├─2071583 /usr/sbin/apache2 -k start
             ├─2071592 /usr/sbin/apache2 -k start
             ├─2071595 /usr/sbin/apache2 -k start
             ├─2071600 /usr/sbin/apache2 -k start
             └─2071602 /usr/sbin/apache2 -k start

In the above output, you can see that the server is active and running. However, if it shows as inactive, you can start the Apache server with the following command:

sudo systemctl start apache2
or
sudo systemctl restart apache2

To reload apache server:

Sudo systemctl reload apache2

To stop the Apache web server:

sudo systemctl stop apache2

At this point, your Apache installation is complete and if you type the server ip in a browser, you will see the default Apache page.

Apache installed Ubuntu 22.04