How to Uninstall NGINX

Last updated on October 3rd, 2024 at 11:15 am

NGINX is a popular web server used by many high traffic websites and applications all over the world. It is very versatile and easy to manage. However, sometimes web administrators need to remove NGINX server from their systems. In this article, we will learn how to uninstall NGINX. Depending on your Linux distribution, you will need to use the appropriate steps to uninstall NGINX from your server. We have described the steps to completely remove NGINX from Ubuntu/Debian as well as RPM-based Linux.

Why Uninstall NGINX

Here are some common use cases when you need to remove NGINX from your system:

  1. Configuration Problems – Sometimes your NGINX configuration may become faulty which causes problems in your website/application. In such cases, you may need to remove and re-install NGINX.
  2. Corrupted Installation – Sometimes, the NGINX installation may have become corrupted due to incomplete or faulty update. In this case also, you will need to uninstall NGINX
  3. Use Different Server – You may want to switch to another web server such as Apache. By default, NGINX runs on port 80, also used by Apache. So you will need to get rid of NGINX to be able to switch to Apache server and run it on port 80.
  4. Installation from Source – If you are installing NGINX from source, and your system already has an existing NGINX installation, then you must remove it before proceeding with installation. Otherwise, there will be 2 instances of NGINX on your system and Linux may use the wrong one.
  5. Start Afresh – If you feel your system has become very slow and bloated, you may need to completely remove NGINX, along with its configuration files and do a fresh installation. This may also be required if your NGINX version is really old and Linux package managers are unable to update them. In this case also, you will need to uninstall and then re-install NGINX.

How to Uninstall NGINX

As mentioned earlier, the steps to uninstall NGINX are different for Debian/Ubuntu and RPM systems. We will cover each of them one by one.

1. Uninstall NGINX in Ubuntu / Debian

Here are the steps to completely remove NGINX from Ubuntu/Debian systems.

1.1 Check NGINX status

You can uninstall NGINX only if it is not running. So, first we need to check the status of NGINX server on our system. You can do this using systemctl command.

$ sudo systemctl status nginx
○ nginx - A high performance web server and a reverse proxy>
Loaded: loaded (/lib/systemd/system/nginx; disabled; v>
Active: inactive (dead)

You should see its status as inactive (dead). Otherwise, stop it using the following command.

1.2 Stop NGINX Server

Stop NGINX server, if it is running, with the following command.

$ sudo systemctl stop nginx

1.3 Remove NGINX Installation

There are two components to NGINX server on a system – its installation and configuration. You can remove its installation with ‘apt remove’ command as shown below.

$ sudo apt remove nginx

The above command will remove NGINX installation but leave behind its configuration files in /etc/nginx. Also, it will not remove any of your website/app files located at /var/www. It is useful if you plan to re-install NGINX in future.

If you want, you can also add other NGINX modules that you want to remove, in the above command. Here is an example to remove nginx-commons package along with NGINX.

$ sudo apt remove nginx nginx-commons

If you also want to remove the configuration files of NGINX server, then use ‘apt purge’ command.

$ sudo apt purge nginx

The above command will delete NGINX installation, and also /etc/nginx directory along with its contents. I t will also remove all NGINX settings and configurations from your system. It is an excellent solution if you want to completely uninstall NGINX and have no plans to re-install it in future. It is also a great solution if you want to do a fresh installation of NGINX.

1.4 Remove Dependencies

Lastly, remove any other dependencies that were installed along with NGINX.

$ sudo apt-get autoremove

You will need to run this command irrespective of whether you uninstalled NGINX using ‘apt remove’ or ‘apt purge’ command.

1.5 Check NGINX status

Once you have removed NGINX server, check its status again to confirm its uninstallation. You should see the following output.

$ sudo systemctl status nginx
Unit nginx could not be found.

1.6 Force Remove NGINX

Sometimes, even after running all these commands, NGINX may not be completely removed. In fact, when you restart your system, it may start running as before. This may happen if you have multiple versions of NGINX installed on your system, and if you have not removed them all, or if you have removed the wrong version. This is because NGINX offers different flavors such as nginx-core, nginx-light, etc. In such cases, you can run the following command to completely remove all variants of NGINX on your system.

$ sudo apt-get remove --purge nginx*

The above command is similar to the purge command used earlier, except that we use a wildcard character after nginx to match all packages starting with nginx keyword.

2. Uninstall NGINX in RPM-based Systems

We need to follow a different sequence of steps to uninstall NGINX from RPM based Linux systems such as Redhat, CentOS, Fedora.

2.1 Check NGINX status

As mentioned before, we can remove NGINX from any system only if it is not running. So run the following command to check its status first.

$ sudo service status nginx
○ nginx - A high performance web server and a reverse proxy>
Loaded: loaded (/lib/systemd/system/nginx; disabled; v>
Active: inactive (dead)

2.2 Stop & Disable NGINX

If it is running, then you need to stop it first and also disable it.

$ sudo systemctl stop nginx
$ sudo systemctl disable nginx

2.3 Disable Auto Start

Run the following command to prevent NGINX from auto starting during system reboot.

$ sudo chkconfig nginx off

2.4 Remove NGINX Configuration & Settings

Next, we need to manually remove NGINX configuration and settings files from our system. They are located in different directories across the system. We will use rm command to delete them.

$ 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

2.3 Remove NGINX Service

Next, remove NGINX application service from your system with the following command.

$ sudo rm -rf /usr/lib/systemd/system/nginx

2.4 Remove NGINX Installation

Lastly, remove NGINX installation itself with any of the following commands. You can use dnf or yum package manager for this purpose.

$ sudo dnf remove nginx
OR
$ sudo yum remove nginx

2.5 Check Status

Again check status of NGINX service to confirm its uninstallation.

$ sudo systemctl status nginx
Unit nginx could not be found.

Uninstallation in RPM-based systems involves more manual steps compared to that for Debian/Ubuntu systems.

Conclusion

In this article, we have learnt why you may need to remove NGINX from your system. We have also learnt how to remove NGINX from Ubuntu/Debian as well as RPM-based systems. Depending on your requirement, you may choose to remove only NGINX installation but retain its configuration and settings, or completely remove NGINX installation altogether.

Also read:

How to Password Protect Directory in NGINX
NGINX Restrict Access to Directory & Subdirectories
How to Restrict URL Access in NGINX

Leave a Reply

Your email address will not be published. Required fields are marked *