Where to install from: Ubuntu package, nginx.org, or Docker
There are three common ways to install nginx on Linux, and they differ in version, worker user, and directory layout.
| Version on Ubuntu 24.04 | Site configs | Worker user | Built-in modules | |
|---|---|---|---|---|
Official nginx.org |
1.30.8 (stable) or 1.31 (mainline) |
/etc/nginx/conf.d/*.conf |
nginx |
HTTP/2, HTTP/3 (quic), stream, realip, auth_request, stub_status, gzip_static |
Ubuntu package (apt install nginx) |
1.24.0 (security backports by Ubuntu) |
/etc/nginx/sites-available/ symlinked to sites-enabled/ |
www-data |
HTTP/2 included, no HTTP/3; ships ready snippets in /etc/nginx/snippets/ |
Docker (nginx:1.30-alpine) |
1.30.8 |
/etc/nginx/conf.d/*.conf and /etc/nginx/templates/ |
nginx |
same modules as the nginx.org package |
For fresh servers, use the official nginx.org repository: you get the current 1.30 branch with HTTP/3 support and the same directory layout as the nginx:1.30-alpine Docker image. If a server already runs the Ubuntu package, every config in this track works there too — just match the site config directory and worker user.
Install nginx 1.30 on Ubuntu 24.04
Install prerequisites and import the signing key
Install the keyring and HTTPS repo tools, then fetch the official
nginx.orgsigning key into/usr/share/keyrings/:sudo apt update sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor \ | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/nullVerify the key fingerprints — the
nginx.orgkeyring currently contains three signing keys:gpg --dry-run --quiet --no-keyring --import --import-options import-show \ /usr/share/keyrings/nginx-archive-keyring.gpg # pub rsa4096 2024-05-29 [SC] # 8540A6F18833A80E9C1653A42FD21310B49F6B46 # uid nginx signing key <[email protected]> # pub rsa2048 2011-08-19 [SC] [expires: 2027-05-24] # 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 # uid nginx signing key <[email protected]> # pub rsa4096 2024-05-29 [SC] # 9E9BE90EACBCDE69FE9B204CBCDCD8A38D88A2B3 # uid nginx signing key <[email protected]>Add the repository and pin its priority
Add the
nginx.orgstable repo (for mainline, change the URL tohttps://nginx.org/packages/mainline/ubuntu):echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ https://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.listPin
nginx.orgahead of Ubuntu’s repo. Without this file, a laterapt upgradecan overwrite thenginx.orgpackage with Ubuntu’s build:sudo tee /etc/apt/preferences.d/99nginx >/dev/null <<'EOF' Package: * Pin: origin nginx.org Pin: release o=nginx Pin-Priority: 900 EOFInstall and enable the service
Refresh package lists, install nginx, and enable it on boot:
sudo apt update sudo apt install -y nginx sudo systemctl enable --now nginxCheck the version and compiled modules:
nginx -v # nginx version: nginx/1.30.8 nginx -V 2>&1 | tr ' ' '\n' | grep '^--with-' # --with-http_v2_module # --with-http_v3_module # --with-http_realip_module # --with-http_auth_request_module # --with-http_stub_status_module # --with-stream # --with-stream_ssl_preread_moduleLowercase
-vprints only the version; uppercase-Vdumps the compiler version, OpenSSL build, and every compiled-in module. Before using a directive likereal_ip_headerorlisten 443 quic, checknginx -Vto confirm your binary includes that module.Verify the HTTP response and worker tree
Hit the default page locally and inspect the running processes:
curl -sI http://127.0.0.1/ # HTTP/1.1 200 OK # Server: nginx/1.30.8 # Content-Type: text/html # Content-Length: 896 # Connection: keep-alive ps -eo pid,ppid,user,cmd | grep '[n]ginx' # 39191 1 root nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf # 39192 39191 nginx nginx: worker process # 39193 39191 nginx nginx: worker processOn a two-core host you get one
masterprocess running asrootand two workers running asnginx. When exposing the server publicly, open ports 80 and 443 in the firewall (The ufw firewall).
Inside /etc/nginx
An nginx.org install keeps /etc/nginx/ flat and minimal:
/etc/nginx/
├── nginx.conf # main file: user, workers, global http settings
├── conf.d/
│ └── default.conf # default virtual server on port 80
├── mime.types # maps file extensions to Content-Type headers
├── fastcgi_params # standard variables for PHP-FPM
├── uwsgi_params # variables for uWSGI
├── scgi_params # variables for SCGI
└── modules -> /usr/lib/nginx/modules # dynamic modules (Brotli, njs, etc.)
The main /etc/nginx/nginx.conf stays short and sets server-wide defaults:
user nginx;
worker_processes auto;Worker user and count. auto spawns one worker per CPU core. On Ubuntu’s distro package, user is www-data.
default:worker_processes 1
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;Error log path and severity (notice logs startup, reloads, and warnings; raise to info or debug while troubleshooting), plus the master PID file path.
default:error_log logs/error.log error
events {
worker_connections 1024;
}Max concurrent connections per worker (client sockets plus upstream connections). Ubuntu’s package defaults to 768; nginx.org sets 1024.
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;MIME type map so .css sends text/css and .svg sends image/svg+xml. Unknown extensions fall back to application/octet-stream (browser prompts to download).
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;Defines the main access log format and destination. Custom formats (such as JSON with upstream timings) go here too.
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;sendfile on transfers files from disk to socket in the kernel without copying them into user space. The commented #tcp_nopush on pairs with sendfile: when enabled (Linux TCP_CORK), it combines HTTP headers and the start of the file into one TCP packet instead of two small ones. keepalive_timeout sets how many seconds an idle client connection stays open. gzip is off by default.
include /etc/nginx/conf.d/*.conf;
}Loads every *.conf file in conf.d/. Files without a .conf suffix (like example.org.disabled) are ignored — handy for turning a site off without deleting its config.
Organizing your own files
Use one file per site or service in /etc/nginx/conf.d/, named after the domain (example.org.conf, api.example.org.conf). Put repeated blocks (security headers, TLS settings, proxy headers) in /etc/nginx/snippets/ (create it with sudo mkdir -p /etc/nginx/snippets) and pull them in with include snippets/headers.conf;.
To disable a site temporarily without deleting it, rename it to .conf.off and reload: the *.conf glob stops matching it.
If the host uses the Debian or Ubuntu package
The distro package inherits Apache’s layout:
| Item | nginx.org package & Docker |
Debian / Ubuntu package |
|---|---|---|
| Main config includes | conf.d/*.conf |
conf.d/*.conf and sites-enabled/* |
| Site config files | /etc/nginx/conf.d/example.org.conf |
/etc/nginx/sites-available/example.org |
| Enabling a site | filename ends in .conf |
symlink inside /etc/nginx/sites-enabled/ |
| Default web root | /usr/share/nginx/html |
/var/www/html |
| Worker user | nginx |
www-data |
To nginx itself both layouts are identical — it only follows include directives. On Ubuntu you can put site files straight into /etc/nginx/conf.d/*.conf, just stick to one scheme per server so you don’t hunt for duplicate server_name blocks across two directories.
Validating and applying changes safely
Run the syntax check before every reload:
sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
When a trailing semicolon ; is missing, nginx reports the next line — the first token it didn’t expect:
nginx: [emerg] invalid parameter "server_name" in /etc/nginx/conf.d/example.org.conf:5
nginx: configuration file /etc/nginx/conf.d/example.org.conf test failed
Line 5 (server_name) is fine here; line 4 (listen 80) is missing its ;.
nginx -T — inspect the full assembled config
Once configs are split across dozens of include files in conf.d/, snippets/, and sites-enabled/, it gets hard to tell which files are loaded and which server block comes first. sudo nginx -T runs the syntax check and prints every included file in load order:
sudo nginx -T 2>&1 | grep '^# configuration file'
# # configuration file /etc/nginx/nginx.conf:
# # configuration file /etc/nginx/mime.types:
# # configuration file /etc/nginx/conf.d/default.conf:
That one-liner immediately confirms whether a new file was picked up and where it sits in the load order.
reload instead of restart
Always chain the syntax test with a zero-downtime reload:
sudo nginx -t && sudo systemctl reload nginx
In /usr/lib/systemd/system/nginx.service, reload sends SIGHUP to master. The master re-reads the config, starts new workers, and tells old workers to finish in-flight requests and exit. If the new config fails to load, master logs the error to error.log and keeps the old workers serving traffic.
Worker file permissions
Workers read web files as nginx (or www-data), not root. Putting a site under /home/example-user/site is a frequent trap: Ubuntu creates home directories with mode 750 or 700, blocking the worker from traversing the path even if index.html is 644. The browser sees 403 Forbidden, and /var/log/nginx/error.log logs Permission denied.
Keep site roots under /var/www/<domain>/ with 755 on directories and 644 on files. Verify traversal across the full directory chain as the worker user:
sudo -u nginx namei -l /usr/share/nginx/html/index.html
# f: /usr/share/nginx/html/index.html
# drwxr-xr-x root root /
# drwxr-xr-x root root usr
# drwxr-xr-x root root share
# drwxr-xr-x root root nginx
# drwxr-xr-x root root html
# -rw-r--r-- root root index.html
Every directory in the chain needs execute (x) permission for the worker’s group or others (o+x), and the target file needs read (r).
How to check
- Confirm version and service state:
nginx -v && systemctl is-active nginx # nginx version: nginx/1.30.8 # active - Validate syntax and list every included file:
sudo nginx -t && sudo nginx -T 2>&1 | grep '^# configuration file' - Verify a reload keeps the
masterPID intact and replaces only worker PIDs:pgrep -a nginx sudo systemctl reload nginx pgrep -a nginx - Confirm nginx responds on port 80:
curl -sI http://127.0.0.1/ | head -n 5 # HTTP/1.1 200 OK # Server: nginx/1.30.8
If it doesn’t work
After adding the nginx.org repo, apt install nginx still installed Ubuntu’s 1.24 package
- Cause
/etc/apt/preferences.d/99nginxis missing or has a typo in a header field: with equal priorities,aptprefers the distro package.- Fix
- Check priorities with
apt-cache policy nginx:https://nginx.org/packages/ubuntushould show900. Rewrite99nginxand rerunsudo apt update && sudo apt install nginx.
apt update fails with E: Invalid record in the preferences file /etc/apt/preferences.d/99nginx, no Package header
- Cause
- Line breaks got flattened in
/etc/apt/preferences.d/99nginx(for example,printfwithout expanding\n) or an extra blank line landed at the top. - Fix
- Rewrite the file with
printf '%s\n' …or acat <<'EOF'heredoc so each header (Package:,Pin:,Pin-Priority:) sits on its own line.
sudo nginx -t flags a valid line: invalid parameter "server_name" in …:5
- Cause
- The previous line is missing its trailing semicolon
;. nginx joins directives across line breaks and treatsserver_nameas an extra argument tolisten. - Fix
- Check one or two lines above the reported line number and add the missing
;.
You added a site config in /etc/nginx/conf.d/ and reloaded, yet nginx ignores it
- Cause
/etc/nginx/nginx.confloadsinclude /etc/nginx/conf.d/*.conf;— files without a.confsuffix are skipped. On Ubuntu’s distro package, the usual cause is dropping a file insites-available/without symlinking it intosites-enabled/.- Fix
- Rename the file to
example.org.conf(or create theln -slink on the Ubuntu package) and confirm it appears insudo nginx -T | grep '^# configuration file'.
sudo nginx -s reload fails with open() "/run/nginx.pid" failed (2: No such file or directory)
- Cause
- The nginx service is stopped, so there is no PID file to signal. It can also happen if you changed the
piddirective innginx.conf. - Fix
- Start the service with
sudo systemctl start nginxand checksystemctl status nginx. On a systemd host, usesystemctl reload nginxinstead of callingnginx -smanually.