> ## 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.

# Demo Nginx Install Config

> Guide to install, start, verify, and inspect Nginx on Ubuntu including configuration files, testing local response, service management, and log checks.

In this lesson you'll install and verify Nginx on an Ubuntu system, inspect its configuration, and confirm the service responds locally. The same workflow applies to other Linux distributions — on CentOS/RHEL use `yum` or `dnf` instead of `apt`. Links to official docs are included for further reading.

If you're on Debian/Ubuntu use `apt` (or `apt-get` for scripts). On CentOS/RHEL use `yum` or `dnf`. Package names and service management with `systemctl` are the same across most modern distributions.

## Overview

Steps covered:

1. Update package lists
2. Install Nginx
3. Start and verify the Nginx service
4. Confirm Nginx responds locally
5. Review Nginx configuration files
6. Check Nginx logs

Recommended references:

* [Nginx Documentation](https://nginx.org/en/docs/)
* [Ubuntu apt guide](https://help.ubuntu.com/community/AptGet/Howto)
* [systemctl (systemd) documentation](https://www.freedesktop.org/software/systemd/man/systemctl.html)

***

## 1) Update package lists

Update the local package index so you install the latest package metadata. Note: `apt update` only refreshes metadata — it does not accept `-y`. Use `-y` with commands that change the system like `apt install` or `apt upgrade`.

```bash theme={null}
bob@alpine-host ~ ➜ sudo apt update
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [128 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [4,219 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [4,329 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [1,298 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [30.9 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates InRelease [128 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease [128 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:11 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1,275 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [4,517 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [34.6 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [4,695 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1,589 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [28.6 kB]
Get:18 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [55.2 kB]
Fetched 34.3 MB in 3s (10.7 MB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
47 packages can be upgraded. Run 'apt list --upgradable' to see them.

bob@alpine-host ~ ➜
```

***

## 2) Install Nginx

Install Nginx using `apt install`. The sample below runs interactively (no `-y`) so you can see the prompt; add `-y` to accept automatically in scripts.

```bash theme={null}
bob@alpine-host ~ ➜ sudo apt install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx-common nginx-core
Suggested packages:
  fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx nginx-common nginx-core
0 upgraded, 7 newly installed, 0 to remove and 47 not upgraded.
Need to get 604 kB of archives.
After this operation, 2,141 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
```

It is normal for Nginx to pull in related modules and helper packages.

***

## 3) Start and verify the Nginx service

Check the service status with `systemctl`. If the service is inactive, start it and verify it becomes active.

```bash theme={null}
bob@alpine-host ~ ➜ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: inactive (dead)
    Docs: man:nginx(8)

bob@alpine-host ~ ✖ sudo systemctl start nginx

bob@alpine-host ~ ➜ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Wed 2025-02-05 11:59:11 EST; 5s ago
    Docs: man:nginx(8)
    Process: 6598 ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;' (code=exited, status=0/SUCCESS)
    Process: 6599 ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;' (code=exited, status=0/SUCCESS)
    Main PID: 6600 (nginx)
    Tasks: 17 (limit: 154503)
    Memory: 14.4M
    CGroup: /system.slice/nginx.service
    ├─6600 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
    ├─6601 nginx: worker process
    ├─6602 nginx: worker process
    └─... (additional worker processes)
```

Focus on the `Active: active (running)` line to confirm the service is up.

Before reloading Nginx after any configuration change, validate the config syntax to avoid downtime: `sudo nginx -t`. If the test passes, apply changes with `sudo systemctl reload nginx`.

***

## 4) Confirm Nginx is responding

Make a local HTTP request with `curl` to verify the default site:

```bash theme={null}
bob@alpine-host ~ ➜ curl localhost
```

A default installation returns the "Welcome to nginx!" HTML page, confirming the server is responding on `localhost`.

***

## 5) Review Nginx configuration files

On Debian/Ubuntu, Nginx configuration lives under `/etc/nginx`. A typical directory listing looks like this:

```bash theme={null}
bob@alpine-host ~ ➜ cd /etc/nginx

bob@alpine-host /etc/nginx🔐➜ ll
total 76
drwxr-xr-x 8 root root 4096 Feb 5 11:58 ./
drwxr-xr-x 1 root root 4096 Feb 5 11:58 ../
drwxr-xr-x 2 root root 4096 Sep 10 09:52 conf.d/
-rw-r--r-- 1 root root 1077 Mar 20 2024 fastcgi.conf
-rw-r--r-- 1 root root 1007 Mar 20 2024 fastcgi_params
-rw-r--r-- 1 root root 2837 Mar 20 2024 koi-utf
-rw-r--r-- 1 root root 2223 Mar 20 09:52 koi-win
-rw-r--r-- 1 root root 3957 Mar 20 2024 mime.types
drwxr-xr-x 2 root root 4096 Sep 10 09:52 modules-available/
drwxr-xr-x 2 root root 4096 Feb 5 11:58 modules-enabled/
-rw-r--r-- 1 root root 1490 Mar 20 2024 nginx.conf
-rw-r--r-- 1 root root 180 Mar 20 2024 proxy_params
-rw-r--r-- 1 root root 636 Mar 20 2024 scgi_params
drwxr-xr-x 2 root root 4096 Feb 5 11:58 sites-available/
drwxr-xr-x 2 root root 4096 Feb 5 11:58 sites-enabled/
drwxr-xr-x 2 root root 4096 Feb 5 11:58 snippets/
-rw-r--r-- 1 root root 664 Mar 20 2024 uwsgi_params
-rw-r--r-- 1 root root 3071 Mar 20 2024 win-utf
```

A simplified excerpt of a typical `/etc/nginx/nginx.conf`:

```nginx theme={null}
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_names_hash_bucket_size 64;
include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE (see https://en.wikipedia.org/wiki/POODLE)

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript
;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
```

Notes on important elements:

* The `http` block contains global HTTP settings, logging, gzip, etc.
* `access_log` and `error_log` define where logs are written (default: `/var/log/nginx/`).
* Include lines load additional files:
  * `include /etc/nginx/conf.d/*.conf;` loads `.conf` files from `conf.d`.
  * `include /etc/nginx/sites-enabled/*;` loads enabled site definitions.
* Best practice: place site `server { ... }` blocks in `sites-available/` and symlink enabled ones into `sites-enabled/`.

The example `ssl_protocols` line lists TLSv1 and TLSv1.1. For production, enable only modern protocols (for example: `ssl_protocols TLSv1.2 TLSv1.3;`) and follow current TLS hardening guides. Older protocols are insecure.

Common commands and file locations:

| Purpose | Command / Path | Notes |
| -: | - | - |
| Test configuration | `sudo nginx -t` | Validate before reload to prevent downtime |
| Reload service | `sudo systemctl reload nginx` | Graceful config reload |
| Service status | `sudo systemctl status nginx` | Check `Active:` line |
| Global config | `/etc/nginx/nginx.conf` | Contains `http` block and global settings |
| Site configs (available) | `/etc/nginx/sites-available/` | Store per-site `server` blocks |
| Site configs (enabled) | `/etc/nginx/sites-enabled/` | Symlinks to enabled sites |
| Additional confs | `/etc/nginx/conf.d/*.conf` | Misc includes |

***

## 6) Check logs

Nginx logs live in `/var/log/nginx`. Immediately after install they may be minimal:

```bash theme={null}
bob@alpine-host /etc/nginx🔒 ➜ cd /var/log/nginx/
bob@alpine-host /var/log/nginx🔒 ➜ ll
total 16
drwxr-xr-x 2 root     adm   4096 Feb  5 11:58 ./
drwxr-xr-x 1 root     root  4096 Feb  5 11:58 ../
-rw-r----- 1 www-data adm     86 Feb  5 12:00 access.log
-rw-r----- 1 www-data adm      0 Feb  5 11:58 error.log
```

As you serve traffic or test sites, `access.log` and `error.log` will populate. You can configure per-site log paths within each `server` block.

***

That's it for this lesson. To practice, create two sample virtual hosts under `sites-available/`, enable them by symlinking into `sites-enabled/`, and test access and logging.

> **[Watch Video](https://learn.kodekloud.com/user/courses/nginx-for-beginners/module/0de43784-b08d-4ce0-8470-a7541b78fe58/lesson/8a76fda2-9055-43c0-af1e-f08df0c8cb1a)**


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