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.
# 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 nginxRunning 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
1. Why is the official Nginx repository often preferred over a Linux distribution's default package for production use?
2. What command should you run before reloading Nginx to catch configuration errors?
3. Which command applies a new Nginx configuration without dropping existing client connections?
4. When running Nginx in Docker, what is the common pattern for supplying a custom configuration?
5. What is a limitation of 'nginx -t' that developers should be aware of?
Was this page helpful?
You May Also Like
What Is Nginx?
An introduction to Nginx as a high-performance web server, reverse proxy, and load balancer, and why it dominates modern web infrastructure.
The Nginx Configuration File Structure
How Nginx's nested context hierarchy, directive inheritance, and include files organize a production configuration.
Nginx Architecture and Worker Processes
How Nginx's master-worker process model and event-driven design deliver high concurrency with a small memory footprint.
Related Reading
Related Study Notes in DevOps
Browse all study notesAnsible Study Notes
DevOps · 30 topics
DevOpsAdvanced Kubernetes Study Notes
Kubernetes · 30 topics
DevOpsAdvanced Bash Scripting Study Notes
Bash · 30 topics
DevOpsApache Kafka Study Notes
Kafka · 30 topics
DevOpsDocker & Kubernetes Study Notes
YAML · 40 topics
DevOpsCI/CD Tools & Pipelines Study Notes
YAML · 37 topics