How to Completely Uninstall Nginx?
Nginx is one of the most popular web servers in the world. But sometimes, you may need to completely uninstall it from your system, whether to fix configuration issues or for a clean reinstallation.
Want to get rid of Nginx on your Ubuntu or Linux system? Then you’ve come to the right place! Debugbar has prepared a little step-by-step guide to cleanly uninstall this web server, delete all its configuration files and start again with a clean install. In short, we’ll explain how to purge Nginx from your machine in just a few simple commands.
Uninstalling Nginx on Ubuntu and Debian with apt
The easiest way to uninstall Nginx on an Ubuntu, Linux or Debian system is to use the apt package manager. Open a terminal and type these commands with sudo:
- sudo apt-get remove nginx
This command removes the main Nginx package but keeps some configuration files in the /etc/nginx directory. This is useful if you plan on reinstalling Nginx in the future.
Otherwise, if you want to delete Nginx and all its files permanently, you’ll need to use the purge command:
- sudo apt-get purge nginx
This will uninstall Nginx completely from your Linux system by also deleting the /etc/nginx folder and its contents. Do this if you want to start again with a clean install later.
Removing Remaining Nginx Packages and Dependencies
The removal process for Nginx with apt remove or apt purge does not remove everything. Some packages installed with Nginx but no longer needed may remain on your system.
To clean up, you need to run:
- sudo apt-get autoremove
Any packages that have become useless will then be listed. Type “Y” to confirm their removal.
Good to know: if you had installed other components for your websites like PHP, MySQL or separate Nginx packages (nginx-core, nginx-common…), you’ll need to remove them individually with:
- $ sudo apt remove nginx “package name without quotes” (e.g.: $ sudo apt remove nginx nginx-core)
Keep this in mind if you want to remove all web hosting components installed on your device.
Completely Removing Nginx on CentOS and Red Hat
For those who use CentOS or Red Hat, the procedure is slightly different.
- First stop the Nginx service:
- sudo systemctl stop nginx
- Then remove the Nginx package with dnf (or yum on older versions):
- sudo dnf remove nginx
- Next, clean up any remaining Nginx configuration files and directories:
- sudo rm -rf /etc/nginx
- sudo rm -rf /var/log/nginx
- sudo rm -rf /var/cache/nginx
- sudo rm -rf /run/nginx
- Finally, remove the Nginx systemd service file:
- sudo rm /usr/lib/systemd/system/nginx.service
And that’s it, no more traces of Nginx on your device! You can verify it has been removed by running sudo systemctl status nginx which should (hopefully) return an error.