Hosting + Ai Website Builder + Free Domain (3 Month Free Credit)
Shop Today

How to Use Nginx on Linux Server [Beginner to Expert]

Use Nginx on a Linux Server to enhance your website’s speed, scalability, and security. Nginx is a powerful, open-source web server recognized for its high performance, efficient use of resources, and suitability for modern web applications.

Configure Nginx on a Linux

In this article, you’ll learn how to use Nginx on a Linux server for web hosting, including installation, setup, configuration, and essential management tasks.

Prerequisites

  • Supported Linux distributions: Ubuntu, Debian, CentOS, Red Hat, Oracle Linux
  • Root or sudo access: Required to install and manage Nginx
  • Terminal access: Ability to execute commands on your Linux server

Steps to Use Nginx on a Linux Server

Use Nginx on a Linux server to create a high-performance, lightweight, and secure web hosting environment. Nginx (pronounced “Engine-X”) is a powerful open-source web server and reverse proxy used for serving static content, load balancing, and handling high-traffic websites. When paired with the stability and security of Linux, Nginx delivers exceptional speed, efficiency, and reliability, making it a top choice for modern web infrastructure.

Step 1: Install Nginx on the Linux Server

Use Nginx on a Linux server by installing it through your distribution’s package manager.

  • Ubuntu/Debian:
sudo apt update sudo apt install nginx
  • CentOS/Red Hat:
sudo yum update sudo yum install nginx

These commands retrieve the latest package lists, then install Nginx from official repositories.

Step 2: Start and Enable Nginx Service

To use Nginx on a Linux server, ensure the service is running and set to start automatically on boot.

Start Nginx:

  • Ubuntu/Debian:
sudo systemctl start nginx
  • CentOS/Red Hat:
sudo systemctl start nginx

Enable Nginx to start on boot:

  • Ubuntu/Debian:
sudo systemctl enable nginx
  • CentOS/Red Hat:
sudo systemctl enable nginx

Check service status:

  • Ubuntu/Debian:
sudo systemctl status nginx
  • CentOS/Red Hat:
sudo systemctl status nginx

Step 3: Configure the Firewall for Nginx

Allow web traffic by updating firewall settings for Nginx.

  • Ubuntu/Debian (UFW):
sudo ufw allow 'Nginx Full' sudo ufw reload
  • CentOS/Red Hat (firewalld):
sudo firewall-cmd --permanent --add-service=http 
sudo firewall-cmd --permanent --add-service=https 
sudo firewall-cmd --reload

This step ensures your server can respond to HTTP and HTTPS requests while you use Nginx on a Linux server for hosting web content.

Step 4: Test Nginx Installation

Check Nginx version:

  • Ubuntu/Debian:
nginx -v
  • CentOS/Red Hat:
nginx -v

Displays the installed Nginx version, confirming a successful installation.

  • Access the default web page:

Open your browser and visit http://your_server_ip (replace with your server’s IP address). You should see the Nginx welcome page, confirming that Nginx is serving web content.

Step 5: Basic Configuration and Management

To effectively manage Nginx on a Linux server, it’s important to understand where and how to edit its configuration files. These files control how your server handles requests, serves content, and routes traffic. Proper configuration is key to optimizing performance, security, and reliability.

Editing Nginx Config Files

  • Main configuration file:
    /etc/nginx/nginx.conf
  • Site-specific configs (Ubuntu/Debian):
    • /etc/nginx/sites-available/
    • /etc/nginx/sites-enabled/
  • Site configs (CentOS/Red Hat):
    • Add site blocks directly in /etc/nginx/nginx.conf or create new files in /etc/nginx/conf.d/

Setting Up Server Blocks (Virtual Hosts)

Use Nginx server blocks to host multiple domains on one Linux server.

Ubuntu/Debian example:

  • Create your website’s root directory:
sudo mkdir -p /var/www/example.com/html sudo chown -R $USER:$USER /var/www/example.com/html
  • Create a new server block config:
sudo nano /etc/nginx/sites-available/example.com
  • Add server block:
server { listen 80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html index.htm; access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; }
  • Enable your site and reload Nginx:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo systemctl reload nginx
  • CentOS/Red Hat example:

Add similar server block to /etc/nginx/conf.d/example.com.conf and reload Nginx.

Common Nginx Commands

Here are essential commands and log file paths every Linux admin should know when managing Nginx:

  • Restart Nginx:
sudo systemctl restart nginx
  • Test configuration syntax:
sudo nginx -t
  • View logs:
    • Access log: /var/log/nginx/access.log
    • Error log: /var/log/nginx/error.log

Conclusion

Use Nginx on a Linux server to take full advantage of efficient, scalable, and secure web hosting. By following these steps to install, configure, and manage Nginx, you ensure your server is ready for personal and business websites alike. With Nginx running on your Linux server, you gain the performance and flexibility needed for today’s web, making it easier than ever to deploy fast, reliable, and secure applications. For more information, visit the NGINX official documentation.

Himanshu Joshi

Leave a Comment

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

Scroll to Top