Learn how to safely remove and reinstall Apache2 Ubuntu for a clean setup. Our Apache Support team is ready to assist you.
Remove and Reinstall Apache2 on Ubuntu
If Apache2 needs a fresh setup or is causing issues on Ubuntu, completely removing it and cleaning leftover files is essential. This guide covers basic purge commands, manual directory cleanup, full package removal, and restoring the default Apache2 configuration for a clean reinstall.
Solution 1: Basic Removal Using Purge and Autoremove
- Run the following command to stop Apache2 before removal:
sudo systemctl stop apache2
- Remove Apache2 and its related files with:
sudo apt purge apache2 apache2-utils apache2-bin apache2.2-common
- Clean up orphaned packages using:
sudo apt autoremove
- Check for remaining Apache2 files and directories (optional):
whereis apache2
ls -l /etc/apache2If needed, manually delete leftover files carefully:
sudo rm -rf <path_to_directory>
- Install a fresh copy of Apache2:
sudo apt install apache2
- Start the Apache2 service
sudo systemctl start apache2
- Check the service status to confirm it is active:
sudo systemctl status apache2Check out this guide to quickly enable mod_rewrite on Apache for Ubuntu systems.
Solution 2: Manual Cleanup After Standard Removal
- Make sure the service is stopped before cleanup:
sudo service apache2 stopVerify with:
sudo service apache2 status
- Remove Apache2 packages, configuration files, and dependencies:
sudo apt purge apache2 apache2-utils apache2-common
sudo apt autoremove --purge
- Delete leftover directories for configuration, web content, and logs:
sudo rm -rf /etc/apache2/
sudo rm -rf /var/www/html/
sudo rm -rf /var/log/apache2/
- Confirm Apache2 has been fully removed by checking the control command:
apache2ctl -SIf you see a “command not found” error, the removal is successful.

Explore this guide to smoothly restart Apache2 on Debian and manage its configuration.
Solution 3: Removing All Apache Packages Using dpkg and Purge
- Make sure the service is stopped before removal:
sudo service apache2 stop
- Remove all Apache2 packages and configuration files:
sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
- Clean up orphaned dependencies no longer needed:
sudo apt-get autoremove --purge
- Manually Delete Configuration and Log Directories (optional but recommended)
sudo rm -rf /etc/apache2
sudo rm -rf /var/lib/apache2
sudo rm -rf /var/log/apache2
- Check if any Apache2 packages remain:
dpkg -l | grep apacheNo output or a message indicating packages are not installed confirms the removal.
Complete Removal Commands
- Stop Apache2 service
sudo systemctl stop apache2
- Purge Apache2 packages and configs
sudo apt-get purge apache2 apache2-utils apache2-bin apache2.2-common
- Remove orphaned dependencies
sudo apt-get autoremove --purge
- Manually clean remaining directories
sudo rm -rf /etc/apache2
sudo rm -rf /var/lib/apache2
sudo rm -rf /var/log/apache2
- Verify removal
dpkg -l | grep apache
- Reinstall Apache2
sudo apt install apache2
- Start Apache2 service and check status
sudo systemctl start apache2
sudo systemctl status apache2Discover the fastest way to install Apache Benchmark on Ubuntu with this guide.
How to Uninstall Apache2 in Ubuntu
- Stop Apache2 service
sudo systemctl stop apache2
- <li” aria-level=”1″>Remove Apache2 packages and configuration files
Remove Apache2 packages and configuration files
- Remove unused dependencies
sudo apt autoremove --purge
- Manually delete remaining configuration directories (optional but recommended)
sudo rm -rf /etc/apache2
sudo rm -rf /var/www/html
sudo rm -rf /var/log/apache2
Use caution with rm -rf to avoid deleting the wrong files.
- Verify removal
systemctl status apache2
dpkg -l | grep apache2The service should show inactive or not found, and no Apache packages should appear in the list.
How to Restore Apache2 Directory on Ubuntu/Debian
- Remove existing Apache2 packages (optional but recommended)
sudo apt-get remove --purge apache2 apache2-utils
sudo apt-get autoremove
- Reinstall Apache2 with default configuration
sudo apt-get --reinstall install apache2 -o DPkg::Options::="--force-confmiss"Forces installation of missing configuration files and restores /etc/apache2 directory.
- Reinstall additional components if needed
sudo apt-get --reinstall install apache2.2-common -o DPkg::Options::="--force-confmiss"Use for modules or specific packages that were previously installed.
- Restart Apache2 service
sudo systemctl restart apache2Apply the restored configuration and ensure Apache2 is running.
Learn how to activate SSL on Apache2 and secure your website effortlessly.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
By following these steps, you can successfully remove and reinstall Apache2 Ubuntu, ensuring all old configurations and files are cleared for a clean, fresh setup. This process helps prevent conflicts and keeps your web server running smoothly.
In brief, our Support Experts demonstrated how to fix the “554 5.7.1 : Relay access denied” error.
