How to Install Nginx on Linux: Ubuntu, Debian and Fedora Print

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

To install Nginx on Ubuntu or Debian, update the package list and install the nginx package. On Fedora, use dnf. Then check the service and request the default page from the server itself.

Install Nginx on Ubuntu or Debian

sudo apt update
sudo apt install nginx

Install Nginx on Fedora

sudo dnf install nginx
sudo systemctl enable --now nginx

Verify the installation

systemctl is-active nginx
curl -I http://127.0.0.1/

If Nginx fails to start, check sudo systemctl status nginx. On a server already running a website, first check which service owns ports 80 and 443. After editing Nginx configuration, run sudo nginx -t before sudo systemctl reload nginx. See the Ubuntu Server installation guide.

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