100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
DevOps

Installing and Running Nginx

How to install Nginx via package managers or Docker, and how to manage the running service safely.

FoundationsBeginner6 min readJul 10, 2026
Analogies

Installing and Running Nginx

There are several common ways to get Nginx running: installing the version bundled with your Linux distribution's package manager (apt on Ubuntu/Debian, yum or dnf on RHEL/CentOS), adding the official Nginx repository for the latest stable release, pulling the official Docker image, or compiling from source for custom module support. For most production use, the official repository or the official Docker image is preferred over the distribution's bundled package, since distro packages often lag several minor versions behind.

🏏

Cricket analogy: Choosing between apt's older Nginx version and the official repo's latest release is like a franchise picking a reliable domestic player from the local circuit versus signing a proven international star like Jofra Archer through a specific overseas draft.

Managing the Nginx Service

Once installed via a package manager, Nginx typically registers as a systemd service, managed with 'systemctl start/stop/restart/status nginx'. Before applying any configuration change, always run 'nginx -t' to validate the syntax; if it reports 'syntax is ok' and 'test is successful', it is then safe to run 'systemctl reload nginx' or 'nginx -s reload' to apply the change without dropping active connections.

🏏

Cricket analogy: Running 'nginx -t' before reloading is like a third umpire checking replay footage before confirming a decision, verifying correctness before it goes live and affects the whole match.

bash
# Ubuntu/Debian: install from the official Nginx repository
sudo apt update
sudo apt install -y curl gnupg2 ca-certificates lsb-release
curl https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update && sudo apt install -y nginx

# Managing the service
sudo systemctl enable --now nginx
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl status nginx

Running Nginx in Docker

The official 'nginx' Docker image provides a pre-built, minimal environment. Rather than baking a custom config into a new image for every change, it's common to mount a local nginx.conf or server block file as a volume into the container's /etc/nginx/conf.d/ directory, and expose port 80 (and 443 for TLS) from the container to the host so the same base image can run different sites just by swapping the mounted config.

🏏

Cricket analogy: Running Nginx in Docker with a custom config mounted as a volume is like a franchise using a standard team kit but swapping in a customized batting order sheet for each match.

Running 'nginx -t' checks configuration syntax but cannot catch every runtime issue, such as a backend that's unreachable. Always follow a config change with a quick manual request against a live endpoint after reloading to confirm the change behaves as intended in practice.

  • Nginx can be installed via a distro's package manager, the official Nginx repository, Docker, or compiled from source.
  • The official repository or Docker image is generally preferred for production since distro packages often lag behind.
  • 'systemctl start/stop/restart/status nginx' manages the service when installed via a package manager.
  • Always run 'nginx -t' to validate configuration syntax before reloading.
  • 'systemctl reload nginx' or 'nginx -s reload' applies configuration changes without dropping active connections.
  • The official Docker image is commonly run with a custom config mounted as a volume rather than baked into a custom image.
  • Validating syntax does not guarantee runtime correctness, so verify behavior with a real request after reloading.

Practice what you learned

Was this page helpful?

Topics covered

#DevOps#NginxStudyNotes#InstallingAndRunningNginx#Installing#Running#Nginx#Managing#StudyNotes#SkillVeris#ExamPrep