Sub topic 2
Installing Nginx on Linux
Prerequisites and package installation
This chapter assumes that you can use a Linux shell, edit files with sudo, understand basic HTTP requests, and distinguish a DNS name from a TCP port. You also need a system that uses systemd, the service manager used by most current Debian, Ubuntu, Fedora, and RHEL installations.
Nginx is usually installed from your distribution’s package repository. A package repository is a trusted collection of software packages maintained for your operating system. Using it gives you a version that integrates with the operating system’s service manager, file layout, permissions, and security updates.
On Debian or Ubuntu, refresh the local package index, install Nginx, and start it immediately. The --now option starts the service and enables it to start automatically during boot.
sudo apt update
sudo apt install nginx
sudo systemctl enable --now nginx
systemctl status nginx
curl -I http://localhostThe final command sends an HTTP request to the local machine and prints only the response headers. A working installation normally returns a status such as HTTP/1.1 200 OK or HTTP/1.1 403 Forbidden. A 403 can still show that Nginx is running, because it means the server answered but refused access to the requested resource.
On Fedora or RHEL-compatible systems, use the corresponding package manager:
sudo dnf install nginx
sudo systemctl enable --now nginx
systemctl status nginx
curl -I http://localhostThe exact Nginx version depends on the distribution release and its configured repositories. Avoid adding a third-party repository until you have a reason, such as needing a particular feature or newer release. Third-party packages increase the number of sources you must trust and maintain.
What installation creates
The package installs the Nginx executable, a systemd unit, configuration files, log locations, and a default document root. The exact paths can vary by distribution, but Debian and Ubuntu commonly use these locations:
| Path | Purpose |
|---|---|
| `/etc/nginx/nginx.conf` | Main configuration file |
| `/etc/nginx/sites-available/` | Server-block files available for use |
| `/etc/nginx/sites-enabled/` | Enabled server-block links |
| `/var/www/html/` | Default static document root |
| `/var/log/nginx/` | Access and error logs |
The package may also create a default server block. It listens on TCP port 80, the conventional port for unencrypted HTTP. Open http://server-address/ in a browser, or use curl locally, to test it.
Confirming the service state
systemctl status nginx shows whether the service is active, recent startup messages, and the process identifier. For a compact check, use:
systemctl is-active nginx
systemctl is-enabled nginxactive means the process is currently running. enabled means systemd will attempt to start it during boot. These are separate properties. A service can be running without being enabled, or enabled without currently running because startup failed.
Exercise
What is the difference between systemctl enable nginx and systemctl start nginx?
Show answer
enable configures Nginx to start during boot. start launches it immediately. systemctl enable --now nginx performs both actions.