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:
- Nginx documentation: https://nginx.org/en/docs/
- Ubuntu packages/apt: https://help.ubuntu.com/community/AptGet/Howto
1. Update package lists
Refresh the package index to ensure you install the latest available Nginx package and dependencies: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:
nginx.
3. Verify and start the Nginx service
Check the service status through systemd to see whether Nginx is running, enabled, or stopped:Active: active (running) — this confirms Nginx is serving.
4. Test with curl
Confirm Nginx responds on the local host by requesting the default page:5. Inspect the Nginx configuration directory
Nginx’s configuration is usually kept under/etc/nginx. List and inspect the directory to understand its structure:
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:
include /etc/nginx/conf.d/*.confandinclude /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 intosites-enabled/. Keepnginx.conffocused 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.6. Logs
Nginx writes its logs to/var/log/nginx by default. List the directory to find the access and error logs:
access.logrecords requests served by Nginx.error.logrecords 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-availableandsites-enabled. - Configure custom access/error logs per site.
- Add SSL/TLS (Let’s Encrypt) and multi-site server blocks for secure hosting.
- Nginx Beginner’s Guide: https://nginx.org/en/docs/beginners_guide.html
- Ubuntu Nginx package on Launchpad/Debian packaging notes (for distro-specific differences).