How to Completely Reinstall Nginx with All Configuration Files on Ubuntu


3 views

When dealing with Nginx reinstallation on Ubuntu, many developers encounter missing configuration files after running apt-get install nginx. This typically occurs when previous removal wasn't clean or package dependencies aren't properly resolved.

First ensure you've completely purged existing Nginx installation:

sudo apt-get remove nginx nginx-common nginx-full --purge
sudo apt-get autoremove
sudo rm -rf /etc/nginx
sudo rm -rf /var/lib/nginx
sudo rm -rf /usr/share/nginx

After complete removal, install with all dependencies:

sudo apt-get update
sudo apt-get install nginx-full

Check if all essential files are present:

ls -l /etc/nginx/nginx.conf
ls -l /etc/nginx/mime.types
ls -l /etc/nginx/sites-available/default

If files are missing after installation, regenerate them:

sudo apt-get install --reinstall nginx-common
sudo service nginx restart

For complete control, consider compiling from source:

wget http://nginx.org/download/nginx-1.25.1.tar.gz
tar -xvf nginx-1.25.1.tar.gz
cd nginx-1.25.1
./configure --prefix=/usr/share/nginx \
            --sbin-path=/usr/sbin/nginx \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --http-log-path=/var/log/nginx/access.log \
            --with-pcre \
            --pid-path=/var/run/nginx.pid \
            --with-http_ssl_module
make
sudo make install

For "mime.types" missing error:

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo wget https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types -O /etc/nginx/mime.types

For permission issues:

sudo chown -R www-data:www-data /var/lib/nginx
sudo chmod -R 755 /var/lib/nginx

When you manually remove Nginx by deleting configuration files and binaries as shown below:

apt-get remove nginx 
rm -rf /etc/nginx/
rm -rf /usr/sbin/nginx
rm /usr/share/man/man1/nginx.1.gz
apt-get remove nginx*

You're left with a partially removed installation that causes errors during reinstallation. The key issue is that Ubuntu's package manager (apt) loses track of configuration files you manually deleted.

First, ensure all Nginx components are purged:

sudo apt-get purge nginx nginx-common nginx-core
sudo apt-get autoremove
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx

Then perform a fresh installation:

sudo apt-get update
sudo apt-get install nginx-full

If the configuration files are still missing after reinstallation, you can:

sudo apt-get install --reinstall nginx-common

This restores all default configuration files including:

  • /etc/nginx/nginx.conf
  • /etc/nginx/mime.types
  • /etc/nginx/sites-available/default

If you prefer to manually set up configurations, here's a basic nginx.conf template:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    
    gzip on;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

And the essential mime.types can be downloaded from Nginx's official repository if missing.

After proper installation, check the service status:

sudo systemctl status nginx

Test configuration syntax:

sudo nginx -t

If everything is correct, you should see:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful