Assuming you are using Ubuntu LTS, you are likely using an Apache version that is often older than the actual stable by the Apache Foundation. By using an older version of Apache, you are missing out on new features and improvements that have been made to the software. In addition, non-security-related bug fixes are not included in Ubuntu LTS releases, which means that your web server may be less stable than it could be. The easiest option for most users without compiling Apache is to install the latest version using a third-party repository source, which in this case, the third-party repository by Ondřej Surý.
In the following tutorial, you will learn how to Upgrade Apache Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa desktop or server using the custom third-party repository by Ondrey Sury, who is most known for his PHP maintainer work Debian/Ubuntu.
Table of Contents
Recommended Steps Before Installation
Before you begin, run an update on your system to ensure all packages are up-to-date to avoid any conflicts during the installation.
sudo apt update
Optionally, you can list the updates for users who require review or are curious.
sudo apt --list upgradable
Proceed to upgrade any outdated packages using the following command.
sudo apt upgrade
Import Apache PPA
Adding Apache Repository by Ondřej Surý Repository
The first step is to import the Apache webserver to the latest version is add the repository by Ondřej Surý. Before you begin, run the following command to ensure these packages are present; most users should be already installed on your system.
sudo apt install dirmngr ca-certificates software-properties-common gnupg gnupg2 apt-transport-https -y
If you’re new to importing GPG keys from Ubuntu’s KeyServer, a common issue is that your command line terminal may not create the necessary directories. But don’t worry! With a straightforward command, these directories will be successfully generated so all can go smoothly with launching PPAs on Launchpad.
sudo gpg --list-keys
gpg: directory '/root/.gnupg' created gpg: keybox '/root/.gnupg/pubring.kbx' created gpg: /root/.gnupg/trustdb.gpg: trustdb created
This can be skipped, but if you encounter an issue, just run the command and re-try.
The next task is to import the GPG key needed.
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/ondrej.gpg --keyserver keyserver.ubuntu.com --recv-keys 14AA40EC0831756756D7F66C4F4EA0AAE5267A6C
gpg: keybox '/usr/share/keyrings/ondrej.gpg' created gpg: key 4F4EA0AAE5267A6C: public key "Launchpad PPA for Ondřej Surý" imported gpg: Total number processed: 1 gpg: imported: 1
Ensure success with your installation by choosing the correct command for the version of Ubuntu you’re running! It’s easy – import your GPG key and select a LaunchPAD PPA that matches.
Import Apache PPA for Ubuntu 22.04 LTS Jammy Jellyfish LTS
echo 'deb [signed-by=/usr/share/keyrings/ondrej.gpg] https://ppa.launchpadcontent.net/ondrej/apache2/ubuntu jammy main' | sudo tee -a /etc/apt/sources.list.d/apache2.list
Import Apache PPA for Ubuntu 20.04 LTS Focal Fossa LTS
echo 'deb [signed-by=/usr/share/keyrings/ondrej.gpg] https://ppa.launchpadcontent.net/ondrej/apache2/ubuntu focal main' | sudo tee -a /etc/apt/sources.list.d/apache2.list
Install or Upgrade Apache
Update your repository to reflect the new change.
sudo apt update
With the PPA now added, you can install the latest Apache version from the PPA or upgrade your existing one. I recommend running the installation command for most users, even if Apache is present.
sudo apt install apache2
Lastly, confirm which version and its source to ensure Apache2 is installed from the PPA using the apt-cache policy command.
apt-cache policy apache2
As stated above, when this tutorial was created, Ubuntu’s default repository had version 2.4.52, while the PPA repository contained and successfully installed Apache version 2.4.54.
Now, make sure Apache is running by using the systemctl command.
systemctl status apache2
If Apache is not activated, to start the web server application, use the following command:
sudo systemctl start apache2
To enable Apache on system boot, use the following:
sudo systemctl enable apache2
This version will always be maintained and updated when a new version of Apache is available; Ondřej Surý updates typically within a few days to a few weeks as he maintains PHP, Nginx, and Apache repositories built; he is consistent with his updating.
Optional. Configure UFW Firewall for Apache
After installing or upgrading Apache, you will need to modify the UFW rules if you have UFW installed. To allow outside access to the default web ports. Luckily, during the installation, Apache registers itself with UFW to provide a few profiles that can be used to enable or disable access, making it easy and quick to configure.
If you want to install the UFW firewall, run the following command.
sudo apt install ufw -y
Once UFW is installed, enable UFW to start and be active on the system boot.
sudo ufw enable
Next, list the application profiles to see the Apache profiles that are available by the following command:
sudo ufw app list
Available applications: Apache Apache Full Apache Secure
From the output above, you have three profile options to choose from. To break it down, Apache runs on port 80 (HTTP), Apache Secure runs on port 443 (HTTPS), and Apache Full is a combination of allowing both. The most common is either Apache Full or Apache Secure.
For the tutorial, since we have not set up SSL, we will enable (Apache) profile with the following command:
sudo ufw allow 'Apache'
Rule added Rule added (v6)
As mentioned above, the rules have been added for both IPV4 and IPV6. Later on, you can disable this profile and enable secure only or disable the Apache rule and use the Apache Full rule instead.
How to Update Apache on Ubuntu Linux
Given that Apache was installed and upgraded with the APT package manager, run the standard APT update commands.
sudo apt update
If one is available, upgrade Apache as follows:
sudo apt upgrade
Or upgrade Apache by itself:
sudo apt upgrade apache2
That is all you need to do to keep your version updated.
Remove Apache Upgrade on Ubuntu Linux
First, stop the service for users who want to restore the standard Ubuntu version.
sudo systemctl stop apache2
Now remove Apache with the following command.
sudo apt autoremove apache2
Remove the imported PPA if you return to the default version from Ubuntu’s repository.
sudo rm /etc/apt/sources.list.d/apache2.list
Lastly, run an APT update to reflect the changes to the APT sources list entries.
sudo apt-get update
From here, you can now re-install the standard Apache version.
Whether managing your server or working as a system administrator, keeping your software up-to-date is important. This includes the Apache webserver that is so prevalent on Linux systems. This tutorial demonstrated how to upgrade Apache on an Ubuntu system using cli commands and the command line terminal. Additionally, we showed how to import a GPG key from the Ubuntu server and include a third-party repository.