How to Install and Configure Nginx on Different Linux Flavors? Print

  • install Nginx, web server setup, Nginx commands, Linux flavors, Linux server, configure Nginx, Nginx setup, Nginx
  • 234

Introduction

Nginx is a powerful and high-performance web server that also serves as a reverse proxy, load balancer, and HTTP cache. This guide provides step-by-step instructions for installing and configuring Nginx on various Linux distributions.

Installing Nginx

Nginx is available in the official repositories of most Linux distributions. Follow the steps below to install it on your preferred flavor:

Ubuntu/Debian

sudo apt update
sudo apt install nginx -y

CentOS/RHEL

sudo yum install epel-release -y
sudo yum install nginx -y

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Fedora

sudo dnf install nginx -y

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Arch Linux

sudo pacman -S nginx

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Configuring Nginx

Default Configuration

The default configuration file is located at:

  • /etc/nginx/nginx.conf (Global settings)
  • /etc/nginx/conf.d/ (Virtual host configurations)

Creating a Basic Virtual Host

To configure a new site, create a file in the /etc/nginx/conf.d/ directory:

sudo nano /etc/nginx/conf.d/example.com.conf

Add the following content:

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

Create the root directory and an example HTML file:

sudo mkdir -p /var/www/example.com
sudo nano /var/www/example.com/index.html

Add your content to the file and save it.

Testing Configuration

Check for syntax errors in the configuration file:

sudo nginx -t

Reload Nginx to apply changes:

sudo systemctl reload nginx

Troubleshooting

    • Firewall Issues: Ensure ports 80 and 443 are open:
sudo ufw allow 'Nginx Full'
  • Syntax Errors: Use nginx -t to identify issues in the configuration files.
  • Logs: Check logs for error details:
    • Error log: /var/log/nginx/error.log
    • Access log: /var/log/nginx/access.log

Best Practices

  • Use SSL/TLS certificates for secure connections. Tools like Certbot can automate the process.
  • Regularly update Nginx to benefit from the latest features and security patches.
  • Optimize configuration for better performance by tuning worker processes and caching settings.

Examples

Reverse Proxy Setup

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Load Balancer Configuration

upstream backend {
    server 192.168.1.10;
    server 192.168.1.11;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
    }
}

Conclusion

Setting up and configuring Nginx on Linux is straightforward and highly customizable. With its performance and flexibility, Nginx is an excellent choice for web servers, reverse proxies, and load balancers. Following the steps and best practices outlined here will ensure a secure and efficient setup.


Was this answer helpful?

« Back