1. Overview
In this article, we’ll be going over the steps to follow when uninstalling the NGINX web server. We’ll cover both Debian and RPM-based systems.
2. Uninstalling NGINX in Debian Systems
Due to different reasons, we might decide to uninstall NGINX. This might result from wrong configurations or us just wanting it out of our systems.
To uninstall NGINX in our Debian-based system, we have two options to choose from. To begin with, we can either use the apt remove command or choose the apt purge command. Before we look at either of these commands, let’s check the state of our NGINX server. To check the status, we’ll use the systemctl status command since it works for both the Debian- and RPM-based systems and any other Linux system that uses systemd:
$ sudo systemctl status nginx.service
○ nginx.service - A high performance web server and a reverse proxy>
Loaded: loaded (/lib/systemd/system/nginx.service; disabled; v>
Active: inactive (dead)
This command shows us if the server is active (running) or if it’s inactive (dead/ not running). Now, let’s go ahead and look at how to uninstall it.
2.1. Remove NGINX
We can use apt remove to uninstall NGINX:
$ sudo apt remove nginx
This removes NGINX from the system but leaves the configuration files behind. In other words, the site configuration files found in the /etc/nginx folder are left and aren’t removed. The site files placed in the /var/www won’t be wiped out during this process. Using apt remove for the job is only beneficial if we plan to reinstall NGINX in the future.
When issuing this command, we can further add separate NGINX packages that we’d want to be removed at the same time. For example. we can run:
$ sudo apt remove nginx nginx-commons
When we check the status of the server now, we find out that it has been masked:
$ sudo systemctl status nginx.service
○ nginx.service
Loaded: masked (Reason: Unit nginx.service is masked.)
Active: inactive (dead)
2.2. Purge Nginx
Apart from the remove command, we can also use purge to remove NGINX:
$ sudo apt purge nginx
The apt purge command completely removes NGINX from our system. It will uninstall the NGINX server and delete the /etc/nginx folder with all its contents. This command also deletes all the configuration files and settings associated with the NGINX server.
Using apt purge to uninstall NGINX is the best option if we don’t expect to use NGINX in the near future. It’s also the preferred way if we want to perform a clean re-installation of the same version of NGINX or a later one.
After running purge, if we check the status, we won’t find the nginx.service unit, thus confirming that the server has been deleted completely:
$ sudo systemctl status nginx.service
Unit nginx.service could not be found.
After uninstalling NGINX using the purge command, we have to remove all the remaining dependencies that were used by NGINX but are not required anymore:
$ sudo apt-get autoremove
2.3. Nginx Failed to Remove
We need to note that, in some occurrences, running the above commands may not wipe out NGINX completely, and upon restart, NGINX may still be running. This may be caused by us removing the wrong variant of the installed NGINX server.
NGINX has different flavors available. These flavors are nginx-core, nginx-full, nginx-light, nginx-extras, nginx-naxsi, and nginx-common (NGINX is a meta package). In such a precedent, we can run:
$ sudo apt-get remove --purge nginx*
This will remove anything installed that is related to NGINX. It’ll also remove the configuration files, and additionally, it will list packages available in repositories that matched the regex “*nginx**” (these packages are just listed and aren’t installed). If we check the status, it will give us similar output to that of the purge command.
Again, if the autoremove command doesn’t purge auto-removable packages by default, then we can use:
$ sudo apt-get autoremove --purge
3. Uninstalling NGINX in RPM Systems
For RPM-based distros like CentOS7, RHEL7, and Fedora, if we want to remove NGINX, we go through a different set of steps. First, we have to stop and disable the NGINX service from running:
$ sudo systemctl stop nginx.service
$ sudo systemctl disable nginx.service
Secondly, we remove the auto-start from NGINX:
$ sudo chkconfig nginx off
Following, we proceed to remove all NGINX files, including binary files, configuration files, and even jpg files:
$ sudo rm -rf /etc/nginx
$ sudo rm -rf /etc/init.d/nginx
$ sudo rm -rf /var/log/nginx
$ sudo rm -rf /var/cache/nginx/
$ sudo rm -rf /usr/sbin/nginx
We also have to remove the NGINX service from our system:
$ sudo rm -rf /usr/lib/systemd/system/nginx.service
Lastly, we remove nginx from our system:
$ sudo dnf remove nginx
Let’s note that dnf works similarly to yum. Likewise, we could instead run:
$ sudo yum remove nginx
If we had created a user for NGINX, then we’d need to delete it together with its directories:
$ sudo userdel -r nginx
If we check the status of our server, we won’t find the nginx.service unit, thus confirming that the server has been successfully uninstalled.
4. Conclusion
In this article, we’ve looked at how to uninstall NGINX from both Debian- and RPM-based distributions.
While looking at Debian, we noted that we have two options, apt purge and apt remove. If we don’t want to lose the settings while uninstalling, then we should use remove. Otherwise, we use purge. If we want to remove it completely or we want to perform a clean re-installation, we should use the purge command.
We further saw how to remove NGINX in RPM-based systems.