Remove Apache Webserver:
Published: 2023-08-16 07:15:11

##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:

   ```bash
   sudo systemctl stop apache2
   ```

2. **Remove Apache Packages:**
   Use the following command to remove the Apache packages:

   ```bash
   sudo apt purge apache2 apache2-utils
   ```

   This command will remove Apache along with any associated utilities.

3. **Remove Configuration Files:**
   To remove configuration files as well, use the following command:

   ```bash
   sudo apt purge apache2 apache2-utils apache2.2-bin apache2-common
   ```

4. **Remove Dependencies:**
   If you're sure you won't need any dependencies related to Apache, you can use the following command to remove them:

   ```bash
   sudo apt autoremove
   ```

5. **Delete Apache Data and Logs (Optional):**
   If you want to remove all Apache-related data and logs, you can delete these directories:

   ```bash
   sudo rm -r /etc/apache2
   sudo rm -r /var/www/html
   sudo rm -r /var/log/apache2
   ```

6. **Adjust Firewall Rules (Optional):**
   If you had allowed Apache traffic in your firewall (e.g., `ufw`), you can remove those rules:

   ```bash
   sudo ufw delete allow 'Apache'
   ```

7. **Check for Any Remaining Packages:**
   After performing the above steps, you can check if there are any remaining Apache-related packages:

   ```bash
   dpkg -l | grep apache
   ```

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

8. **Clean Up:**
   Finally, you can run the following command to clean up any residual configuration files and dependencies:

   ```bash
   sudo apt-get clean
   ```

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] 

```

```
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:~$ 
```

## 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

```

## 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:~$ 

```

## 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

```

## Adjust Firewall Rules (Optional):

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

```

## 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>

```

## Clean Up:

```
muthukumar@DevOpsEngineer:~$ sudo apt-get clean
muthukumar@DevOpsEngineer:~$ 
```