I've decided to switch my web server from Apache2 to Nginx on my Ubuntu machine, and I want to make sure the old installation is entirely gone. I tried a simple remove command, but I still see configuration folders and some service entries left behind. What is the proper sequence of commands to purge the packages, remove dependencies, and delete any leftover config files safely?
3 answers
To completely remove Apache2, you should use the purge command instead of just remove. Start by stopping the service with sudo systemctl stop apache2. Then, execute sudo apt-get purge apache2 apache2-utils apache2-bin apache2-common. This command removes the packages along with their configuration files. Afterward, run sudo apt-get autoremove to delete any orphaned dependencies that were only needed by Apache. To be absolutely thorough, manually check for the /etc/apache2 directory; if it still exists, you can delete it with sudo rm -rf /etc/apache2. This process ensures a "clean slate" for your next web server installation and prevents port conflicts or leftover service fragments from causing issues later.
If I use the purge command, will it also delete the website files I have stored in /var/www/html/, or does it only target the server's own configuration and binary files?
ou can verify if it's truly gone by running which apache2 or apache2 -v. If the system returns "command not found," the uninstallation was successful.
I agree with Jennifer. Another quick check is to run sudo systemctl status apache2. If it shows "Unit apache2.service could not be found," you’ve successfully cleared the service from the system's init manager as well!
That is an excellent question, Robert. The apt purge command is designed to remove the software and its configuration files (usually in /etc/), but it typically leaves your data in /var/www/ untouched. However, as an SEO and DevOps best practice, you should always create a backup of your web directory before running any destructive commands. If you want those files gone too, you would need to manually run sudo rm -rf /var/www/html after you have verified the uninstallation.