> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Install Config Introduction

> Guide to installing and configuring Nginx on multiple platforms, managing its service lifecycle, structuring nginx.conf and server blocks, hosting a static site, and configuring firewall access

In this lesson you'll learn the essentials to install and configure Nginx across common platforms, manage its service lifecycle, and host a simple static website. We'll also review the nginx.conf layout and show how to secure your site with a firewall.

What you will cover:

* What package managers are and how they differ across platforms
* Installing Nginx on Ubuntu, CentOS/RHEL, macOS, and Windows
* Managing the Nginx service (status, start, stop, restart, reload)
* The structure of `nginx.conf` and the common include patterns (`sites-available` / `sites-enabled`)
* Creating server blocks (virtual hosts) to host static content
* Allowing HTTP/HTTPS through UFW (or platform equivalents)

## Package managers — quick overview

Package managers are tools that simplify installing, upgrading, configuring, and removing software by handling repositories, dependency resolution, and binary distribution. Common examples:

| Package Manager | Primary platforms | Notes / Example install command |
| - | - | - |
| apt | Debian, Ubuntu and derivatives | `sudo apt update && sudo apt install nginx` |
| yum / dnf | CentOS, RHEL, Fedora | `sudo yum install nginx` or `sudo dnf install nginx` |
| Homebrew | macOS | `brew install nginx` |
| Chocolatey / winget | Windows | `choco install nginx -y` or `winget install --id Nginx.Nginx` |

These commands are a starting point — some distributions may require enabling repositories (e.g., EPEL for older CentOS versions) or following distro-specific install notes.

## Installing Nginx — examples by OS

Ubuntu / Debian (APT)

```bash theme={null}
sudo apt update
sudo apt install nginx
# verify:
sudo systemctl status nginx
```

CentOS / RHEL / Fedora (yum / dnf)

```bash theme={null}
# CentOS 7 / RHEL 7 might need EPEL; many Fedora/RHEL systems use dnf
sudo yum install nginx      # or: sudo dnf install nginx
sudo systemctl enable --now nginx
```

macOS (Homebrew)

```bash theme={null}
brew update
brew install nginx
# Start as a background service:
brew services start nginx
# Or run in foreground:
nginx
```

Windows (Chocolatey / winget)

```powershell theme={null}
choco install nginx -y
# or with winget:
winget install --id Nginx.Nginx
# If installed as a service, use the Services MMC or:
sc start nginx
```

## Managing the Nginx service

Use the native service manager for your OS. Common commands:

| Action | systemd (Linux) | macOS (Homebrew) | Windows |
| - | - | - | - |
| Start | `sudo systemctl start nginx` | `brew services start nginx` | `sc start nginx` |
| Stop | `sudo systemctl stop nginx` | `brew services stop nginx` | `sc stop nginx` |
| Restart | `sudo systemctl restart nginx` | `brew services restart nginx` | `sc stop nginx` then `sc start nginx` |
| Reload config | `sudo systemctl reload nginx` or `sudo nginx -s reload` | `nginx -s reload` | `nginx -s reload` (if running) |
| Status | `sudo systemctl status nginx` | `brew services list` | Check Services MMC or `sc queryex nginx` |

Always test configuration before reload:

```bash theme={null}
sudo nginx -t
```

## Nginx configuration layout (nginx.conf, server blocks, and includes)

Nginx uses a hierarchical configuration system with global directives, the `events` block, the `http` block, and inside `http` one or more `server` blocks that contain `location` blocks. Debian-style layouts commonly separate sites into `sites-available` and `sites-enabled` and use `include` directives to load them.

Example minimal `nginx.conf` skeleton:

```nginx theme={null}
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    sendfile on;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # include per-site configs (Debian/Ubuntu style)
    include /etc/nginx/sites-enabled/*;

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

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

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

Common patterns:

* `include /etc/nginx/conf.d/*.conf;` for packaged configs
* `sites-available` + `sites-enabled` where `sites-enabled` contains symlinks to active configs
* Use `nginx -t` to validate config syntax and `systemctl reload nginx` to apply changes without dropping connections

![A presentation slide titled "Objectives" showing a numbered list. It outlines learning about package managers, installing Nginx on multiple OSes, managing Nginx services, and configuring nginx.conf.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Install-Config/Install-Config-Introduction/nginx-install-package-managers-services-config.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=493c0fbad30d1716e0cec4ce05003b76)

## Host a simple static website (server block / virtual host)

1. Create the site directory and a simple index file (example for Ubuntu/Debian):

```bash theme={null}
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
chmod -R 755 /var/www/example.com

cat > /var/www/example.com/html/index.html <<'HTML'
<!doctype html>
<html>
<head><meta charset="utf-8"><title>Example</title></head>
<body><h1>Welcome to example.com</h1></body>
</html>
HTML
```

2. Create a server block file (e.g., `/etc/nginx/sites-available/example.com`) and add the server block shown earlier.

3. Enable the site (Debian/Ubuntu):

```bash theme={null}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```

4. Verify by visiting `http://<SERVER_IP>/` or `http://example.com` if DNS points to your server.

Ports:

* HTTP — port 80
* HTTPS — port 443 (requires TLS certificate; consider Let's Encrypt for free certs)

## Firewall (UFW example)

On Ubuntu systems using UFW:

```bash theme={null}
sudo ufw allow 'Nginx Full'   # opens 80 and 443
sudo ufw allow 'Nginx HTTP'   # opens 80 only
sudo ufw enable
sudo ufw status
```

Be careful with firewall and SSH changes on remote servers; confirm you have another active session or console access before applying strict rules.

When managing services or editing system configuration you will typically need elevated privileges (for example, using `sudo`). If you are working on a remote server, be careful when changing firewall rules or SSH configuration so you don't lock yourself out.

## Summary and next steps

* Use your OS package manager to install Nginx and learn the service manager commands for start/stop/reload.
* Use `nginx -t` to test changes and `systemctl reload nginx` (or the OS equivalent) to apply them safely.
* Structure sites with separate server blocks and leverage `sites-available` / `sites-enabled` on Debian-style systems.
* Open the appropriate firewall ports and obtain TLS certificates for HTTPS.

Further reading and references:

* [Nginx Documentation](https://nginx.org/en/docs/)
* [Ubuntu Server Guide](https://ubuntu.com/server/docs)
* [systemd — managing services](https://www.freedesktop.org/wiki/Software/systemd/)
* [UFW — Uncomplicated Firewall](https://help.ubuntu.com/community/UFW)
* [Homebrew](https://brew.sh/)
* [Chocolatey](https://chocolatey.org/) | [winget](https://learn.microsoft.com/windows/package-manager/winget/)

> **[Watch Video](https://learn.kodekloud.com/user/courses/nginx-for-beginners/module/0de43784-b08d-4ce0-8470-a7541b78fe58/lesson/196d68ff-0e61-4b1b-a24b-3ef74ccf275c)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.