How to Completely Uninstall and Reinstall Apache2 on Ubuntu Server for a Clean Setup


1 views

Sometimes Apache2 configurations can become corrupted or overly complex after multiple modifications. When you're encountering persistent issues that standard troubleshooting can't resolve, a complete reinstall often proves more efficient than hunting down individual configuration problems.

First, stop all Apache2 services:

sudo systemctl stop apache2
sudo systemctl disable apache2

Now completely remove Apache2 and its dependencies:

sudo apt-get purge apache2 apache2-utils apache2-bin apache2-data
sudo apt-get autoremove

Don't forget to clean up residual config files:

sudo rm -rf /etc/apache2/
sudo rm -rf /var/www/
sudo rm -f /etc/systemd/system/multi-user.target.wants/apache2.service

Check for any remaining Apache-related packages:

dpkg -l | grep apache

If any packages appear, remove them with:

sudo apt-get purge package-name

Update your package lists before installation:

sudo apt-get update
sudo apt-get install apache2

After installation, verify the default configuration works:

sudo systemctl start apache2
sudo systemctl enable apache2

Check the status with:

systemctl status apache2

If you had backup of your configurations, you can selectively restore them. For virtual hosts:

sudo cp /backup/path/vhosts/* /etc/apache2/sites-available/
sudo a2ensite your-site.conf
sudo systemctl reload apache2

For modules:

sudo cp /backup/path/mods-available/* /etc/apache2/mods-available/
sudo a2enmod your-module
sudo systemctl restart apache2



When Apache2 configurations become corrupted or you suspect unauthorized modifications, a complete purge and reinstall is often more efficient than troubleshooting individual issues. Unlike simple removal, this process ensures all configuration files, dependencies, and system modifications are properly reset.

First, stop all running Apache processes to prevent conflicts during removal:

sudo systemctl stop apache2
sudo systemctl disable apache2

Use these commands to remove Apache2 and related packages while preserving your document root (/var/www/html contents):

sudo apt-get purge apache2 apache2-utils apache2-bin apache2-data
sudo apt-get autoremove --purge

For thorough cleanup, manually remove remaining directories:

sudo rm -rf /etc/apache2/ /var/lib/apache2/ /var/log/apache2/

Confirm no Apache components remain:

dpkg -l | grep apache
which apache2

Both commands should return empty output if removal was successful.

Install Apache2 with default configurations:

sudo apt-get update
sudo apt-get install apache2

Verify the installation:

sudo systemctl status apache2
apache2 -v

After reinstalling, consider these best practices:

# Secure your installation
sudo a2enmod security
sudo a2enmod ssl

# Create backup of default config
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak

If you encounter port conflicts after reinstallation:

# Check for processes using port 80/443
sudo lsof -i :80
sudo lsof -i :443

# Alternatively, check Apache error logs
sudo tail -f /var/log/apache2/error.log