How to Permanently Remove Apache2 from Linux?

If you have ever installed the Apache2 web server on a Linux machine and later decided that you no longer need it, you might wonder how to remove it permanently from your system. While it might seem straightforward, removing Linux requires a bit more than just a simple uninstallation. 

This article will guide you through the steps to permanently remove Apache2 from your Linux machine, ensuring that all related files, directories, and configurations are completely wiped out.

How to Permanently Removing apache2 From Linux?

You can follow this method to permanently remove the apache2 web server from Linux, which consists of multiple steps as discussed below:

Step 1: Finding All Paths of Apache2

To get the information of all the paths associated with the apache2 web server, you can run the following command:

$ whereis apache2

Now, let’s head over to the removal steps.

Step 2: Remove Apache2 Executables

First step is to remove the Apache2 executables using the commands written below: 

For Debian/Ubuntu-Based Distributions:

$ sudo apt remove apache2

In CentOS and Fedora, the Apache2 is named “httpd” which can be removed using the command:

$ sudo dnf remove httpd              # For Fedora
$ sudo yum remove httpd            # For CentOS

Step 3: Remove All the Dependencies Associated With Apache2  

After removing the executables, it is necessary to remove all the dependencies associated with the package: 

For Debian/Ubuntu-Based Distributions: 

$ sudo apt autormemove

The same command can be executed in CentOS and Fedora by typing the below commands:

$ sudo dnf autoremove              # For Fedora
$ sudo yum autoremove            # For CentOS

There is another method which will not only remove the package file but configuration files as well that are associated with it as shown below:

$ sudo apt purge apache2

Step 4: Verify the Apache2 Complete Removal

After running the above commands you can verify if still there is anything left related to apache by typing the below command:

$ whereis apache2

As you can see that some portion of the apache2 server is still left that you can delete by typing:

$ sudo rm -rf /etc/apache2

Whereas the “-rf” flag in the above command is used to remove the directory forcibly. 

Step 5: Verify the Complete Removal of Apache2 Again

Now you need to type the same command again if it’s been completely deleted or not by typing:

$ whereis apache2

Now you can see that the above command is not displaying any path, which means that apache2 is now completely deleted.

Pro Tip: You might be aware that you can run multiple commands on the terminal, saving you time as you don’t need to enter these commands separately. Look at the example below for a better understanding:

$ sudo apt remove --autoremove apache2

Similarly, this command can be used on CentOS and Fedora by using their respective package managers.

Conclusion

To remove Apache2 from Linux, first, remove the executables using the mentioned commands based on your distribution. After that, remove the dependencies and configurations associated with Apache2, which will permanently remove apache2 from your system.