How do I cleanly remove apache2 server on ubuntu server and then reinstall?

Solution 1:

First stop your server obviously:

sudo service apache2 stop

Remove apache2 packages and dependencies:

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

If you manually modified or installed stuff, apt might not remove it. Check what's left:

whereis apache2

Have a look whats inside these directories, and if you're confident you want to trash it, manually remove the directories. In my case:

sudo rm -Rf /etc/apache2 /usr/lib/apache2 /usr/include/apache2

Solution 2:

Run the following two commands:

sudo apt-get --purge remove apache2
sudo apt-get remove apache2-common