Installing and Configuring Nginx on Linux Ubuntu ,RedHat (CentOS)

Installing and Configuring Nginx on Linux Ubuntu ,RedHat (CentOS)

Nginx, renowned for its performance and stability, stands as a prevalent web server option for Linux systems.

Installing and configuring Nginx on Ubuntu:

Let's update the packages and install nginx using package manager APT

sudo apt update && sudo apt install -y nginx

above command will server two purposes updating the packages as well as installing nginx webserver.

Verify the installation

sudo nginx -v

Let us check nginx is installed and running

sudo systemctl status nginx if server is loaded and not running run this command sudo systemctl start nginx

Configuring Nginx

The default Nginx configuration serves content from the /var/www/html directory. To modify this behavior, locate the primary configuration file, typically found at /etc/nginx/sites-available/default. Utilize a text editor (e.g., nano, vim) to access and edit this file.

If you want to create an new configuration file which will include your domain and other configuration

sudo vi /etc/nginx/sites-available/newconfig.conf

server  {
        listen 80;
        server_name <dns name>;

        location / {
            proxy_pass http://localhost:8080/;
            proxy_redirect off;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade  $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }

Above snippet is for proxy_pass the request to localhost port

Once we created file we need to link the file to sites-enabled becuase by default nginx will read the config file from sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/newconfig.conf /etc/nginx/sites-enabled

Let's check nginx configuration is fine or not using below command

sudo nginx -t

You should get the results like above. Finally to pickup our new configuration changes by nginx let's restart the service

sudo systemctl restart nginx Now let's check the status of nginx service sudo systemctl status nginx


Installing and configuring Nginx RedHat (CentOS)

CentOS/RHEL/ Oracle Linux/AlmaLinux/Rocky Linux distributions uses yum or dnf as package managers.

Updating packages and installing EPEL repository (OS Packages repository)

sudo yum install epel-release -y

sudo yum update -y

Installing Nginx

sudo yum install nginx

Verify the installation

sudo nginx -v

Follow above configuration steps as same as ubuntu mentioned 😊

Read more