Skip to main content
This lesson shows how to install, verify, and inspect Nginx on an Ubuntu-based system. Commands are shown for apt-based distributions; on CentOS/RHEL use yum or dnf instead. Paths like /etc/nginx and /var/log/nginx are common, but verify them for your specific OS.
If you’re on CentOS or Red Hat use yum or dnf instead of apt. The configuration directories (/etc/nginx, /var/log/nginx) are common across many Linux distributions but may vary—especially with custom packages or older releases.

Quick command reference

Useful references:

1. Update package lists

Refresh the package index to ensure you install the latest available Nginx package and dependencies:
Tip: apt update only refreshes the package index. If you want to upgrade installed packages non-interactively, run sudo apt -y upgrade. Example output when updating package lists:

2. Install Nginx

Install Nginx using apt. Running interactively will prompt for confirmation; include -y to skip prompts:
Typical install output (shows dependencies and disk usage before confirmation):
It is normal to see several modules and helper packages installed along with nginx.

3. Verify and start the Nginx service

Check the service status through systemd to see whether Nginx is running, enabled, or stopped:
If Nginx was just installed but not started, you might see:
Start Nginx:
Then confirm it is active (running):
Example of an active status:
The key line is Active: active (running) — this confirms Nginx is serving.

4. Test with curl

Confirm Nginx responds on the local host by requesting the default page:
You should see the default “Welcome to nginx!” HTML page when Nginx is serving content correctly.

5. Inspect the Nginx configuration directory

Nginx’s configuration is usually kept under /etc/nginx. List and inspect the directory to understand its structure:
Example output showing typical files and directories:
The primary global configuration file is nginx.conf. It sets global directives (worker processes, logging, gzip, etc.) and includes other configuration files via include statements. Relevant excerpts from a typical /etc/nginx/nginx.conf:
Notes:
  • include /etc/nginx/conf.d/*.conf and include /etc/nginx/sites-enabled/* pull in additional server blocks and per-site configs.
  • Best practice: keep site definitions in sites-available/ and enable them by symlinking into sites-enabled/. Keep nginx.conf focused on global settings.
Always validate configuration changes before reloading the service. Run sudo nginx -t to test syntax and correctness. Failing to test can cause Nginx to reject a reload and potentially interrupt service.
Before applying changes:
If the test is successful, reload Nginx to apply configuration changes without restarting the process:

6. Logs

Nginx writes its logs to /var/log/nginx by default. List the directory to find the access and error logs:
Example output after a fresh install:
  • access.log records requests served by Nginx.
  • error.log records runtime errors and warnings. You can configure per-site log locations inside server blocks to separate logs per virtual host.

Next steps

In a follow-up lesson you’ll learn to:
  • Create and enable site configurations (virtual hosts) using sites-available and sites-enabled.
  • Configure custom access/error logs per site.
  • Add SSL/TLS (Let’s Encrypt) and multi-site server blocks for secure hosting.
For further reading:

Watch Video