193

It seems a simple apt-get remove apache2 does not completely remove apache2 as I can still see it on one of the processes when running top. How does one remove apache2 completely on his ubuntu server?

It's not removed indeed:

~# which apache2
/usr/sbin/apache2
~# whereis apache2
apache2: /usr/sbin/apache2 /etc/apache2 /usr/lib/apache2 /usr/share/apache2 /usr/share/man/man8/apache2.8.gz

But when I do apt-get remove apache2 again:

# apt-get remove apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package apache2 is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Jürgen Paul
  • 2,097

10 Answers10

211

apache2 is a metapackage that just selects other packages. If you installed apache by installing that package, you just need to run this to clean up the automatically selected packages:

sudo apt autoremove

If that doesn't work, you might have installed one of the dependents manually. You can target all the apache2- packages from space and nuke the lot:

sudo apt remove apache2*

For future reference, to find out which package a binary is from, you can run this:

dpkg -S `which apache2`

I'd expect that to come back with apache2.2-bin (at the time of writing).

Oli
  • 293,335
  • 14
    also, use --purge if you want the configuration files to be deleted as well: apt-get --purge remove apache2 – xyious Aug 17 '12 at 10:03
  • 2
    The last command outputs apache2-mpm-prefork: /usr/sbin/apache2 – Jürgen Paul Aug 17 '12 at 10:03
  • 1
    @Severus fair enough -- I was just guessing but that makes sense. Yeah I'd just use the wildcard but keen an eye on what it's going to delete. apache2-common is used by some tools that aren't the apache2 httpd so you might need to reinstall some thing after. – Oli Aug 17 '12 at 10:06
  • @Oli Removing things with a regex 'apache2*' is dangerous, loads of things end up getting removed – jhbsk Dec 08 '13 at 07:57
  • 2
    @jasdeepkhalsa It's not "dangerous". apt-get (unlike apt-cache) limits its searches to names-only so it's not that far-reaching. Everything it captures is Apache or Apache dependent. Check it yourself with apt-get -s remove apache2* – Oli Dec 09 '13 at 08:53
  • should be sudo apt-get remove apache2.* – Li-chih Wu Mar 11 '16 at 05:34
  • it's too dangerous to run sudo apt-get autoremove – Khaled AbuShqear May 27 '18 at 10:39
  • @Shqear Not really. If the packages you need are only installed to fulfil dependencies on other packages, that's your problem. Mark them manual and autoremove will never remove them. And if you're concerned that something bad might happen, use the -s flag to simulate a dry-run before the real thing. – Oli May 29 '18 at 09:20
  • The Apache is there after server reboot (!), but E: Unable to locate package apache2.* on sudo apt remove apache2.*... With UBUNTU 20 LTS only sudo apt-get purge apache2 apache2-utils apache2-bin apache2.2-common is working. – Peter Krauss Jul 05 '20 at 12:46
  • This just removed dokku. Fortunately not production but beware, autoremove is absolutely dangerous. If this was production I would be absolutely F(*&#%'d right now. – Neil Apr 25 '22 at 18:57
  • On Ubuntu 22.04, this doesn't work and removes 0 packages. – MikeB Mar 07 '24 at 16:21
  • @MikeB I've changed the command to apt remove apache2*. This used to us a "regular expression" and now seems to use a glob. – Oli Mar 08 '24 at 09:23
192

Follow these steps to remove the apache2 service using Terminal:

  1. First stop the apache2 service if it is running with: sudo service apache2 stop
  2. Now remove and cleanup all the apache2 packages with:

    sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
    //or 
    sudo apt-get purge apache2 apache2-utils apache2-bin apache2.2-common
    
  3. Finally, run sudo apt-get autoremove just in case any other cleanup in needed

You can do the following two tests to confirm apache has been removed:

  1. which apache2 - should return a blank line
  2. sudo service apache2 start - should return apache2: unrecognized service
T.Todua
  • 551
  • 1
  • 4
  • 15
jhbsk
  • 2,031
86

A very simple and straightforward way that worked for me is as follows:

  1. Stop apache2.

    sudo service apache2 stop
    
  2. Uninstall Apache2 and its dependent packages.

    sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
    
  3. Use autoremove option to get rid of other dependencies.

    sudo apt-get autoremove
    
  4. Check whether there are any configuration files that have not been removed.

    whereis apache2
    
  5. If you get a response as follows apache2: /etc/apache2 remove the directory and existing configuration files.

    sudo rm -rf /etc/apache2  
    

Source: How to uninstall and remove Apache2 on Ubuntu or Debian by Dan Nanni

find_x
  • 961
  • 6
  • 4
8

Remove apache2 from the Ubuntu

enter image description here

To remove totally from your localhost

1st check the current status using

service apache2 status

if it's active (running) write next

sudo apt remove apache2*

Say yes using y.

Kulfy
  • 17,696
8

For a Debian based OS, like Ubuntu, use:

sudo apt remove --purge apache2
sudo apt autoremove -y
Nmath
  • 12,333
MD SHAYON
  • 385
5

First check if you are using right package name, IMO correct package name is : apache2.x-common

If you want to completely remove the package including config files then try:

    dpkg --purge apache2.2-common
Amol Sale
  • 1,006
4

Take time to check if you are using the different package apache2.2-bin for Ubuntu 16.04 the latest is apache2.4-bin

sudo apt-get purge apache2 apache2-utils apache2.4-bin apache2.4-common

pensebien
  • 171
3

I tried the other answers here but this is the most complete solution I have found which worked for me:

$ sudo service apache2 stop
$ sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
$ sudo apt-get autoremove

The output of the below command will provide you with information the installed package software, version, architecture and short description about the package. grep command is used to find the apache package

$ sudo dpkg --get-selections | grep apache | cut -f 1
$ sudo apt-get remove --purge <package name from the previous command output>
$ sudo whereis apache
$ sudo rm -rf <directory/file path from the previous command output>

see http://www.xavierdilipkumar.com/post/how-to-uninstall-php-apache-and-mysql-on-ubuntu

1

Use this to delete installation and its dependencies. It will also reload the firewall

sudo dpkg --purge apache2
sotirov
  • 3,169
0

These commands will completely remove Apache2, all its configs, logs and www files.

sudo service apache2 stop
sudo apt purge apache2
sudo apt purge apache2-utils
sudo apt autoremove
sudo rm -rf /etc/apache2
sudo rm -rf /var/lib/apache2
sudo rm -rf /var/log/apache2

And if you want to remove all www files too,

sudo rm -rf /var/www/html

WARNING: sudo rm -rf /var/www/html will delete all files in your /var/www/html directory, make sure you don't have anything important there!!!

sotirov
  • 3,169
  • 1
    You should not add the last command. The question is how to remove apache2, not the files. Someone might copy and paste all the commands without reading your warning (I was going to do the same). That will be a big disaster. So please remove the last command. – Biswadeep Sarkar Jan 06 '24 at 19:49
  • This is why the warning is there, and now you comment. – sotirov Jan 06 '24 at 22:45