What is the Best Way to Uninstall Nginx?

Nginx is used to manage web servers and comes with some great features like high performance and speed, robustness, and scalability. However, there comes a situation where you might need to uninstall it because of any reason, like upgrading to a newer version, switching to a different web server, or troubleshooting issues. 

This post will explore all the methods to uninstall Nginx on Linux:

Uninstall Nginx Package Only

If you want to uninstall the Nginx web server package executable files then the below command can be used for this purpose:

$ sudo apt remove nginx        # For Ubuntu
$ sudo yum remove nginx        # For CentOS
$ sudo dnf remove nginx        # For Fedora

In the same way, you need to remove the nginx-common package as well that you can do by typing the below command:

$ sudo apt remove nginx-common        # For Ubuntu
$ sudo yum remove nginx-common        # For CentOS
$ sudo dnf remove nginx-common        # For Fedora

To save some time, you can also run both of the above-mentioned commands together as shown below:

$ sudo apt remove nginx nginx-common            # For Ubuntu
$ sudo yum remove nginx nginx-common            # For CentOS
$ sudo dnf remove nginx nginx-common            # For Fedora

Uninstall Nginx Package and Configuration Files Only

The remove command will only be able to uninstall the package files on nginx web server. So if you want to uninstall the Nginx package along with the configuration files, then the below command can serve you for this purpose:

$ sudo apt purge nginx                #For Debian/Ubuntu
$ sudo yum remove nginx               #For CentOS
$ sudo dnf remove --purge nginx       #For Fedora

The next thing you can do is to remove an nginx-common package as well as a for the Nginx package and is installed automatically when you install Nginx. It is used to maintain other Nginx-related packages that may be installed on your system, and you can uninstall it by typing:

$ sudo apt purge nginx-common                # For Ubuntu
$ sudo yum remove nginx-common               # For CentOS
$ sudo dnf remove --purge nginx-common       # For Fedora

Note: You might be aware that you can run multiple commands as well on the terminal. This will not only remove the Nginx web server but also the nginx-common package using a single command as shown below:

$ sudo apt purge nginx nginx-common                # For Ubuntu
$ sudo yum remove nginx nginx-common               # For CentOS
$ sudo dnf remove --purge nginx nginx-common       # For Fedora

Uninstall Nginx Dependencies

When you install the nginx web server, then some additional packages will also be installed along with it so that it can work properly. But when you run the purge command, these additional packages will not be removed, and for that, you need to run the below command:

$ sudo apt autoremove           # For Ubuntu
$ sudo yum autoremove           # For CentOS 
$ sudo dnf autoremove           # For Fedora

Verify the Complete Uninstallation of Nginx

If you want to verify that there is still anything left related to the Nginx web server, then you can run the below command: 

$ whereis nginx

The no result output shows that the Niginx has been removed completely from the system. 

Conclusion

The Nginx can be removed from Linux using various ways, i.e., removing only executable packages, removing only dependencies, removing only configurations, and removing all at once. It depends on the end-user’s requirement of the user to choose the method. This post has listed all the possible methods to uninstall Niginx from Linux.