DEV Community

Cover image for Remove Apache Webserver:
MuthuKumar
MuthuKumar

Posted on • Updated on

Remove Apache Webserver:

Remove Apache Webserver:

If you want to completely remove Apache from your Linux system, you can use the following commands. Please note that this will remove Apache and its configuration files, so make sure you have backed up any important data or configurations before proceeding.

  1. Stop Apache: Before removing Apache, stop the service if it's running:
   sudo systemctl stop apache2
Enter fullscreen mode Exit fullscreen mode
  1. Remove Apache Packages: Use the following command to remove the Apache packages:
   sudo apt purge apache2 apache2-utils
Enter fullscreen mode Exit fullscreen mode

This command will remove Apache along with any associated utilities.

  1. Remove Configuration Files: To remove configuration files as well, use the following command:
   sudo apt purge apache2 apache2-utils apache2.2-bin apache2-common
Enter fullscreen mode Exit fullscreen mode
  1. Remove Dependencies: If you're sure you won't need any dependencies related to Apache, you can use the following command to remove them:
   sudo apt autoremove
Enter fullscreen mode Exit fullscreen mode
  1. Delete Apache Data and Logs (Optional): If you want to remove all Apache-related data and logs, you can delete these directories:
   sudo rm -r /etc/apache2
   sudo rm -r /var/www/html
   sudo rm -r /var/log/apache2
Enter fullscreen mode Exit fullscreen mode
  1. Adjust Firewall Rules (Optional): If you had allowed Apache traffic in your firewall (e.g., ufw), you can remove those rules:
   sudo ufw delete allow 'Apache'
Enter fullscreen mode Exit fullscreen mode
  1. Check for Any Remaining Packages: After performing the above steps, you can check if there are any remaining Apache-related packages:
   dpkg -l | grep apache
Enter fullscreen mode Exit fullscreen mode

If there are any packages left, you can use sudo apt purge <package-name> to remove them.

  1. Clean Up: Finally, you can run the following command to clean up any residual configuration files and dependencies:
   sudo apt-get clean
Enter fullscreen mode Exit fullscreen mode

That's it! Apache and its related components should be removed from your Linux system. Always be cautious when using the purge command, as it permanently deletes files and configurations. Double-check that you're removing the right packages to avoid accidentally deleting important data.

Try it YourSelf:

muthukumar@DevOpsEngineer:~$ sudo systemctl stop apache2
muthukumar@DevOpsEngineer:~$    sudo apt purge apache2 apache2-utils
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  apache2-data libapache2-mod-php8.1 php8.1 php8.1-bcmath php8.1-cli
  php8.1-common php8.1-curl php8.1-imagick php8.1-intl php8.1-mbstring
  php8.1-mysql php8.1-opcache php8.1-readline php8.1-xml php8.1-zip
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  apache2* apache2-utils*
0 upgraded, 0 newly installed, 2 to remove and 65 not upgraded.
After this operation, 951 kB disk space will be freed.
Do you want to continue? [Y/n] 

Enter fullscreen mode Exit fullscreen mode
Do you want to continue? [Y/n] Y
(Reading database ... 242418 files and directories currently installed.)
Removing apache2 (2.4.52-1ubuntu4.6) ...
Removing apache2-utils (2.4.52-1ubuntu4.6) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for ufw (0.36.1-4ubuntu0.1) ...
(Reading database ... 242341 files and directories currently installed.)
Purging configuration files for apache2 (2.4.52-1ubuntu4.6) ...
dpkg: warning: while removing apache2, directory '/etc/apache2/sites-available' 
not empty so not removed
Processing triggers for ufw (0.36.1-4ubuntu0.1) ...
muthukumar@DevOpsEngineer:~$ 
Enter fullscreen mode Exit fullscreen mode

Remove Configuration Files:

muthukumar@DevOpsEngineer:~$ sudo apt purge apache2 apache2-utils apache2.2-bin apache2-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

Enter fullscreen mode Exit fullscreen mode

Remove Dependencies:

muthukumar@DevOpsEngineer:~$ sudo apt autoremove
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  apache2-data libapache2-mod-php8.1 php8.1 php8.1-bcmath php8.1-cli
  php8.1-common php8.1-curl php8.1-imagick php8.1-intl php8.1-mbstring
  php8.1-mysql php8.1-opcache php8.1-readline php8.1-xml php8.1-zip
0 upgraded, 0 newly installed, 15 to remove and 52 not upgraded.
After this operation, 25.2 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 242178 files and directories currently installed.)
Removing apache2-data (2.4.52-1ubuntu4.6) ...
Removing php8.1 (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-imagick (3.7.0-3+ubuntu22.04.1+deb.sury.org+1) ...
Removing libapache2-mod-php8.1 (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-bcmath (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-cli (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-zip (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-xml (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-curl (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-intl (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-mbstring (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-mysql (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-opcache (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-readline (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Removing php8.1-common (8.1.21-1+ubuntu22.04.1+deb.sury.org+1) ...
Processing triggers for man-db (2.10.2-1) ...
muthukumar@DevOpsEngineer:~$ 

Enter fullscreen mode Exit fullscreen mode

Delete Apache Data and Logs (Optional):

muthukumar@DevOpsEngineer:~$ sudo rm -r /etc/apache2
muthukumar@DevOpsEngineer:~$ sudo rm -r /var/www/html
muthukumar@DevOpsEngineer:~$ sudo rm -r /var/log/apache2

Enter fullscreen mode Exit fullscreen mode

Adjust Firewall Rules (Optional):

muthukumar@DevOpsEngineer:~$ sudo ufw delete allow 'Apache'
Could not delete non-existent rule
Could not delete non-existent rule (v6)

Enter fullscreen mode Exit fullscreen mode

Check for Any Remaining Packages:

muthukumar@DevOpsEngineer:~$ Check for Any Remaining Packages:
Command 'Check' not found, did you mean:
  command 'check' from deb gitlab-shell (13.19.1-2ubuntu0.20.04.2)
Try: sudo apt install <deb name>

Enter fullscreen mode Exit fullscreen mode

Clean Up:

muthukumar@DevOpsEngineer:~$ sudo apt-get clean
muthukumar@DevOpsEngineer:~$ 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)