Sub topic 2
Installing Nginx on Linux
This chapter shows how to install Nginx from your distribution's package manager and confirm that it works. It also covers where the package puts its files and how to open the firewall. It assumes you can use the Linux command line and run commands with sudo. It also assumes you know what an HTTP request is and what a TCP port is. Configuration, service management, and HTTPS come in later chapters.
Choosing where Nginx comes from
You can get Nginx from three places.
- Your distribution's repositories. This is the default choice. Packages are tested with your release and receive security fixes through normal updates. The version is often older than the newest upstream release.
- The nginx.org repositories. The Nginx project publishes packages for major distributions. You can choose the stable branch or the mainline branch. Mainline gets new features sooner. Both branches receive bug fixes, and the project describes mainline as suitable for production.
- Compiling from source. This lets you pick exact modules and build options. You then own every update, so avoid it unless you need a module the packages lack.
For most servers, use the distribution package. Switch to nginx.org only when you need a feature or fix that your distribution's version lacks.
| Source | Version freshness | Updates | Typical use |
|---|---|---|---|
| Distribution package | Older, stable | Through `apt` or `dnf` | Most servers |
| nginx.org package | Current stable or mainline | Through `apt` or `dnf` after adding the repository | Newer features |
| Source build | Any | Manual | Custom modules |
Installing on Debian and Ubuntu
Update the package index first. Then install the nginx package. The Debian packaging starts the service for you right after installation.
sudo apt update
sudo apt install nginx
sudo systemctl enable --now nginx
systemctl status nginx
curl -I http://localhostThe enable --now command does two things. enable makes the service start at every boot. --now also starts it immediately if it is not already running. On Debian and Ubuntu the service is usually already enabled and running, so the command changes nothing. Running it is still a good habit, because it works the same on every distribution.
The last command, curl -I, sends a request and prints only the response headers. A healthy default install answers like this:
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Content-Type: text/htmlThe version number and distribution tag will differ on your system. If you see 200 OK and a Server: nginx header, the installation works.
Installing on RHEL, Fedora, Rocky, and AlmaLinux
These distributions use dnf. The package is also called nginx. The key difference is that installing does not start the service. You must start it yourself.
sudo dnf install nginx
sudo systemctl enable --now nginx
systemctl status nginx
curl -I http://localhostOn some RHEL-family releases, nginx is delivered as a module stream. If dnf install nginx installs an older version than you expected, list the streams with dnf module list nginx. You can then enable a newer one with sudo dnf module enable nginx:<stream> before installing.
What the package puts on disk
The layout differs a little between distribution families. Knowing it saves you from searching later.
| Item | Debian and Ubuntu | RHEL family |
|---|---|---|
| Main config file | `/etc/nginx/nginx.conf` | `/etc/nginx/nginx.conf` |
| Extra config | `/etc/nginx/sites-available/` and `sites-enabled/`, plus `conf.d/` | `/etc/nginx/conf.d/` |
| Default web root | `/var/www/html` | `/usr/share/nginx/html` |
| Worker user | `www-data` | `nginx` |
| Logs | `/var/log/nginx/` | `/var/log/nginx/` |
| Binary | `/usr/sbin/nginx` | `/usr/sbin/nginx` |
The sites-available and sites-enabled directories are a Debian convention, not part of Nginx itself. You write a file in sites-available and enable it by symlinking it into sites-enabled. The next chapters on configuration use these paths.
The worker user is the account that Nginx's worker processes run as. The master process runs as root so it can bind to ports 80 and 443. Workers drop to the unprivileged user. Any file you want to serve must be readable by that user.
Checking what you installed
Two flags report build information.
nginx -v
nginx -Vnginx -v prints the version. nginx -V (capital V) also prints the compiler flags and the configure arguments. The arguments show which modules are built in, such as --with-http_ssl_module and --with-http_v2_module. Check this output before you assume a feature exists. If a directive fails with "unknown directive", the module behind it is often missing from your build.
Some features ship as separate packages. On Debian and Ubuntu, dynamic modules are packaged as libnginx-mod-*, and the nginx-extras package bundles several of them. You load a dynamic module with a load_module line at the top of nginx.conf.
Opening the firewall
A running Nginx is still unreachable from other machines if a host firewall blocks ports 80 and 443. How you open them depends on the firewall tool.
For ufw on Ubuntu, the Debian package installs application profiles:
sudo ufw app list
sudo ufw allow 'Nginx Full'
sudo ufw statusThe profile Nginx Full opens ports 80 and 443. Nginx HTTP opens only 80, and Nginx HTTPS opens only 443.
For firewalld on the RHEL family:
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reloadWithout --permanent, the rule disappears at the next reload or reboot. Without --reload, the permanent rule does not take effect yet.
Cloud providers add a second layer. Security groups or network ACLs must also allow the ports. If curl http://localhost works on the server but a remote request times out, check the cloud rules as well as the host firewall.
The installation flow
Testing locally first separates two kinds of failure. If curl fails on the server, the problem is Nginx itself. If it works locally but fails remotely, the problem is the network path.
Common installation problems
Port 80 is already in use. Nginx cannot start if another process is listening on port 80. On Debian and Ubuntu, a preinstalled Apache is a frequent cause. Find the process with ss:
sudo ss -ltnp | grep ':80 'The -l flag lists listening sockets, -t limits output to TCP, -n shows numbers instead of names, and -p shows the owning process. Stop or reconfigure the other service, or change the port Nginx listens on. The error log in /var/log/nginx/error.log shows bind() to 0.0.0.0:80 failed (98: Address already in use) in this case.
The default page still shows after you add a site. The default site is still enabled. On Debian and Ubuntu, it is the symlink /etc/nginx/sites-enabled/default. Remove only the symlink, not the file in sites-available, when you replace it with your own site.
Permission denied on your files. The worker user cannot read the files or one of the directories above them. Every directory in the path needs execute permission for that user. Check with namei -l /path/to/file.
SELinux blocks access on the RHEL family. SELinux is a mandatory access control system. It restricts what Nginx may read and which network connections it may make, even when ordinary file permissions look correct. Files outside the standard web locations may need the right label. A proxy to a backend application may need the httpd_can_network_connect boolean. Look for "denied" lines in sudo ausearch -m avc -ts recent before you disable SELinux. Disabling it is rarely the right fix.
Keeping the installation updated
Security fixes arrive through the normal package update path. On Debian and Ubuntu, run sudo apt update && sudo apt upgrade. On the RHEL family, run sudo dnf upgrade. The package restarts or reloads the service as part of the upgrade, depending on the distribution's packaging. Plan upgrades for a quiet period on busy servers, and run systemctl status nginx afterward.
Exercise
You installed Nginx on a Rocky Linux server. dnf install nginx finished without errors, but curl -I http://localhost fails with "Connection refused". What is the most likely cause, and which command fixes it and also makes the fix survive a reboot?
Show answer
On the RHEL family the package does not start the service after installation, so nothing is listening on port 80. Run sudo systemctl enable --now nginx. The enable part starts it at boot and --now starts it immediately.
Exercise
A site served by Nginx on Ubuntu works with curl http://localhost on the server, but a request from your laptop times out. The service is running. Name two places to check, and the ufw command that opens both web ports.
Show answer
Check the host firewall (sudo ufw status) and the cloud provider's security group or network ACL. The command is sudo ufw allow 'Nginx Full', which opens ports 80 and 443.